# Cache validity: expiry, negative results, invalidation, and generation-safe fetch completion. Source: https://github.com/paymog/bend-kit/tree/main/cache import Base import bend-kit-collections@0.1.6.0/omap.bend as OMap import bend-kit-collections@0.1.6.0/order.bend as Order # How long a result stays fresh, in the units of the caller's clock. type Policy is Data: Policy{ttl: Nat, negative_ttl: Nat} # What one fetch found upstream: a value at an upstream version, or nothing. type Found<-V: Data> is Data: Found{version: Nat, value: V} Missing{} # A cached result. It serves a read at `now` only while now < expires. type Entry<-V: Data> is Data: Hit{version: Nat, value: V, expires: Nat} Miss{expires: Nat} # The state of one key. Fetches take the generations floor, floor+1, ..., next-1. # A completion lands only when floor <= its generation < next. It then raises # floor past its generation, so no older fetch can land after it. type Slot<-V: Data> is Data: Slot{next: Nat, floor: Nat, entry: Maybe<&2, Entry>} # One fetch in flight: its key, its generation, and the clock when it started. # It is affine, so it completes at most once. type Ticket is Type: Ticket{key: String, gen: Nat, at: Nat} type Cache<-V: Data> is Data: Cache{policy: Policy, slots: OMap.OMap>} # Slot # ---- def Slot.new(-V: Data) -> Slot: Slot{0n, 0n, None{}} def Entry.expires(-V: Data, e: Entry) -> Nat: match e: case Hit{v, x, t}: t case Miss{t}: t # The entry for a result fetched at `at`. Its age counts from the fetch start. def Entry.of(-V: Data, r: Found, p: Policy, at: Nat) -> Entry: match r: case Found{v, x}: Policy{ttl, neg} = p Hit{v, x, Nat.add(at, ttl)} case Missing{}: Policy{ttl, neg} = p Miss{Nat.add(at, neg)} def Slot.serve(-V: Data, fresh: Bool, e: Entry) -> Maybe<&2, Entry>: match fresh: case True{}: Some{e} case False{}: None{} def Slot.lookup(-V: Data, m: Maybe<&2, Entry>, +now: Nat) -> Maybe<&2, Entry>: match m: case None{}: None{} case Some{+e}: Slot.serve(V, Nat.is_lt(now, Entry.expires(V, e)), e) # The fresh entry at `now`, or None when the caller must fetch. def Slot.read(-V: Data, s: Slot, +now: Nat) -> Maybe<&2, Entry>: Slot{n, f, m} = s Slot.lookup(V, m, now) # Takes the next generation for a new fetch. def Slot.start(-V: Data, s: Slot) -> Nat & Slot: Slot{+n, f, m} = s (n, Slot{1n+n, f, m}) def Slot.complete.if(-V: Data, ok: Bool, n: Nat, f: Nat, m: Maybe<&2, Entry>, g: Nat, e: Entry) -> Slot: match ok: case True{}: Slot{n, 1n+g, Some{e}} case False{}: Slot{n, f, m} # Stores the result of fetch g, unless g is stale or was never issued. def Slot.complete(-V: Data, p: Policy, s: Slot, +g: Nat, at: Nat, r: Found) -> Slot: Slot{+n, +f, m} = s Slot.complete.if(V, Nat.is_le(f, g) && Nat.is_lt(g, n), n, f, m, g, Entry.of(V, r, p, at)) # Drops the entry and voids every fetch issued so far. def Slot.invalidate(-V: Data, s: Slot) -> Slot: Slot{+n, f, m} = s Slot{n, n, None{}} # Drops the entry. The generations stay, so no fetch that was stale before is accepted after. def Slot.evict(-V: Data, s: Slot) -> Slot: Slot{n, f, m} = s Slot{n, f, None{}} # Cache # ----- def Cache.new(-V: Data, p: Policy) -> Cache: Cache{p, OMap.OMap.new(String, Slot)} def Cache.slot(~V: Data, slots: OMap.OMap>, +key: String) -> Slot: Maybe.default(&2, Slot, OMap.OMap.get(~String, ~Slot, ~Order.string, slots, key), Slot.new(V)) def Cache.put(~V: Data, p: Policy, slots: OMap.OMap>, +key: String, +s: Slot) -> Cache: Cache{p, OMap.OMap.put(~String, ~Slot, ~Order.string, slots, key, s)} # The fresh entry for key at `now`, or None when the caller must fetch. def Cache.read(~V: Data, c: Cache, +key: String, +now: Nat) -> Maybe<&2, Entry>: Cache{p, slots} = c Slot.read(V, Cache.slot(~V, slots, key), now) def Cache.start.fin(~V: Data, p: Policy, slots: OMap.OMap>, +key: String, at: Nat, r: Nat & Slot) -> Ticket & Cache: (g, s) = r (Ticket{key, g, at}, Cache.put(~V, p, slots, key, s)) # Starts a fetch of key at clock `at`. Fetch upstream, then pass the ticket to complete. def Cache.start(~V: Data, c: Cache, +key: String, at: Nat) -> Ticket & Cache: Cache{p, +slots} = c Cache.start.fin(~V, p, slots, key, at, Slot.start(V, Cache.slot(~V, slots, key))) # Stores what the ticket's fetch found. A stale ticket changes nothing. def Cache.complete(~V: Data, c: Cache, t: Ticket, r: Found) -> Cache: Cache{+p, +slots} = c Ticket{+key, g, at} = t Cache.put(~V, p, slots, key, Slot.complete(V, p, Cache.slot(~V, slots, key), g, at, r)) # Drops key's entry and voids its fetches in flight. def Cache.invalidate(~V: Data, c: Cache, +key: String) -> Cache: Cache{p, +slots} = c Cache.put(~V, p, slots, key, Slot.invalidate(V, Cache.slot(~V, slots, key))) # Drops key's entry. Its generations stay, so stale fetches stay stale. def Cache.evict(~V: Data, c: Cache, +key: String) -> Cache: Cache{p, +slots} = c Cache.put(~V, p, slots, key, Slot.evict(V, Cache.slot(~V, slots, key)))