[Catalyst] DBIC v Cache: What are catalyst best practices for
caching DBIC results (with relationships included)
Ashley Pond V
apv at sedition.com
Wed Nov 14 20:08:29 GMT 2007
I'm having trouble getting Plugin::Cache to handle DBIC objects
correctly. Past instruction, and recent refresher, from MST shows
that you have to reset/revive the result source and this works for a
single object but blows up for prefetch.
In this code
my $article = $c->cache->get($id);
if ( $article )
{
$article->result_source($c->model("DB")->source
("article"));
}
else
{
$article = $c->model("DB::article")->find($id)
or die "RC_404: no such article";
$c->cache->set($id, $article);
}
No problem. But I really want to save the related object trips to the
DB too. So, since articles may have comments I want to prefetch them
as they will be used beneath an article. But then they are missing
their result_source and resetting it does not seem to work(?). The
following code produces an error when reloaded after first being cached.
my $article = $c->cache->get($id);
if ( $article )
{
$article->result_source($c->model("DB")->source
("article"));
$_->result_source($c->model("DB")->source("comment"))
for $article->comments();
}
else
{
$article = $c->model("DB::article")
->find($id,
{ prefetch => [qw/comments/] }
)
or die "RC_404: no such article";
$c->cache->set($id, $article);
}
Caught exception in MyApp::Controller::Article->article "Can't call
method
+ "source" on an undefined value at
+ /Library/Perl/5.8.6/DBIx/Class/ResultSourceHandle.pm line
+ 62."
What am I doing wrong? Can this be handled at all (resetting
result_sources gets ugly fast when you bring in the other involved
objects like user and document lineage)? What is the best practice
regarding caching DBIC results in Catalyst?
Thanks!
-Ashley
More information about the Catalyst
mailing list