Types
EmptyLruCacheError = object of LruCacheError
LruCache[K; T] = ref object capacity: int list: DoublyLinkedList[Node[K, T]] table: Table[K, DoublyLinkedNode[Node[K, T]]]
LruCacheError = object of CatchableError
Procs
proc `[]=`[K, T](cache: LruCache[K, T]; key: K; val: T)
- Put value v in cache with key k. Remove least recently used value from cache if length exceeds capacity.
proc `[]`[K, T](cache: LruCache[K, T]; key: K): T
- Read value from cache by key and update recentness Raise KeyError if key is not in cache.
proc contains[K, T](cache: LruCache[K, T]; key: K): bool
- Check whether key in cache. Does NOT update recentness.
proc del[K, T](cache: LruCache[K, T]; key: K)
- Delete key in cache. Does nothing if key is not in cache.
proc getLruKey[K, T](cache: LruCache[K, T]): K
- Return least recently used key. Raise EmptyLruCacheError if cache is empty.
proc getLruValue[K, T](cache: LruCache[K, T]): T
- Return least recently used value. Raise EmptyLruCacheError if cache is empty.
proc getMruKey[K, T](cache: LruCache[K, T]): K
- Return most recently used key. Raise EmptyLruCacheError if cache is empty.
proc getMruValue[K, T](cache: LruCache[K, T]): T
- Return most recently used value. Raise EmptyLruCacheError if cache is empty.
proc getOption[K, T](cache: LruCache[K, T]; key: K): Option[T]
- Similar to get, but return None if key is not in cache or else return Some(value) and update recentness
proc getOrDefault[K, T](cache: LruCache[K, T]; key: K; val: T): T
- Similar to get, but return val if key is not in cache
proc getOrPut[K, T](cache: LruCache[K, T]; key: K; val: T): T
- Similar to get, but put and return val if key is not in cache
proc isFull[K, T](cache: LruCache[K, T]): bool {.inline.}
- Equivalent to cache.len == cache.capacity Raise EmptyLruCacheError if cache is empty.
proc newLruCache[K, T](capacity: int): LruCache[K, T]
- Create a new Least-Recently-Used (LRU) cache that store the last capacity-accessed items.