NAME

Cache::FastMmap - Uses an mmap'ed file to act as a shared memory interprocess cache

SYNOPSIS

use Cache::FastMmap;

# Uses vaguely sane defaults
$Cache = Cache::FastMmap->new();

# $Value must be a reference...
$Cache->set($Key, $Value);
$Value = $Cache->get($Key);

$Cache = Cache::FastMmap->new(raw_values => 1);

# $Value can't be a reference...
$Cache->set($Key, $Value);
$Value = $Cache->get($Key);

ABSTRACT

A shared memory cache through an mmap'ed file. It's core is written in C for performance. It uses fcntl locking to ensure multiple processes can safely access the cache at the same time. It uses a basic LRU algorithm to keep the most used entries in the cache.

DESCRIPTION

In multi-process environments (eg mod_perl, forking daemons, etc), it's common to want to cache information, but have that cache shared between processes. Many solutions already exist, and may suit your situation better:

  • MLDBM::Sync - acts as a database, data is not automatically expired, slow

  • IPC::MM - hash implementation is broken, data is not automatically expired, slow

  • Cache::FileCache - lots of features, slow

  • Cache::SharedMemoryCache - lots of features, VERY slow. Uses IPC::ShareLite which freeze/thaws ALL data at each read/write

  • DBI - use your favourite RDBMS. can perform well, need a DB server running. very global. socket connection latency

  • Cache::Mmap - similar to this module, in pure perl. slows down with larger pages

  • BerkeleyDB - very fast (data ends up mostly in shared memory cache) but acts as a database overall, so data is not automatically expired

In the case I was working on, I needed:

  • Automatic expiry and space management

  • Very fast access to lots of small items

  • The ability to fetch/store many items in one go

Which is why I developed this module. It tries to be quite efficient through a number of means:

  • Core code is written in C for performance

  • It uses multiple pages within a file, and uses Fcntl to only lock a page at a time to reduce contention when multiple processes access the cache.

  • It uses a dual level hashing system (hash to find page, then hash within each page to find a slot) to make most get() calls O(1) and fast

  • On each set(), if there are slots and page space available, only the slot has to be updated and the data written at the end of the used data space. If either runs out, a re-organisation of the page is performed to create new slots/space which is done in an efficient way

The class also supports read-through, and write-back or write-through callbacks to access the real data if it's not in the cache, meaning that code like this:

my $Value = $Cache->get($Key);
if (!defined $Value) {
  $Value = $RealDataSource->get($Key);
  $Cache->set($Key, $Value)
}

Isn't required, you instead specify in the constructor:

Cache::FastMmap->new(
  ...
  context => $RealDataSourceHandle,
  read_cb => sub { $_[0]->get($_[1]) },
  write_cb => sub { $_[0]->set($_[1], $_[2]) },
);

And then:

my $Value = $Cache->get($Key);

$Cache->set($Key, $NewValue);

Will just work and will be read/written to the underlying data source as needed automatically.

PERFORMANCE

If you're storing relatively large and complex structures into the cache, then you're limited by the speed of the Storable module. If you're storing simple structures, or raw data, then Cache::FastMmap has noticeable performance improvements.

See http://cpan.robm.fastmail.fm/cache_perf.html for some comparisons to other modules.

COMPATIABILITY

Cache::FastMmap uses mmap to map a file as the shared cache space, and fcntl to do page locking. This means it should work on most UNIX like operating systems, but will not work on Windows or Win32 like environments.

MEMORY SIZE

Because Cache::FastMmap mmap's a shared file into your processes memory space, this can make each process look quite large, even though it's just mmap'd memory that's shared between all processes that use the cache, and may even be swapped out if the cache is getting low usage.

However, the OS will think your process is quite large, which might mean you hit some BSD::Resource or 'ulimits' you set previously that you thought were sane, but aren't anymore, so be aware.

USAGE

