package savvy

  1. Overview
  2. Docs
A straightforward OAuth2 client

Install

Dune Dependency

Authors

Maintainers

Sources

savvy-0.4.0.tbz
sha256=a9e2b62cbf273c881d47d86db243706e41dcf52ce3107288b7edcf61cc53bb10
sha512=2903142b0f64d06c02ab91e3a3545d8e38f0bef34a57e7aa8d97d06f11f12daee43825a4c91afa39f5c6542b61e8bb3a57b1076358031ae74b5d6d0e5944cecd

doc/src/savvy.storage/storage.ml.html

Source file storage.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(* Any user-defined storage implementation must have at least these three methods *)
module type STORAGE_UNIT =
  sig 
    type t
    type value (* (string * config * float) *)

    (* All storage mechanisms want to be sure that old data is cleared out eventually *)
    val ttl: float

    (* When implementing this interface, I recommend doing a clean out of stale values in get *)
    val get: string -> (value * float) option
    val remove: string -> unit
    val update: string -> value -> unit
  end

module type STORAGE_KIND =
  sig
    type value
    val ttl : float
  end

module MakeInMemoryStorage(V : STORAGE_KIND) : STORAGE_UNIT with type value = V.value =
  struct
    type value = V.value
    type stored = (value * float)
    type t = (string, stored) Hashtbl.t

    let ttl = V.ttl

    let store = Hashtbl.create 100

    let is_expired (_value, created_at) =
      Unix.time () -. created_at > V.ttl

    let clean () =
      Hashtbl.filter_map_inplace
        (fun _key v -> if is_expired v then None else Some v)
        store

    (* Due to sealing, only the below methods are publicly accessible *)
    let get state = 
      clean ();
      Hashtbl.find_opt store state

    let remove state = Hashtbl.remove store state
    
    let update state value = Hashtbl.replace store state (value, Unix.time ())
  end

OCaml

Innovation. Community. Security.