Library
Module
Module type
Parameter
Class
Class type
Make_option(C)
is a MAP_OPTION
. The cache policies are that of C
.
Similar to MAP
but the promises resolve to meaningful option
: When a promise resolve to None
it is interpreted as a failure by the cache and the binding is removed. When a promise resolve to Some _
is interpreted as success and the binding is kept.
module C : Rache.TRANSFER
type key = C.key
The type of keys on which values in the cache are indexed.
val create : int -> 'a t
create n
creates a cache with a size-bound of n
. Remember that the size-bound is not upheld strictly by all caches. Moreover, caches instantiated with a specialised size (i.e., empty and singleton caches) ignore the size parameter entirely.
put c k p
binds the key k
to the promise p
in the cache c
. This transfer the responsibility for p
to c
: c
will cancel the promise if it is ever remove
d, clear
ed, put
, etc.
If k
is already bound to a promise p'
in c
, the previous binding disappears and is replaced by the new binding to p
. If this happens then p'
is canceled.
If k
is not already bound in c
, then put
adds a new binding from k
to p
. This may or may not cause another binding to be removed from the cache, depending on the number of bindings already present in the cache c
, the size-bound of the cache c
, and the policy of the cache c
towards its size-bound. If a supernumerary binding is removed, its promise is canceled.
take c k
removes the binding of k
from c
and returns it to the caller along with the responsibility for the promise: the cache will not cancel the promise.
If k
is not bound in c
, then take c k
does nothing.
bind c k f
waits for the promise bound to k
in c
to resolve and applies f
to the resolved value. In more hand-wavy words: bind c k f
is similar to Lwt.bind (find c k) f
.
If k
isn't bound in c
, then it returns None
.
When you call bind c k f
you take shared-responsibility for the promise. From this there is an important remark:
The promise's ownership is shared. For this reason the cache will not cancel the promise, even if the key-promsie binding is removed from the cache because a supernumerary binding is inserted. If this happens the cache simply lets go of the promise and you become the sole owner. There is no mechanism to be notified that you become the sole owner. If you need stricter rules of ownership you need to restrict yourself to using put
and take
only.
Note that the cache is designed to gracefully handle cancelation of the promises inside. And so you can cancel a promise you called bind
on.
bind_or_put c k mk f
is identical to bind c k f
if k
is bound in c
.
If k
is not bound in c
, then the promise mk k
is created. It is bound to k
in c
. Its responsibility is shared by the cache and the caller as per the documentation of bind
above.
fold f c init
folds the function f
and value init
over the keys and values that the bindings of c
resolve to. Bindings with promises resolving to None
are ignored.
Note that for some caches, this function may fold over a subset of the bindings of c
. Specifically, on caches with a Weak
overflow policy, only the strongly-held elements are folded over.
fold_oldest_first
is like fold
but in reversed order: oldest elements of c
first. This function has the same limitation as fold
.
remove c k
removes the binding from k
in c
and cancels its promise (if it hasn't resolved yet).
If k
is not bound in c
, it does nothing.
val clear : 'a t -> unit
clear c
removes all bindings from c
, canceling all unresolved promises.
val length : 'a t -> int
length c
is the number of bindings held by c
.
val capacity : 'a t -> int
capacity c
is the number of bindings c
can hold: capacity (create n) = n