[Dbix-class] Caching a resultset?

Peter Rabbitson rabbit+dbic at rabbit.us
Mon Jan 14 18:18:06 GMT 2013


On Mon, Jan 14, 2013 at 11:07:34AM -0500, Jesse Sheidlower wrote:
> On Tue, Jan 15, 2013 at 01:30:55AM +1100, Peter Rabbitson wrote:
> > On Sun, Jan 13, 2013 at 11:35:06PM -0500, Jesse Sheidlower wrote:
> > > 
> > > I have a Catalyst app that very frequently (pretty much every request)
> > > requires several DBIC resultsets that return a small number of values,
> > > that very rarely change. I'm trying to cache this, so I can update the
> > > cache when the values change and not have to hit my DB a half-dozen
> > > times on every request for data that is effectively static.
> > > 
> > > Originally the relevant line was along the lines of:
> > > 
> > >   $c->stash->{subjects} = $c->model('WordsDB::Subject')->search();
> > > 
> > > I replaced this, following the C::P::Cache docs, with 
> > > 
> > >   unless ( $c->stash->{subjects} = $cache->get( 'subjects' ) ) {
> > >     $c->stash->{subjects} = 
> > >         $c->model('WordsDB::Subject')->search();
> > >     $cache->set( 'subject', $c->stash->{subjects} );
> > >   }
> > > 
> > > However, this dies (on a second run, when it's actually hitting the
> > > cache) with "undef error - Can't call method "select" on an undefined
> > > value at /usr/share/perl5/DBIx/Class/ResultSet.pm line 957". So I'm
> > > assuming that I can't just stuff a RS into the cache and expect it to
> > > work. Is there an easy way around it? (I know I could retrieve the
> > > actual data and put that into the cache, but then I'd have to rewrite a
> > > whole bunch of templates, that are expecting a resultset.)
> > > 
> > > Thanks.
> > 
> > So apart from the mystery around the nonsensical exception - does this help?
> > 
> > local $DBIx::Class::ResultSourceHandle::thaw_schema = $c->model(...)->schema;
> 
> Yes! Putting that before the "unless" seems to make everything work
> correctly.... Will be testing further this afternoon. Thanks!

Please let me know if wider testing is succesful.

> 
> Does this need to be documented somewhere, or does something need to be
> fixed, or...?

Please see [1]. There are really two problems:

1) the exception thrown was supposed to be [2]. This part is a bug, and 
I can't yet figure out what is causing it. The test at [3] is nearly 
identical, and behaves correctly. Any thoughts on why yours failed 
(perhaps a debugger walk) would be more than welcome.

2) The docs suck (again as per [1]) and would welcome imrpovements. Also 
adding stuff to C::P::Cache won't hurt either.

Cheers

[1] http://lists.scsys.co.uk/pipermail/dbix-class/2013-January/011040.html
[2] https://metacpan.org/source/GETTY/DBIx-Class-0.08204/lib/DBIx/Class/ResultSource.pm#L1132
[3] https://metacpan.org/source/GETTY/DBIx-Class-0.08204/t/84serialize.t#L199





More information about the DBIx-Class mailing list