Hello. This is what i use - example in attach.

And in your schema class you could override 'load_components' method to use
that resultset class for all of your sources:

sub load_classes {
   my $self = shift;
   my $ret = $self->SUPER::load_classes(@_);
   $self->source($_)->resultset_class('My::ResultSet')
       for $self->sources;
   return $ret;
}

Usage is simple:

my @objs = $rs->search({COND}, {ATTRS, memcache => 3600});
my $num = $rs->count({COND}, {memcache => 3600});

Cache is on only when memcache attr is present.

What is cached:
- $rs->all (aka search in list context)
- $rs->find
- $rs->single
- $rs->count

Iterators are not cached.




2007/5/8, Matt S Trout <[EMAIL PROTECTED]>:

On Thu, Apr 19, 2007 at 10:50:51AM -0700, Evaldas Imbrasas wrote:
> Hi there,
>
> Is anyone using memcached to cache DBIC objects? It would be ideal if
> there was a possibility to define memcached as a caching layer in DBIC
> and just it transparently.

I'd love to see this implemented but haven't had time to make it happen
myself yet. If you're interesting in getting your feet wet I can give you
a branch off -current to hack in and guide you through the design I had in
mind.

--
     Matt S Trout       Need help with your Catalyst or DBIx::Class
project?
  Technical Director    Want a managed development or deployment platform?
Shadowcat Systems Ltd.   Contact mst (at) shadowcatsystems.co.uk for a
quote

http://www.shadowcatsystems.co.uk/

_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive:
http://www.mail-archive.com/dbix-class@lists.rawmode.org/

package My::ResultSet;
use base qw/DBIx::Class::ResultSet/;
use strict;
use Storable;
use Digest::MD5;

__PACKAGE__->mk_classaccessors(qw/memd/);

sub single {
    my ($self, $where, $attrs) = @_;

    if ( my $cache_time = $attrs->{memcache} ||
         $self->_resolved_attrs->{memcache}  and
         ( my $memd ||= $self->memd )
    )
    {
        my $key = 'S-'.getCacheKey([$self->_resolved_attrs, $where, $attrs]);
        my $obj;
        return $obj if !$attrs->{expire} and $obj = $memd->get($key);
        $obj = $self->SUPER::single($where);
        $memd->set($key, $obj, $cache_time);
        return $obj;
    }

    return $self->SUPER::single($where);
}

sub all {
    my $self = shift;

    if ( my $cache_time = (my $attrs = $self->_resolved_attrs)->{memcache} and
         ( my $memd ||= $self->memd )
    )
    {
        my $key = 'A-'.getCacheKey($attrs);
        my $objs;
        return @$objs if !$attrs->{expire} and $objs = $memd->get($key);
        @$objs = $self->SUPER::all(@_);
        $memd->set($key, $objs, $cache_time);
        return @$objs;
    }

    return $self->SUPER::all(@_);
}

sub _count {
    my $self = shift;

    if ( my $cache_time = (my $attrs = $self->_resolved_attrs)->{memcache} and
         ( my $memd ||= $self->memd )
    )
    {
        my $key = 'C-'.getCacheKey($attrs);
        my $count;
        return $count if !$attrs->{expire} && defined ($count = 
$memd->get($key));
        $count = $self->SUPER::_count(@_);
        $memd->set($key, $count, $cache_time);
        return $count;
    }

    return $self->SUPER::_count(@_);
}

sub getCacheKey {
    local $Storable::canonical = 1;
    Digest::MD5::md5_hex(Storable::freeze($_[0]));
}

1;
_______________________________________________
List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
Wiki: http://dbix-class.shadowcatsystems.co.uk/
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
Searchable Archive: http://www.mail-archive.com/dbix-class@lists.rawmode.org/

Reply via email to