Re: [CDBI] Memcached

[email protected] (Randal L. Schwartz)
Newsgroups gmane.comp.lang.perl.modules.class-dbi
Message-ID <[email protected]>
>>>>> "Derek" == Derek Watson <[email protected]> writes:

Derek> Can you share your approach and experience? Did you overload
Derek> retrieve() and update()? Did it work as expected?

Sweet was passing data that Cache::FastMmap didn't like, so here's
the top of my database class:

    package INITECH::DB;
    use base Class::DBI::Sweet;
    use strict;

    BEGIN {
      package My::Cache;
      use base qw(Cache::FastMmap);

      sub set {
        my $self = shift;
        my $key = shift;
        my $value = shift;
        ## wrap value, pass it along
        $self->SUPER::set($key, \$value, @_);
      }

      sub get {
        my $self = shift;
        my $key = shift;
        my $value = $self->SUPER::get($key);
        $value = $$value if defined $value;
        return $value;
      }
    }

    BEGIN {
      use File::Temp qw(tempfile);

      our $long_cache = My::Cache->new
        (share_file => "/tmp/initech_db_$<",
         expire_time => 86400,
        ) or die "Cannot create long_cache";
      our ($per_hit_cache_fh, $per_hit_cache_filename) = tempfile();
      our $per_hit_cache = My::Cache->new
        (share_file => $per_hit_cache_filename, # unlinked on program exit
        ) or die "Cannot create per_hit_cache";
      __PACKAGE__->cache($per_hit_cache); # everybody is cached this much
      END { unlink $per_hit_cache_filename }
    }

    sub CACHE_ME {                  # cache me even between hits
      my $class = shift;
      ## warn "enabling cache on $class\n";
      die "value of long_cache is gone" unless our $long_cache;
      $class->cache($long_cache);
    }

    __PACKAGE__->connection("dbi:Pg:dbname=blah blah", $user, $pass);
    ...

Then each class that wanted to be in the "long" cache (basically,
read-only data) added:

  package INITECH::DB::SomeType;
  use base INITECH::DB;
  __PACKAGE__->CACHE_ME;

All other items were cached only per-hit.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.