[Dbix-class] Caching a resultset?

Jesse Sheidlower jester at panix.com
Mon Jan 14 12:20:26 GMT 2013


On Mon, Jan 14, 2013 at 11:06:56PM +1100, Peter Rabbitson wrote:
> On Mon, Jan 14, 2013 at 06:30:42AM -0500, Jesse Sheidlower wrote:
> > On Mon, Jan 14, 2013 at 10:13:44PM +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} );
> > > >   }
> 
> What is $cache being serialized via? Storable? Or some other scheme?

I'm using Cache::FastMmap, which uses Storable (unless you flip a switch
to have it store values as raw binary data; I didn't flip this switch).

> > > > 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".
> 
> What is the exact code from get()ing the cache value to the error (or 
> what is the smallest piece of code you can reduce it to)?

It's in a Catalyst context, so there's a ton of stuff in between, but
the core is that I put the RS into the Catalyst stash as indicated
above, and then in a template I have:

    [% WHILE ( subject = subjects.next ) %]
       <option value="[% subject.id %]">[% subject %]</option>
    [% END %]

Thanks.



More information about the DBIx-Class mailing list