Search This Blog

Saturday 7 February 2015

A note to myself on Caching

In Perl, a good module to use is CHI, a wrapper around specific data storage. This means there is one API to access the cache and the particular data store is hidden from the application.
  • Local system

  •     use CHI;
    
        # Choose a driver
        
        my $cache = CHI->new( driver     => 'FastMmap',
                              root_dir   => '/path/to/cache',
                              cache_size => '1k'
        );
        my $cache = CHI->new( driver  => 'Memcached::libmemcached',
                              servers => [ "10.0.0.15:11211",
                                           "10.0.0.15:11212" ],
                              l1_cache => { driver   => 'FastMmap'
                                            root_dir => '/path/to/cache' }
        );
        my $cache = CHI->new( driver   => 'BerkeleyDB',
                              root_dir => '/path/to/cache'
        );
        my $cache = CHI->new( driver => 'Memory',    global => 1 );
        my $cache = CHI->new( driver => 'RawMemory', global => 1 );


    CHI::Driver::FastMmap          Shared memory inter-process cache via mmaped files
    CHI::Driver::BerkeleyDB        Shared memory inter-process cache via Berkeley DB
                                                      environment using the Concurrent Data Store (CDS), making it
                                                      safe for multiple processes to read and write the cache without
                                                      explicit locking
    CHI::Driver::Memory               In-process memory based cache
    CHI::Driver::RawMemory        In-process memory based cache that stores references
                                                      directly instead of serializing/deep-copying)
  • Distributed systems

  •     use CHI;
    
        my $cache = CHI->new(
            driver => 'Memcached',   # or 'Memcached::Fast', 
                                       or 'Memcached::libmemcached'
            namespace => 'products',
            servers => [ "10.0.0.15:11211", "10.0.0.15:11212"
                         "/var/sock/memcached",
                         "10.0.0.17:11211"
                         [ "10.0.0.17:11211", 3
                       ],
            debug => 0,
            compress_threshold => 10_000,
        );

Code examples taken from: CHI module on CPAN

No comments: