package aches-lwt

  1. Overview
  2. Docs

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

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.

val put : 'a t -> key -> 'a option Lwt.t -> unit

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.

val take : 'a t -> key -> 'a option Lwt.t option

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_all : 'a t -> (key * 'a option Lwt.t) list

take_all c

val take_some : 'a t -> (key -> bool) -> (key * 'a option Lwt.t) list

take_some c f

val bind : 'a t -> key -> ('a option -> 'b Lwt.t) -> 'b Lwt.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.

val bind_or_put : 'a t -> key -> (key -> 'a option Lwt.t) -> ('a option -> 'b Lwt.t) -> 'b Lwt.t

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.

val fold : (key -> 'a -> 'b -> 'b Lwt.t) -> 'a t -> 'b -> 'b Lwt.t

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 -> 'b Lwt.t) -> 'a t -> 'b -> 'b Lwt.t

fold_oldest_first is like fold but in reversed order: oldest elements of c first. This function has the same limitation as fold.

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

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 filter : 'a t -> (key -> bool) -> unit

filter c f

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


Innovation. Community. Security.