Because the cache uses shared memory through an mmap'd file, you have to make sure each process connects up to the file. There's probably two main ways to do this:

  • Create the cache in the parent process, and then when it forks, each child will inherit the same file descriptor, mmap'ed memory, etc and just work.

  • Explicitly connect up in each forked child to the share file

The first way is usually the easiest. If you're using the cache in a Net::Server based module, you'll want to open the cache in the pre_loop_hook, because that's executed before the fork, but after the process ownership has changed and any chroot has been done.

In mod_perl, just open the cache at the global level in the appropriate module, which is executed as the server is starting and before it starts forking children, but you'll probably want to chmod or chown the file to the permissions of the apache process.

METHODS

new(%Opts)

Create a new Cache::FastMmap object.

Basic global parameters are:

  • share_file

    File to mmap for sharing of data (default: /tmp/sharefile)

  • init_file

    Clear any existing values and re-initialise file. Useful to do in a parent that forks off children to ensure that file is empty at the start (default: 0)

    Note: This is quite important to do in the parent to ensure a consistent file structure. The shared file is not perfectly transaction safe, and so if a child is killed at the wrong instant, it might leave the the cache file in an inconsistent state.

  • raw_values

    Store values as raw binary data rather than using Storable to free/thaw data structures (default: 0)

  • expire_time

    Maximum time to hold values in the cache in seconds. A value of 0 means does no explicit expiry time, and values are expired only based on LRU usage. Can be expressed as 1m, 1h, 1d for minutes/hours/days respectively. (default: 0)

You may specify the cache size as:

  • cache_size

    Size of cache. Can be expresses as 1k, 1m for kilobytes or megabytes respectively. Automatically guesses page size/page count values.

Or specify explicit page size/page count values. If none of these are specified, the values page_size = 64k and num_pages = 89 are used.

  • page_size

    Size of each page. Must be a power of 2 between 4k and 1024k. If not, is rounded to the nearest value.

  • num_pages

    Number of pages. Should be a prime number for best hashing

The cache allows the use of callbacks for reading/writing data to an underlying data store.

  • context

    Opaque reference passed as the first parameter to any callback function if specified

  • read_cb

    Callback to read data from the underlying data store. Called as:

    $read_cb->($context, $Key)

    Should return the value to use. This value will be saved in the cache for future retrievals. Return undef if there is no value for the given key

  • write_cb

    Callback to write data to the underlying data store. Called as:

    $write_cb->($context, $Key, $Value, $ExpiryTime)

    In 'write_through' mode, it's always called as soon as a set(...) is called on the Cache::FastMmap class. In 'write_back' mode, it's called when a value is expunged from the cache if it's been changed by a set(...) rather than read from the underlying store with the read_cb above.

    Note: Expired items do result in the write_cb being called if 'write_back' caching is enabled and the item has been changed. You can check the $ExpiryTime against time() if you only want to write back values which aren't expired.

    Also remember that write_cb may be called in a different process to the one that placed the data in the cache in the first place

  • delete_cb

    Callback to delete data from the underlying data store. Called as:

    $delete_cb->($context, $Key)

    Called as soon as remove(...) is called on the Cache::FastMmap class

  • cache_not_found

    If set to true, then if the read_cb is called and it returns undef to say nothing was found, then that information is stored in the cache, so that next time a get(...) is called on that key, undef is returned immediately rather than again calling the read_cb

  • write_action

    Either 'write_back' or 'write_through'. (default: write_through)

  • empty_on_exit

    When you have 'write_back' mode enabled, then you really want to make sure all values from the cache are expunged when your program exits so any changes are written back. This is a bit tricky, because we don't know if you're in a child, so you must ensure that the parent process either explicitly calls empty() or that this flag is set to true when the parent connects to the cache, and false in all the children

get($Key, [ \%Options ])

Search cache for given Key. Returns undef if not found. If read_cb specified and not found, calls the callback to try and find the value for the key, and if found (or 'cache_not_found' is set), stores it into the cache and returns the found value.

