|
|
Cache - the Cache interface
The Cache modules are designed to assist a developer in persisting data for a specified period of time. Often these modules are used in web applications to store data locally to save repeated and redundant expensive calls to remote machines or databases.
The Cache interface is implemented by derived classes that store cached data in different manners (such as as files on a filesystem, or in memory).
To use the Cache system, a cache implementation must be chosen to suit your needs. The most common is Cache::File, which is suitable for sharing data between multiple invocations and even between concurrent processes.
Using a cache is simple. Here is some very simple sample code for instantiating and using a file system based cache.
use Cache::File;
my $cache = Cache::File->new( cache_root => '/tmp/cacheroot' ); my $customer = $cache->get( $name );
unless ($customer) { $customer = get_customer_from_db( $name ); $cache->set( $name, $customer, '10 minutes' ); }
return $customer;
Of course, far more powerful methods are available for accessing cached data. Also see the TIE INTERFACE below.
Return a 'Cache::Entry' object for the given key. This object can then be used to manipulate the cache entry in various ways. The key can be any scalar string that will uniquely identify an entry in the cache.
purge()
Remove all expired data from the cache.
clear()
Remove all entries from the cache - regardless of their expiry time.
count()
Returns the number of entries in the cache.
size()
Returns the size (in bytes) of the cache.
When a cache is constructed these properties can be supplied as options to the
new()
method.
The current default expiry time for new entries into the cache. This property can also be reset at any time.
my $time = $c->default_expires(); $c->set_default_expires( $expiry );
The removal strategy object for the cache. This is used to remove object from the cache in order to maintain the cache size limit.
When setting the removal strategy in new(), the name of a strategy package or
a blessed strategy object reference should be provided (in the former case an
object is constructed by calling the new()
method of the named package).
The strategies 'Cache::RemovalStrategy::LRU' and 'Cache::RemovalStrategy::FIFO' are available by default.
my $strategy = $c->removal_strategy();
The size limit for the cache.
my $limit = $c->size_limit();
The load callback for the cache. This may be set to a function that will get called anytime a 'get' is issued for data that does not exist in the cache.
my $limit = $c->load_callback(); $c->set_load_callback($callback_func);
The validate callback for the cache. This may be set to a function that will get called anytime a 'get' is issued for data that does not exist in the cache.
my $limit = $c->validate_callback(); $c->set_validate_callback($callback_func);
These methods all have counterparts in the Cache::Entry package, but are provided here as shortcuts. They all default to just wrappers that do '$c->entry($key)->method_name()'. For documentation, please refer to Cache::Entry.
tie %hash, 'Cache::File', { cache_root => $tempdir };
$hash{'key'} = 'some data'; $data = $hash{'key'};
The Cache classes can be used via the tie interface, as shown in the synopsis. This allows the cache to be accessed via a hash. All the standard methods for accessing the hash are supported , with the exception of the 'keys' or 'each' call.
The tie interface is especially useful with the load_callback to automatically populate the hash.
These methods are only for use internally (by concrete Cache implementations).
These methods define the interface by which the removal strategy object can manipulate the cache (the Cache is the 'context' of the strategy). By default, methods need to be provided to remove the oldest or stalest objects in the cache - thus allowing support for the default FIFO and LRU removal strategies. All derived Cache implementations should support these methods and may also introduce additional methods (and additional removal strategies to match).
remove_oldest()
Removes the oldest entry in the cache and returns its size.
remove_stalest()
Removes the 'stalest' (least used) object in the cache and returns its size.
This method isn't actually part of the strategy interface, nor does it need to be defined by Cache implementations. Instead it should be called by implementations whenever the size of the cache increases. It will take care of checking the size limit and invoking the removal strategy if required. The size argument should be the new size of the cache.
These methods are only for use internally (by concrete Cache implementations).
Converts a timespec as described for Cache::Entry::set_expiry() into a unix time.
Cache::Entry, Cache::File, Cache::RemovalStrategy
The Cache modules are a total redesign and reimplementation of Cache::Cache and thus not directly compatible. It would be, however, quite possible to write a wrapper module that provides an identical interface to Cache::Cache.
The semantics of use are very similar to Cache::Cache, with the following exceptions:
Chris Leishman <chris@leishman.org> Based on work by DeWitt Clinton <dewitt@unto.net>
Copyright (C) 2003-2006 Chris Leishman. All Rights Reserved.
This module is distributed on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. This program is free software; you can redistribute or modify it under the same terms as Perl itself.
$Id: Cache.pm,v 1.7 2006/01/31 15:23:58 caleishm Exp $