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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
exception Exception of string
module Map = Map.Make (String)
type data = string Map.t
type t =
{ key : string
; data : data
; expire_date : Ptime.t
}
let sexp_of_t { key; expire_date; _ } =
let open Sexplib0.Sexp_conv in
let open Sexplib0.Sexp in
List
[ List [ Atom "key"; sexp_of_string key ]
; List [ Atom "expire_date"; sexp_of_string (Ptime.to_rfc3339 expire_date) ]
]
;;
let one_week = 60 * 60 * 24 * 7
let default_expiration_date now = one_week |> Ptime.Span.of_int_s |> Ptime.add_span now
let key session = session.key
let data session = session.data
let is_expired now session = Ptime.is_later now ~than:session.expire_date
type data_map = (string * string) list [@@deriving yojson]
let string_of_data data =
data |> Map.to_seq |> List.of_seq |> data_map_to_yojson |> Yojson.Safe.to_string
;;
let data_of_string str =
str
|> Yojson.Safe.from_string
|> data_map_of_yojson
|> Result.map List.to_seq
|> Result.map Map.of_seq
;;
type map = (string * string) list [@@deriving yojson]
let get key session = Map.find_opt key session.data
let set ~key ~value session = { session with data = Map.add key value session.data }
let remove ~key session = { session with data = Map.remove key session.data }
let pp ppf { key; data; _ } =
Caml.Format.fprintf ppf "key: %s data: %s " key (string_of_data data)
;;
let t =
let encode m =
let data = m.data |> string_of_data in
Ok (m.key, data, m.expire_date)
in
let decode (key, data, expire_date) =
match data |> data_of_string with
| Ok data -> Ok { key; data; expire_date }
| Error msg -> Error msg
in
Caqti_type.(custom ~encode ~decode (tup3 string string ptime))
;;