package aches-lwt

  1. Overview
  2. Docs

Make_result(C) is a MAP_RESULT. The cache policies are that of C.

Similar to MAP but the promises resolve to meaningful result: When a promise resolve to Error it is interpreted as a failure by the cache and the binding is removed. When a promise resolve to Ok _ is interpreted as success and the binding is kept.

Parameters

module C : Rache.TRANSFER

Signature

type key = C.key

The type of keys on which values in the cache are indexed.

type ('a, 'error) t

The type of caches holding bindings from key to ('a, 'error) result Lwt.t

val create : int -> ('a, 'error) 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, 'error) t -> key -> ('a, 'error) result 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, 'error) t -> key -> ('a, 'error) result 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, 'error) t -> (key * ('a, 'error) result Lwt.t) list

take_all c

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

take_some c f

val bind : ('a, 'error) t -> key -> (('a, 'error) result -> '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, 'error) t -> key -> (key -> ('a, 'error) result Lwt.t) -> (('a, 'error) result -> '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, 'error) 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, 'error) 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, 'error) 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, 'error) t -> unit

clear c removes all bindings from c, canceling all unresolved promises.

val filter : ('a, 'error) t -> (key -> bool) -> unit

filter c f

val length : ('a, 'error) t -> int

length c is the number of bindings held by c.

val capacity : ('a, 'error) t -> int

capacity c is the number of bindings c can hold: capacity (create n) = n

OCaml

Innovation. Community. Security.