Skip to content

serverless-dns/lfu-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jun 12, 2023
41ab262 · Jun 12, 2023

History

94 Commits
Mar 10, 2023
Feb 16, 2022
Jun 12, 2023
Jun 12, 2023
Mar 11, 2023
Mar 1, 2022
May 30, 2021
Dec 11, 2021
Feb 16, 2022
Mar 4, 2021
Apr 27, 2023
Jun 12, 2023
Mar 2, 2022

Repository files navigation

Least Frequently Used cache.

/strat contains an approximate implementation of the Clock algorithm, which increments frequency on cache-hits and decrements frequency of items in the same 'clock' where a newer entry seeks admission into, which helps avoid starvation. Another algorithm, O1 implements a constant-time LFU-LRU hybrid cache, but the flip side is it consumes extra memory compared to Clock, though is very much simpler to reason about.

/ds contains implementations of underlying stores supporting the cache: A HashMap backed by the native Map impl, and a restrictive RangeList backed by a Skip List.

That is, Clock.js, MultiClock.js, O1.js instances can be backed by either HashMap for point queries (takes ~500ms for 1M point-queries), or by RangeList for range queries (takes ~5000ms for 1M range queries; see the perf workflow).

lfu.js serves as the entrypoint to construct and interact with these LFUs.

  import { LfuCache, RangeLfu } from "@serverless-dns/lfu.js";

  const lfu = new LfuCache("L1", 10)
  lfu.put(1, "a") // 1 -> "a"
  const v = lfu.get(1) // v = "a"

  const rgcache = new RangeLfu("R1", 10)
  rgcache.put(1, 10, "a") // (1, 10) -> "a"
  const v = rgcache.get(5) // v = "a"

All caches are magic. Knowing their mechanism is not enough to predict their outcome.

- Avery Pennarun.