lrucache

Types

LRUCache[K; T] = ref object
  capacity: int
  list: DoublyLinkedList[Node[K, T]]
  table: Table[K, DoublyLinkedNode[Node[K, T]]]

Procs

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 capacity[K, T](cache: LRUCache[K, T]): int
proc capacity=[K, T](cache: LRUCache[K, T]; capacity: int)
proc len[K, T](cache: LRUCache[K, T]): int
Return number of key in cache
proc contains[K, T](cache: LRUCache[K, T]; key: K): bool
Check whether key in cache. Does NOT update recentness.
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 del[K, T](cache: LRUCache[K, T]; key: K)
Delete key in cache. Does nothing if key is not in cache.
proc clear[K, T](cache: LRUCache[K, T])
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 `[]=`[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 get[K, T](cache: LRUCache[K, T]; key: K): T
Alias of cache[key]
proc put[K, T](cache: LRUCache[K, T]; key: K; val: T): T
Alias of cache[key] = val
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
Equivalent to cache.len == 0
proc isFull[K, T](cache: LRUCache[K, T]): bool
Equivalent to cache.len == cache.capacity