src/lrucache

Types

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 capacity=[K, T](cache: LruCache[K, T]; capacity: int)
Resize the maximum capacity of cache
proc capacity[K, T](cache: LruCache[K, T]): int
Get the maximum capacity of cache
proc clear[K, T](cache: LruCache[K, T])
remove all items
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 get[K, T](cache: LruCache[K, T]; key: K): T
Alias of cache[key]
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 isEmpty[K, T](cache: LruCache[K, T]): bool {.inline.}
Equivalent to cache.len == 0
proc isFull[K, T](cache: LruCache[K, T]): bool {.inline.}
Equivalent to cache.len == cache.capacity Raise EmptyLruCacheError if cache is empty.
proc len[K, T](cache: LruCache[K, T]): int
Return number of key in cache
proc newLruCache[K, T](capacity: int): LruCache[K, T]
Create a new Least-Recently-Used (LRU) cache that store the last capacity-accessed items.
proc peek[K, T](cache: LruCache[K, T]; key: K): T
Read value by key, but NOT update recentness. Raise KeyError if key is not in cache.
proc put[K, T](cache: LruCache[K, T]; key: K; val: T): T
Alias of cache[key] = val