Sub::Contract::Cache - A data cache
my $cache = new Sub::Contract::Cache(max_size => 10000, namespace => 'foo'); if ($cache->has($key)) { return $cache->get($key); } else { my $value = foo(@args); $cache->set($key,$value); return $value; }
A Sub::Contract::Cache is just a data cache used by contracts to memoize subroutine's results. Sub::Contract has its own cache implementation for efficiency reasons.
my $cache = new(max_size => $max_size, namespace => $name)
Return an empty cache object that may contain up to $max_size elements and caches results from the subroutine $name.
$max_size
$name
$contract->clear([size => $max_size])
Empty this cache of all its elements.
$contract->set($key,$ref_result)
Add a cache entry for the key $key with result $result.
$key
$result
$contract->has($key)
Return true if the cache contains a result for this key, false if not.
$contract->get($key)
Return the cached result associated with key $key. You must call has first to ensure that there really is a cached result for this key. get on an unknown key will return undef and not fail.
has
get
See 'Sub::Contract'.
$Id: Cache.pm,v 1.3 2009/06/16 12:23:58 erwan_lemonnier Exp $
Erwan Lemonnier <erwan@cpan.org>
<erwan@cpan.org>
See Sub::Contract.
To install Sub::Contract, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::Contract
CPAN shell
perl -MCPAN -e shell install Sub::Contract
For more information on module installation, please visit the detailed CPAN module installation guide.