[Dbix-class] relationships and caching

Matt S Trout dbix-class at trout.me.uk
Tue Apr 18 17:05:39 CEST 2006


Michael Kang wrote:
> Hi -
> 
> A question about caching and esp. for relationships:
> 
> Let's say I have a data model with a one-to-many relationship between  
> Folders and Items:
> 
> Folder->has_many ( items )
> Item->has_a ( folder )
> 
> I've tried implementing a simple caching layer by overloading the  
> create/find/update/delete methods, but this only works for simple row  
> retrievals, since the column data is stored in the object. But for  
> relationships, the SQL is generated and executed on the fly... anyone  
> have an idea of a good way to make it work in this case?
> 
> For example, when adding an item to a folder, I'd insert the row, but  
> I'd also want the folder object in the cache to "know" about the new  
> item, without having to hit the database again.

Declare items with 'cache => 1' to enable the caching that already exists.

Then tweak add_to_folders to do something like

my $rs = $self->related_resultset('items')
$rs->set_cache([ @{$rs->get_cache}, $new_item ]);

- except you're also going to need to worry about maintaining order etc. And 
catching all the other different ways said $new_item could get created. 
Whatever you do, make sure you document clearly which methods can be used 
while maintaining consistency.

Tuning your database's own query cache will probably turn out to be simpler ...

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list