%Options is optional, and is used by get_and_set() to control the locking behaviour. For now, you should probably ignore it unless you read the code to understand how it works

set($Key, $Value, [ \%Options ])

Store specified key/value pair into cache

%Options is optional, and is used by get_and_set() to control the locking behaviour. For now, you should probably ignore it unless you read the code to understand how it works

get_and_set($Key, $Sub)

Atomically retrieve and set the value of a Key.

The page is locked while retrieving the $Key and is unlocked only after the value is set, thus guaranteeing the value does not change betwen the get and set operations.

$Sub is a reference to a subroutine that is called to calculate the new value to store. $Sub gets $Key and the current value as parameters, and should return the new value to set in the cache for the given $Key.

For example, to atomically increment a value in the cache, you can just use:

$Cache->get_and_set($Key, sub { return ++$_[1]; });

The return value from this function is the new value stored back into the cache.

Notes:

  • Do not perform any get/set operations from the callback sub, as these operations lock the page and you may end up with a dead lock!

  • Make sure your sub does not die/throw an exception, otherwise the unlocking code will be skipped. You can protect yourself by wrapping everything in your sub in an eval { }

remove($Key)

Delete the given key from the cache

clear()

Clear all items from the cache

Note: If you're using callbacks, this has no effect on items in the underlying data store. No delete callbacks are made

purge()

Clear all expired items from the cache

Note: If you're using callbacks, this has no effect on items in the underlying data store. No delete callbacks are made, and no write callbacks are made for the expired data

empty($OnlyExpired)

Empty all items from the cache, or if $OnlyExpired is true, only expired items.

Note: If 'write_back' mode is enabled, any changed items are written back to the underlying store. Expired items are written back to the underlying store as well.

get_keys($Mode)

Get a list of keys/values held in the cache. May immediately be out of date because of the shared access nature of the cache

If $Mode == 0, an array of keys is returned

If $Mode == 1, then an array of hashrefs, with 'key', 'last_access', 'expire_time' and 'flags' keys is returned

If $Mode == 2, then hashrefs also contain 'value' key

multi_get($PageKey, [ $Key1, $Key2, ... ])

The two multi_xxx routines act a bit differently to the other routines. With the multi_get, you pass a separate PageKey value and then multiple keys. The PageKey value is hashed, and that page locked. Then that page is searched for each key. It returns a hash ref of Key => Value items found in that page in the cache.

The main advantage of this is just a speed one, if you happen to need to search for a lot of items on each call.

For instance, say you have users and a bunch of pieces of separate information for each user. On a particular run, you need to retrieve a sub-set of that information for a user. You could do lots of get() calls, or you could use the 'username' as the page key, and just use one multi_get() and multi_set() call instead.

A couple of things to note:

  1. This makes multi_get()/multi_set() and get()/set() incompatiable. Don't mix calls to the two, because you won't find the data you're expecting

  2. The writeback and callback modes of operation do not work with multi_get()/multi_set(). Don't attempt to use them together.

multi_set($PageKey, { $Key1 = $Value1, $Key2 => $Value2, ... })>

Store specified key/value pair into cache

INTERNAL METHODS

_expunge_all($Mode, $WB)

Expunge all items from the cache

Expunged items (that have not expired) are written back to the underlying store if write_back is enabled

_expunge_page($Mode, $WB, $Len)

Expunge items from the current page to make space for $Len bytes key/value items

Expunged items (that have not expired) are written back to the underlying store if write_back is enabled

SEE ALSO

MLDBM::Sync, IPC::MM, Cache::FileCache, Cache::SharedMemoryCache, DBI, Cache::Mmap, BerkeleyDB

Latest news/details can also be found at:

http://cpan.robm.fastmail.fm/cachefastmmap/

AUTHOR

Rob Mueller <mailto:cpan@robm.fastmail.fm>

COPYRIGHT AND LICENSE

Copyright (C) 2003-2006 by FastMail IP Partners

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.