CHI: Cache Interface for Perl

jonswar on 2008-01-23T23:57:50

CHI, a module I've been working on for a few months, has made it to CPAN:

   file: $CPAN/authors/id/J/JS/JSWARTZ/CHI-0.03.tar.gz
   size: 62313 bytes
    md5: ec828f2466ba266e11cd6d1dd5ca2913

CHI provides a unified caching API, designed to assist a developer in persisting data for a specified period of time. It is intended as an evolution of DeWitt Clinton's Cache::Cache package, adhering to the basic Cache API but adding new features and addressing limitations in the Cache::Cache implementation.

You might think of it as a fledgling "DBI for caching".

Driver classes already exist for in-process memory, plain files, memory mapped files and memcached. Other drivers such as BerkeleyDB and DBI will be coming soon. Fortunately, implementing drivers is fairly easy, on the order of creating a TIE interface to your data store.

Special thanks to the Hearst Digital Media group, where CHI was first designed and developed, for blessing the open source release of this code.

There's lots more in store for this module, so stay tuned! Feedback welcome here or on the Perl cache mailing list.


Nice

stu42j on 2008-01-24T19:17:51

This looks like a cool project. I particularly like the Multilevel idea.

I've been wanting to add a caching layer to my app but one thing I've been pondering on is handling different objects in the same cache.

I know the common solution is to prepend the object name to the key but it would be nice if this was abstracted by the cache layer itself. Perhaps an optional objectspace parameter could be added to the get/set methods? Or how about something like:

my $cache = CHI->new( driver => 'Memory',
    objectspaces => [ 'customer', 'user', 'product' ] );
 
my $customer = $cache->get_customer($name);
$cache->set_customer( $name, $customer, "10 minutes" );
my $user = $cache->get_user($name);
Is that too crazy?

Re:Nice

jonswar on 2008-01-26T12:59:16

There is already support for multiple namespaces, but I take it that's not what you're looking for.

I've been considering adding support for complex keys that are automatically serialized, so you could say:

my $customer = $cache->get({customer => $name}); $cache->set({customer => $name}, $customer, "10 minutes" ); my $user = $cache->get({user => $name});

which is maybe a little cleaner than prepending onto a string...

Re:Nice

jonswar on 2008-01-26T13:01:19

Sorry, that should have read:

my $customer = $cache->get({customer => $name});
$cache->set({customer => $name}, $customer, "10 minutes" );
my $user = $cache->get({user => $name});

Re:Nice

stu42j on 2008-01-26T18:34:31

Right, I didn't want to use namespaces since that is set per cache object.

I like the complex key idea, though.