package anycache

  1. Overview
  2. Docs

define a cache with custom key and monad types

Parameters

module M : Monad

Signature

type key = K.t

the type for cache keys

type 'a deferred = 'a M.t

the type for deferred computations

type 'a t

the type for caches storing associating keys of type key with values of type 'a

type 'a validator = (key * 'a option) -> 'a deferred

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

val with_cache : 'a t -> (key -> 'a deferred) -> key -> 'a deferred

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.

val with_validator : 'a t -> 'a validator -> key -> 'a deferred

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.

val get : 'a t -> key -> 'a option deferred

get cache key retrieves the key from the cache if it exists. The key's position in Anycache_LRU is updated

val set : 'a t -> key -> 'a -> unit

set cache key value stores value under key in the cache. If a value for key was already stored then it is replaced by the new value. It is not specified whether the key's position is updated in the Anycache_LRU.

OCaml

Innovation. Community. Security.