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#
opam install cache
Usage#
String-keyed cache (pre-instantiated)#
let cache = Cache.String.create ~clock:(Eio.Stdenv.clock env) () in
Cache.String.set cache "user:42" user_data;
match Cache.String.get cache "user:42" with
| Some data -> (* use cached data *)
| None -> (* expired or missing *)
Compute-if-absent pattern#
(* Returns cached value or computes and caches it *)
let data = Cache.String.get_or_compute cache "user:42" (fun () ->
fetch_from_database 42)
Custom key type#
module IntCache = Cache.Make (struct
type t = int
let equal = Int.equal
let hash = Hashtbl.hash
end)
let cache = 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