Cache -- Generic TTL-based cache with Eio support#
A generic in-memory cache with configurable TTL (time-to-live), jitter to prevent thundering herd, and thread-safe access using Eio mutexes.
Installation#
Install with opam:
$ opam install cache
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install cache
Usage#
String-keyed cache (pre-instantiated)#
let lookup env =
let cache = Cache.String.create ~clock:(Eio.Stdenv.clock env) () in
Cache.String.set cache "user:42" "Alice";
match Cache.String.get cache "user:42" with
| Some data -> ()
| None -> ()
Compute-if-absent pattern#
(* Returns cached value or computes and caches it *)
let data cache =
Cache.String.get_or_compute cache "user:42" (fun () -> "Alice")
Custom key type#
module IntCache = Cache.Make (struct
type t = int
let equal = Int.equal
let hash = Hashtbl.hash
end)
let cache clock = IntCache.create ~clock ~base_ttl:300.0 ~jitter:0.1 ()
API#
create ~clock ?base_ttl ?jitter ()-- Create cache (default TTL: 60s, jitter: 0.2)get/set/remove-- Basic operationsget_or_compute-- Compute-if-absent with automatic cachinggc-- Remove expired entriesclear-- Remove all entriesstats-- Returns(total_entries, valid_entries)
Licence#
MIT