NAME
Tie::FileLRUCache - A lightweight but robust filesystem based persistent LRU cache
SYNOPSIS
OBJECT INTERFACE
use Tie::FileLRUCache;
my $cache = Tie::FileLRUCache->new({ -cache_dir => $directory, -keep_last => 100 });
# Inserting value into LRU cache using '-key'
$cache->update({ -key => $key, -value => $value });
# Inserting value into LRU cache using '-cache_key'
my $cache_key = $cache->make_cache_key({ -key => $key });
$cache->update({ -cache_key => $cache_key, -value => $value });
# Checking LRU cache
my ($in_cache,$value) = $cache->check({ -key => $key });
if ($in_cache) {
return $value;
}
# Not in cache - do something else
# Checking LRU cache with speed up hack for objects, hashes, arrays etc used as keys
my $cache_key = $cache->make_cache_key({ -key => $something });
my ($in_cache,$value) = $cache->check({ -cache_key => $cache_key });
if ($in_cache) {
return $value;
}
# Not in cache - do something else
# Deleting a key and its value from the cache
$cache->delete({ -key => $key });
# Clearing LRU cache
$cache->clear;
TIED INTERFACE
use Tie::FileLRUCache;
[$X =] tie %hash, 'Tie::FileLRUCache', $cache_dir, $keep_last_n;
# Adding a key/value to the cache
$hash{$key} = $value;
# Checking the cache
if (not exists $hash{$key}) {;
# No match
.
.
.
} else {
my $value = $hash{$key};
.
.
.
}
# Removing a value from the cache;
delete $hash{$key};
# Clearing the cache
%hash = ();
Note: Iteration over the cache (each, keys, values) is _NOT_
supported.
DESCRIPTION
Provides a persistent filesystem based LRU cache.
It uses the 'last accessed' timestamp generated by the file system to determine the 'oldest' cache entry and discards the oldest cache entries when needed to stay under the -keep_last limit.
If you store thing very fast (such that many entries receive the _same_ time stamp), it is essentially a coin toss which entry within a single timestamped second gets purged from the cache to make room for new ones.
OBJECT METHODS
new($parm_ref);
-
Creates and optionally initializes a Tie::FileLRUCache object:
Example:
my $cache = Tie::FileLRUCache->new({ -cache_dir => '/tmp/testing', -keep_last => 100, });
check($parm_ref);
-
Reads the cache for the key.
Returns two values: $cache_hit (true if a hit was found, false if not) $value (the cached value, undef if no hit)
Examples:
my ($cache_hit,$value) = $cache->check({ -key => $key }); my ($cache_hit,$value) = $cache->check({ -cache_key => $cache_key });
The '-key' form is used when you just want to use a raw key. It can use blessed objects, hash refs, scalars, or array refs as keys. The more complex structures take a speed penalty for computing a canonical form. You can minimize this penalty by using the '-cache_key' form instead.
The '-cache_key' form is used for performance reasons when using keys such as complex blessed objects or hashes as a key. The -cache_key is obtained with a call to 'make_cache_key'. It is legal to mix -cache_key and -key based calls - they are cross-compatible.
make_cache_key($parm_ref);
-
Generates a cache key by canonicalizing a passed key as a network ordered canonical Storable string.
Example:
my $cache_key = $cache->make_cache_key({ -key => $key });
update($parm_ref);
-
Updates the Least Recently Used cache for the specified key with the passed value. '-keep_last' is optional after the first access to a dataset. It will use the _most recent_ 'keep_last' used if not specified.
It is legal to use ordinary scalars, hash references, or array references as keys as well as objects as -keys or -values. Basically, anything that Storable can serialize can be used.
Examples:
$cache->update({ -key => $key, -value => $value }); $cache->update({ -key => $key, -value => $value, -keep_last => 100}); my $cache_key = $cache->make_cache_key({ -key => $key }); $cache->update({ -cache_key => $cache_key, -value => $value }); my $cache_key = $cache->make_cache_key({ -key => $key }); $cache->update({ -cache_key => $cache_key, -value => $value, -keep_last => 50 });
delete($parm_ref);
-
Forces the deletion of a specific key from the cache.
Example:
$cache->delete({ -key => $key });
COPYRIGHT
Copyright 1999, Benjamin Franz (<URL:http://www.nihongo.org/snowhare/>) and FreeRun Technologies, Inc. (<URL:http://www.freeruntech.com/>). All Rights Reserved. This software may be copied or redistributed under the same terms as Perl itelf.
AUTHOR
Benjamin Franz
TODO
Debugging.