NAME

Sub::Contract::Cache - A data cache

SYNOPSIS

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;
}

DESCRIPTION

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.

API

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.

$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.

$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.

SEE ALSO

See 'Sub::Contract'.

VERSION

$Id: Cache.pm,v 1.3 2009/06/16 12:23:58 erwan_lemonnier Exp $

AUTHOR

Erwan Lemonnier <erwan@cpan.org>

LICENSE

See Sub::Contract.