aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/util/cache.gleam
blob: c007d358a6c48c198c6e022da2226679ada4c671 (plain)
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
import gleam/dict.{type Dict}
import gleam/otp/actor.{type Next, Stop}
import gleam/erlang/process.{type Subject, Normal}

const timeout = 1000

type Message(k, v) {
  Shutdown
  Get(key: k, client: Subject(Result(v, Nil)))
  Set(key: k, value: v)
}

type Server(k, v) =
  Subject(Message(k, v))

fn handle_message(
  message: Message(k, v),
  map: Dict(k, v),
) -> Next(Message(k, v), Dict(k, v)) {
  case message {
    Shutdown -> Stop(Normal)
    Get(key, client) -> {
      process.send(client, dict.get(map, key))

      actor.continue(map)
    }
    Set(key, value) -> actor.continue(dict.insert(map, key, value))
  }
}

pub opaque type Cache(k, v) {
  Cache(server: Server(k, v))
}

pub fn create(apply fun: fn(Cache(k, v)) -> t) -> t {
  let assert Ok(server) = actor.start(dict.new(), handle_message)
  let result = fun(Cache(server))
  process.send(server, Shutdown)
  result
}

pub fn set(in cache: Cache(k, v), for key: k, insert value: v) -> Nil {
  process.send(cache.server, Set(key, value))
}

pub fn get(from cache: Cache(k, v), fetch key: k) -> Result(v, Nil) {
  process.call(cache.server, fn(c) { Get(key, c) }, timeout)
}

pub fn memoize(with cache: Cache(k, v), this key: k, apply fun: fn() -> v) -> v {
  let result = case get(from: cache, fetch: key) {
    Ok(value) -> value
    Error(Nil) -> fun()
  }
  set(in: cache, for: key, insert: result)
  result
}