https://github.com/jackhftang/lrucache.nim
LRU cache in pure nim
https://github.com/jackhftang/lrucache.nim
lru-cache nim
Last synced: about 1 month ago
JSON representation
LRU cache in pure nim
- Host: GitHub
- URL: https://github.com/jackhftang/lrucache.nim
- Owner: jackhftang
- Created: 2020-04-12T12:42:54.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T11:12:55.000Z (over 3 years ago)
- Last Synced: 2023-04-19T16:30:13.791Z (about 3 years ago)
- Topics: lru-cache, nim
- Language: Nim
- Homepage: https://jackhftang.github.io/lrucache.nim/
- Size: 43.9 KB
- Stars: 8
- Watchers: 2
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://jackhftang.github.io/lrucache.nim/)
## Usage
```nim
# 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
```