Legend:
Library
Module
Module type
Parameter
Class
Class type
MAP_OPTION are caches for option promises (_ option Lwt.t).
The cache is automatically cleaned of promises that are rejected (that is, that fail with an exception) and of those that resolve to None. In that way, the cache is opinionated: it considers option as a kind of error management strategy where None is not a value worth keeping.
The cache also cancels promises that it purges out for reasons of space.
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.
type key
The type of keys on which values in the cache are indexed.
type'a t
The type of caches holding bindings from key to 'a option Lwt.t
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 removed, cleared, 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.
val take_some : 'at->(key-> bool)->(key * 'a optionLwt.t) list
take_some c f
val bind : 'at->key->('a option->'bLwt.t)->'bLwt.t option
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.
val fold_oldest_first : (key->'a->'b->'bLwt.t)->'at->'b->'bLwt.t
fold_oldest_first is like fold but in reversed order: oldest elements of c first. This function has the same limitation as fold.