Generic TTL cache with Eio
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 80.4%
Dune 6.7%
Other 12.9%
20 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-cache https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-cache
git@git.recoil.org:gazagnaire.org/ocaml-cache git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-cache

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

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 operations
  • get_or_compute -- Compute-if-absent with automatic caching
  • gc -- Remove expired entries
  • clear -- Remove all entries
  • stats -- Returns (total_entries, valid_entries)

Licence#

MIT