Tie::Cacher is a Perl module to cache a (sub)set of key/value pairs. Tie and OO interface.
SYNOPSIS
# The Object Oriented interface:
use Tie::Cacher;
$cache = Tie::Cacher->new($max_count);
$cache = Tie::Cacher->new(%options);
$cache = Tie::Cacher->new(%options);
$cache->store($key, $value);
$value = $cache->fetch($key);
$node = $cache->fetch_node($key);
$nr_keys = $cache->keys;
@keys = $cache->keys;
@keys = $cache->recent_keys;
@keys = $cache->old_keys;
$key = $cache->most_recent_key;
$key = $cache->oldest_key;
$key = $cache->first_key;
($key, $value) = $cache->first_key;
$key = $cache->next_key;
($key, $value) = $cache->next_key;
$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 Êche, 'Tie::Cache', $max_count;
$tied = tie Êche, 'Tie::Cache', %options;
$tied = tie Êche, 'Tie::Cache', {%options};
# cache supports normal tied hash functions
$cache{1} = 2; # STORE
print "$cache{1}n"; # FETCH
print "Yesn" if exists $cache{1}; # EXISTS
@keys = keys Êche; # KEYS
# FIRSTKEY, NEXTKEY
while(($k, $v) = each Êche) { print "$k: $vn"; }
delete $cache{1}; # DELETE
Êche = (); # CLEAR
# Or use the OO methods on the underlying tied object:
print $tied->max_count, "n";
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.
Product's homepage
Requirements:
· Perl