Library
Module
Module type
Parameter
Class
Class type
define a cache with custom key and monad types
module K : Map.OrderedType
type key = K.t
the type for cache keys
type 'a deferred = 'a M.t
the type for deferred computations
the type for cache validation callbacks. validate (key,old)
is called with old = None
if the key is not in the cache yet and has to be computed. It is called with old=Some data
if the key is already in the cache. The validator has to decide whether to return the same value or recompute it (perhas because it is expired / no longer fresh).
val create : int -> 'a t
create n
creates a Anycache_LRU/2Q that can hold at most n
elements
with_cache cache f key
acts like f key
, except it might make fewer calls to f
when the key
is already in the cache. On successful termination of f key
the result is stored in the cache. f key
must always return the same value when called with the same key. Cached values never expire, they are only removed if the cache capacity is exceeded, see cache replacement policy.
with_validator cache validator key
calls validator
each time key
is accessed. If the key
is not in the cache validator (key, None)
is called and it should compute the value associated with key
. If the key
is already in the cache then validator [key, Some value]
is called and validator
should decide whether the value
is still fresh, or if it should be recomputed. The value returned by validator
in either case is stored in the cache
. validate (key,_)
is allowed to return different values when called with the same key again, provided it properly validates the lifetime of old entries. Values are removed from the cache when its capacity is exceeded, see cache replacement policy.
get cache key
retrieves the key
from the cache
if it exists. The key's position in Anycache_LRU is updated