CHI 0.2 - Subcaches

jonswar on 2009-04-26T04:19:03

I've just released CHI 0.2. The main visible change is that multi-level caches have been fleshed out and made easier to use.

There are two kinds of multi-level relationships that I wanted to be able to express easily with CHI:

  • L1 (level 1) cache: Sits in front of the primary cache in order faster access for commonly accessed cache entries. i.e. a cache for your cache.
  • Mirror cache: Sits behind the primary cache and, over time, mirrors its contents. Useful for migrating from one cache to another without a sudden performance hit.

Initially CHI had a Multilevel driver that would let you place two or more caches inside a container cache object. The problem was that adding an L1 to an existing cache required changing it to a Multilevel cache, causing existing driver-specific calls to fail. (e.g. If I change a File cache to a Multilevel cache, File-specific methods will no longer get handled right.)

In 0.2 I switched to a primary cache / subcache model, which seems more appropriate. Now the File cache has an L1 subcache, and File-specific methods (as well as many ancillary methods on which the L1 relationship has no clear meaning) simply go to the primary cache.

The usage is also simpler. Here we place an in-process Memory cache in front a Memcached cache:

    my $cache = CHI->new(
        driver   => 'Memcached',
        servers  => [ "10.0.0.15:11211", "10.0.0.15:11212" ],
        l1_cache => { driver => 'Memory' }
    );

Note that there isn't a way yet to specify a size limit for the memory cache, which would make this a lot more self-maintaining. :) That's coming soon. In the meantime, I'm planning to use this for an unlimited request-based cache, clearing it manually at the end of each web request:

    $cache->l1_cache->clear();

Here we prepare to migrate from an old to a new cache directory:

    my $cache = CHI->new(
        driver          => 'File',
        root_dir        => '/old/cache/root',
        mirror_cache => { driver => 'File', root_dir => '/new/cache/root' },
    );

We leave this running for a few hours (or as needed), then replace it with

    my $cache = CHI->new(
        driver   => 'File',
        root_dir => '/new/cache/root'
    );

More details in the Subcaches section of the CHI 0.2 documentation.