LRUCache<K, V> Class

A mapping of a key/value pairs, where the size of the cache can be limited.

When entries are inserted, if the cache is "full", the least-recently-used (LRU) value is dropped. When entries are retrieved, they are moved to the front of the LRU list.

Illustration of the design:


      entry             entry             entry             entry
      ______            ______            ______            ______
     | head |.newer => |      |.newer => |      |.newer => | tail |
     |  A   |          |  B   |          |  C   |          |  D   |
     |______| <= older.|______| <= older.|______| <= older.|______|

 removed  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  added

Extended by

Methods

Name Description
constructor(limit: number, container: EntryContainer<K, V>): LRUCache Construct a new LRUCache to hold up to limit entries.  
assign(entries: Iterable<[K, V]>): void Replace all values in this cache with key-value pairs (2-element Arrays) from provided iterable.  
clear(): void Removes all entries  
delete(key: K): V | undefined Remove entry key from cache and return its value.  
entries(): Iterator<[K, V] | undefined> | undefined Returns an iterator over all entries, starting with the oldest.  
find(key: K): V | undefined Access value for key without registering recent use.  
forEach(fun: (value: V, key: K, m: LRUCache<K, V>) => void, thisObj?: any): void Call fun for each entry, starting with the oldest entry.  
get(key: K): V | undefined Get and register recent use of .  
has(key: K): boolean Check if there's a value for key in the cache without registering recent use.  
keys(): Iterator<K | undefined> | undefined Returns an iterator over all keys, starting with the oldest.  
set(key: K, value: V): LRUCache<K, V> Put into the cache associated with .  
shift(): [K, V] | undefined Purge the least recently used (oldest) entry from the cache.  
toJSON(): Array<{ key: K, value: V }> Returns a JSON (array) representation  
toString(): string Returns a String representation  
values(): Iterator<V | undefined> | undefined Returns an iterator over all values, starting with the oldest.  

Properties

Name Type Description
limit number Maximum number of items this cache can hold  
newest undefined | Entry<K, V> Most recently-used entry.  
oldest undefined | Entry<K, V> Least recently-used entry.  
size number Current number of items  

Defined in

Last Updated: 13 June, 2024