package ringo-lwt

  1. Overview
  2. Docs

Module Functors.MakeSource

A Mutable structure akin to a Ringo.CACHE_MAP but with Lwt-aware functions. E.g., consider the following use of a Ringo.CACHE_MAP:

let c = Ringo_map.create 1024 in let resolve k = match Ringo_map.find_opt k with | Some v -> Lwt.return v | None -> do_resolve k >>= fun v -> Ringo_map.replace c k v; Lwt.return v

In this example, there is a race condition: if do_resolve takes time to complete, another call to resolve may be made concurrently to the first one.

The function find_or_replace in Ringo_lwt.CACHE_MAP works around this issue.

Parameters

Signature

Sourcetype key = C.key

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

Sourcetype 'a t

The type of caches holding bindings from key to 'a

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

Sourceval replace : 'a t -> key -> 'a Lwt.t -> unit

replace c k p binds the key k to p in the cache c.

Note that promise that are rejected are automatically removed from the cache.

Note that, for the purpose of determining if an inserted binding is supernumerary, and thus if it pushes another binding out of the cache, an unresolved binding counts fully.

Sourceval 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 bindings of c.

Note that fold waits for the different bindings to resolve. As a result, you can write let join c = fold (fun _ _ () -> Lwt.return_unit) () to wait for all currently-held bindings to resolve.

Note that for some cache, 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.

Sourceval fold_promises : (key -> 'a Lwt.t -> 'b -> 'b) -> 'a t -> 'b -> 'b

fold_promises f c init folds the function f and value init over the promises of bindings of c.

You can use this function to count the unresolved promises of c, or to cancel all unresolved promises, or some other such functio.

Note that for some cache, 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.

Sourceval find_opt : 'a t -> key -> 'a Lwt.t option

find_opt c k is None if k is not bound in c. Otherwise it is Some p where p is bound to k in c.

Note that the in some caches, this may have a side effect on the k-to-v binding. Specifically, in some caches, it might make it less likely to be removed when supernumerary bindings are inserted.

Sourceval find_or_replace : 'a t -> key -> (key -> 'a Lwt.t) -> 'a Lwt.t

find_or_replace c k f behaves likes find c k if k is bound in c, and it behaves like replace c k f otherwise. Either way, it returns the promise that resolve to the value assoicated to k whichever behaviour find_or_replace resembled.

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

remove c k removes the binding from k in c. If k is not bound in c, it does nothing. If the binding is not resolved yet, it also cancels the promise.

Note that in some caches, removed bindings can still count towards the size bound for some time.

Sourceval length : 'a t -> int

length c is the number of bindings held by c.

Sourceval capacity : 'a t -> int

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

Sourceval clear : 'a t -> unit

clear c removes all bindings from c. It also cancels unresolved bindings.