Does DBIx::Class have transparent caching?
Asked Answered
O

4

9

In the C#/.Net world, there are ORMs such as NHibernate or ActiveRecord that includes transparent caching: database updates are transparently replicated to the cache, objects are retrieved directly from the cache when available, etc (often with memcached).

It doesn't look like transparent caching is available in Perl with DBIx::Class. Did I miss something? That seems like a common need, I'm surprised I couldn't find anything on it on CPAN or Google.

Optimal answered 12/8, 2009 at 18:4 Comment(1)
You get a lot of hits for google.com/search?q=DBIx%3A%3AClass+cachingActinium
S
6

Semi-transparently there is DBIx::Class::Cursor::Cached (from mst, like DBIC). You need to provide a Cache object to your connections or schema objects though. Seems very undocumented unfortunately.

The Cookbook does have an example for using Tie::Cache on DBIC, and there are also the (get|set|clear)_cache functions on DBIx::Class::ResultSet, but they are probably not exactly what you need.

Sawtelle answered 12/8, 2009 at 19:4 Comment(2)
From what I understood, this is caching for a very limited use case. if you do a search, and go over the items in your search several times, you can cache the result set. But you can not cache individual objects. I've tested it, in my case it does not help at all (I actually get worse performances because I cache the search result, but use it once only)Optimal
Actually, it could be a solution. The problem is with relationships: the main resultset is cached, but not the relationships.Optimal
M
6

Here's a simple way that you could add caching with CHI. I haven't actually tried this, so there may be pitfalls I haven't considered, especially with regard to the serialization of DBIC result sets.

package My::Table;
use strict; 
use warnings;

use base 'DBIx::Class';

use Storable 'freeze';
use CHI;

$Storable::canonical = 1;

__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('mytable');

# ....

my $CACHE = CHI->new( driver => 'Memory' );

sub search { 
    my $self = shift;

    my $key = freeze( \@_ );      # make cache key from params
    if ( my $rs = $CACHE->get( $key ) ) { 
        return $rs;
    }

    # Note: there are issues with context propagation here
    my $rs = $self->next::method( @_ );
    $CACHE->set( $key => $rs );
    return $rs;
}

sub update { 
    my $self = shift;

    my @keys = $self->find_all_cache_items_affected_by_this_update( @_ );
    $CACHE->remove( $_ ) for @keys;

    $self->next::method( @_ );
}

It's a bit clunky, but I think it's a good starting point. If you do this type of thing in a base class for all your DBIx::Class table classes, you should be able to build in transparent caching pretty easily.

Meacham answered 12/8, 2009 at 19:30 Comment(2)
Thanks, it looks like the way to go. I'm using Catalyst with Catalyst::Model::DBIC::Schema and I count not find the right place to override the search method for example: I tried in the DBIx::lass, and in DBIx::Class::Schema, but using $c->model(''')->search does not use the new search methodOptimal
Just in case: search is a result set method and that's where you need to override it. E.g. override ResultSet::Foo->search, or simply set a default resultset class when loading the schema, and extend that in all your customized resultset classes.Chevron
D
1

I have come across this same need with my DBIx::Class based model, and after reviewing the answers here I don't really see anything that is the solution I'm looking for. After struggling with this issue, I'm starting to think that my business layer should handle the caching, so that I treat DBIx::Class as a persistence layer that does not implement business logic.

For example, my current code with ideal caching would be something like this:

my $network = SL::Model::App->resultset('Network')->search({ ip => '127.0.0.1' });

And the $network object is served from the $memcached cache that I configured during DBIx::Class schema initialization

The new code would be:

my $network = SL::Network->find_by_ip_or_create({ ip => '127.0.0.1' });

Meanwhile, in a nearby module:

package SL::Network;
...
use SL::Model::App;
use SL::Cache;

our $cache = SL::Cache->new;

sub find_by_ip_or_create {
    my ($class, $args) = @_;

    my $network;
    unless ($network = $cache->get('network|' . $args->{ip}) {
        $network = SL::Model::App->resultset('Network')->find_or_create({ wan_ip => $args->{ip}});
        $cache->set('network|' . $args->{ip} => DBIx::Class::Schema->freeze($network));
    }
    return $network;

}

You get the idea.

Drugi answered 5/11, 2010 at 6:13 Comment(0)
C
0

I would like to add that, instead of adding a 'search' method in My::Table,

one can also enhance the ->search method provided by DBIx::Class::ResultSet, like so:

package Schema::ResultSet::My::Table;
use base 'DBIx::Class::ResultSet';

sub search {
    my ( $self, $args ) = ( shift, shift );

    # do what you want here with the args passed to ->search
    return $self->next::method( $args, @_ );
}

Also, you can very likely subclass ResultSet so you can provide this altered ( cached ) search to all ResultSets, thus keeping the caching code in one place for all tables, which would be a lot less messy imho.

This I have not tested yet, though.

To make the above example work, put it in a file with the name of your schema class, in the directory "../Schema/ResultSet/", and make sure your Schema.pm contains "load_namespaces();" which will nicely auto-load all your overloaded classes that you put there ( I think my Catalyst install did that automatically, but I do not recall ).

DBIx::Class::ResultSet

Chevron answered 31/10, 2014 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.