package aches-lwt

  1. Overview
  2. Docs

MAP are caches for plain promises.

The cache is automatically cleaned of promises that are rejected (that is, that fail with an exception).

The cache also cancels promises that it purges out for reasons of space.

A Mutable structure akin to a hash-table, but with a size bound and for storing promimses. Note that, different caches have different policies towards the size bounds: some uphold the bound strictly, some treat the bound as a suggestion. In addition, some caches count their elements somewhat sloppily.

In general, the caches of Aches are intended to be used in settings that do not require strict, by-the-number, extremely-predictable behaviors.

See Lache (or Functors) for more information.

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 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.

val put : 'a t -> key -> 'a 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 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 Lwt.t) list

take_all c

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

take_some c f

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

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

OCaml

Innovation. Community. Security.