Tie-Cacher
view release on metacpan or search on metacpan
$exists = $cache->exists($key);
$cache->delete(@keys);
$value = $cache->delete(@keys);
@values = $cache->delete(@keys);
$cache->clear;
$nr_keys = $cache->count;
$hit = $cache->hit;
$old_hit = $cache->hit($new_hit);
$missed = $cache->missed;
$old_missed = $cache->missed($new_missed);
$max_count = $cache->max_count;
$old_max_count = $cache->max_count($new_max_count);
$validate = $cache->validate;
$old_validate = $cache->validate($new_validate);
$load = $cache->load;
$old_load = $cache->load($new_load);
$save = $cache->save;
$old_save = $cache->save($new_save);
$user_data = $cache->user_data;
$old_user_data = $cache->user_data($new_user_data);
# The Tie interface:
use Tie::Cacher;
$tied = tie %cache, 'Tie::Cache', $max_count;
$tied = tie %cache, 'Tie::Cache', %options;
$tied = tie %cache, 'Tie::Cache', {%options};
# cache supports normal tied hash functions
$cache{1} = 2; # STORE
print "$cache{1}\n"; # FETCH
print "Yes\n" if exists $cache{1}; # EXISTS
@keys = keys %cache; # KEYS
# FIRSTKEY, NEXTKEY
while(($k, $v) = each %cache) { print "$k: $v\n"; }
delete $cache{1}; # DELETE
%cache = (); # CLEAR
# Or use the OO methods on the underlying tied object:
print $tied->max_count, "\n";
=head1 DESCRIPTION
This module implements a least recently used (LRU) cache in memory through
a tie and a OO interface. Any time a key/value pair is fetched or stored,
an entry time is associated with it, and as the cache fills up, those members
of the cache that are the oldest are removed to make room for new entries.
So, the cache only "remembers" the last written entries, up to the
size of the cache. This can be especially useful if you access
great amounts of data, but only access a minority of the data a
majority of the time.
The implementation is a hash, for quick lookups, overlaying a doubly linked
list for quick insertion and deletion. Notice that the OO interface will
be faster than the tie interface.
=head2 EXPORT
None
=head2 METHODS
Notice that in the methods you will see a number of places where a
node is returned where you might have expected a value or a reference to
a value. This node is an array reference that actually has the value
at index 0, followed by a few internal fields. You are however free to
put extra associated data in this array after them or even do things like
bless the array. This gives you an easy way to decorate values.
=over
=item X<new>$cache = Tie::Cacher->new($max_count)
=item $cache = Tie::Cacher->new(%options)
=item $cache = Tie::Cacher->new(\%options)
Creates a new Tie::Cache object. Will throw an exception on failure
(the only possible failures are invalid arguments).
Options are name value pairs, where the following are currently recognized:
=over
=item X<option_validate>validate => \&code
If this option is given, whenever a data L<fetch|"fetch"> or
L<fetch_node|"fetch_node"> is done (notice that when using the tied interface,
a list context L<each|perlfunc/"each"> implies a L<fetch|"fetch"> for the
value) and the requested key already exists, the given code is called like:
$validate->($cache, $key, $node)
where $node is an internal array reference. You can access the current value
corresponding to the key as $node->[0]. The code should either return true,
indicating the value is still valid, or false, meaning it's not valid anymore.
In the invalid case the current entry gets removed and the fetch proceeds
as if the key had not been found (which includes a possible
L<load|"option_load">).
In the valid case, you may in fact change the value through $node->[0] before
returning. (That way you can save a call to L<load|"option_load">, but there
won't be any implied L<save|"option_save"> call, so you'll have to do that
yourself if you want it).
If the code dies, the exception will be passed to the application, but
The L<hit|"hit"> counter will have been increased, the key will still be in
the cache and will have been marked as recently used.
=item X<option_load>load => \&load
If this option is given, whenever a data L<fetch|"fetch"> or
( run in 1.158 second using v1.01-cache-2.11-cpan-800906f7e73 )