No description
Find a file
2023-01-27 19:12:55 +08:00
docs Release 1.1.4 2022-02-21 00:47:55 +08:00
src fix: make len available to other procs 2022-02-09 11:52:57 +11:00
tests add get Lru Mru key value 2020-09-07 19:08:38 +08:00
.gitignore init 2020-04-12 20:42:30 +08:00
.release-it.json fix name of .release-it and hooks 2020-05-08 00:09:22 +08:00
lrucache.nimble Release 1.1.4 2022-02-21 00:47:55 +08:00
README.md Update README.md 2023-01-27 19:12:55 +08:00

LRU cache

A common implemenation of LRU cache (hash table + doubly-linked list). All operations are in time complexity of O(1). This implementation is not thread-safe.

Installation

$ nimble install lrucache

API

See here

Usage

# create a new LRU cache with initial capacity of 1 items
let cache = newLRUCache[int, string](1) 

cache[1] = "a"
cache[2] = "b"

# key 1 is not in cache, because key 1 is eldest and capacity is only 1
assert: 1 notin cache 
assert: 2 in cache

# increase capacity and add key 1 
cache.capacity = 2 
cache[1] = "a"
assert: 1 in cache
assert: 2 in cache

# update recentness of key 2 and add key 3, then key 1 will be discarded.
echo cache[2]
cache[3] = "c"
assert: 1 notin cache
assert: 2 in cache
assert: 3 in cache