https://github.com/serverless-dns/lfu-cache
https://github.com/serverless-dns/lfu-cache
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/serverless-dns/lfu-cache
- Owner: serverless-dns
- License: mpl-2.0
- Created: 2021-03-04T14:45:00.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-05-08T12:54:23.000Z (about 1 year ago)
- Last Synced: 2025-06-11T18:32:02.519Z (about 1 year ago)
- Language: JavaScript
- Size: 104 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
*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`](https://github.com/serverless-dns/lfu-cache/actions/workflows/perf.yml)).
`lfu.js` serves as the entrypoint to construct and interact with these LFUs.
```js
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](https://apenwarr.ca/log/20230415).