Util::Medley::Cache - Simple caching mechanism.
version 0.060
my $cache = Util::Medley::Cache->new; # see ATTRIBUTES for options # # positional # $cache->set('unittest', 'test1', {foo => bar}); my $data = $cache->get('test', 'unittest'); my @keys = $cache->getKeys('unittest'); $cache->delete('unittest', 'test1'); # # named pair # $cache->set(ns => 'unittest', key => 'test1', data => { foo => 'bar' }); my $data = $cache->get(ns => 'unittest', key => 'test1'); my @keys = $cache->getKeys(ns => 'unittest'); $cache->delete(ns => 'unittest', key => 'test1');
This class provides a thin wrapper around CHI. The caching has 2 levels:
level 1 (memory)
level 2 (disk)
When fetching from the cache, level 1 (L1) is always checked first. If the requested object is not found, it searches the level 2 (L2) cache.
The cached data can be an object, reference, or string.
All methods confess on error.
Location of the L2 file cache.
$HOME/.util-medley/cache
Toggles caching on or off.
1
Sets the cache expiration.
0 (never)
Sets the cache namespace.
Toggles the L1 cache on or off.
To disable L1 cache, set MEDLEY_CACHE_L1_DISABLED=1.
Toggles the L2 cache on or off.
To disable L2 cache, set MEDLEY_CACHE_L2_DISABLED=1.
Clears all cache for a given namespace.
clear([$ns]) clear([ns => $ns])
The cache namespace.
Deletes a cache object.
delete($key, [$ns]) delete(key => $key, [ns => $ns])
Unique identifier of the cache object.
Deletes L1 cache and removes L2 from disk completely.
destroy([$ns]) destroy([ns => $ns])
Gets a unique cache object. Returns undef if not found.
get($key, [$ns]) get(key => $key, [ns => $ns])
Returns the expiration epoch for a given key.
getExpiresAt($key, [$ns]) getExpiresAt(key => $key, [ns => $ns])
Returns a list of cache keys.
getKeys([$ns]) getKeys([ns => $ns])
Gets the L2 cache dir.
getNamespaceDir([$ns]) getNamespaceDir([ns => $ns])
Gets a list of namespaces.
getNamespaces()
Commits the data object to the cache.
set($key, $data, [$ns], [$expire_epoch]) set(key => $key, data => $data, [ns => $ns], [expire_epoch => $expire_epoch] );
An object, reference, or string.
The epoch to expire the cache item at. This overrides the expireSecs attribute.
To install Util::Medley, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Util::Medley
CPAN shell
perl -MCPAN -e shell install Util::Medley
For more information on module installation, please visit the detailed CPAN module installation guide.