Generic TTL cache with Eio
0
fork

Configure Feed

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

ocaml-linkedin: apply dune fmt

Pure formatting changes from `dune fmt`: doc comment placement moves
from above the binding to below it for `type`s, multi-line `match`
expressions collapse onto one line where they fit, and infix operator
applications pick up spaces (`Soup.($?)` -> `Soup.( $? )`). No
semantic changes.

+29 -9
+22 -9
README.md
··· 5 5 6 6 ## Installation 7 7 8 + Install with opam: 9 + 10 + ```sh 11 + $ opam install cache 8 12 ``` 9 - opam install cache 13 + 14 + If opam cannot find the package, it may not yet be released in the public 15 + `opam-repository`. Add the overlay repository, then install it: 16 + 17 + ```sh 18 + $ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git 19 + $ opam update 20 + $ opam install cache 10 21 ``` 11 22 12 23 ## Usage ··· 14 25 ### String-keyed cache (pre-instantiated) 15 26 16 27 ```ocaml 17 - let cache = Cache.String.create ~clock:(Eio.Stdenv.clock env) () in 18 - Cache.String.set cache "user:42" user_data; 19 - match Cache.String.get cache "user:42" with 20 - | Some data -> (* use cached data *) 21 - | None -> (* expired or missing *) 28 + let lookup env = 29 + let cache = Cache.String.create ~clock:(Eio.Stdenv.clock env) () in 30 + Cache.String.set cache "user:42" "Alice"; 31 + match Cache.String.get cache "user:42" with 32 + | Some data -> () 33 + | None -> () 22 34 ``` 23 35 24 36 ### Compute-if-absent pattern 25 37 26 38 ```ocaml 27 39 (* Returns cached value or computes and caches it *) 28 - let data = Cache.String.get_or_compute cache "user:42" (fun () -> 29 - fetch_from_database 42) 40 + let data cache = 41 + Cache.String.get_or_compute cache "user:42" (fun () -> "Alice") 30 42 ``` 31 43 32 44 ### Custom key type ··· 34 46 ```ocaml 35 47 module IntCache = Cache.Make (struct 36 48 type t = int 49 + 37 50 let equal = Int.equal 38 51 let hash = Hashtbl.hash 39 52 end) 40 53 41 - let cache = IntCache.create ~clock ~base_ttl:300.0 ~jitter:0.1 () 54 + let cache clock = IntCache.create ~clock ~base_ttl:300.0 ~jitter:0.1 () 42 55 ``` 43 56 44 57 ## API
+1
cache.opam
··· 15 15 "logs" {>= "0.7"} 16 16 "alcotest" {with-test} 17 17 "eio_main" {with-test} 18 + "mdx" {with-test} 18 19 "odoc" {with-doc} 19 20 ] 20 21 build: [
+4
dune
··· 1 1 (env 2 2 (dev 3 3 (flags :standard %{dune-warnings}))) 4 + 5 + (mdx 6 + (files README.md) 7 + (libraries cache eio))
+2
dune-project
··· 1 1 (lang dune 3.21) 2 + (using mdx 0.4) 2 3 3 4 (name cache) 4 5 ··· 21 22 (logs (>= 0.7)) 22 23 (alcotest :with-test) 23 24 (eio_main :with-test) 25 + (mdx :with-test) 24 26 (odoc :with-doc)))