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<K, V>(limit: number, container: EntryContainer<K, V>): LRUCache<K, V> Construct a new LRUCache to hold up to limit entries.  
assign(entries: Iterable<[K, V], any, any>): 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): undefined | V Remove entry key from cache and return its value.  
entries(): undefined | Iterator<undefined | [K, V], any, any> Returns an iterator over all entries, starting with the oldest.  
find(key: K): undefined | V 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): undefined | V 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(): undefined | Iterator<undefined | K, any, any> 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(): undefined | [K, V] Purge the least recently used (oldest) entry from the cache.  
toJSON(): { key: K, value: V }[] Returns a JSON (array) representation  
toString(): string Returns a String representation  
values(): undefined | Iterator<undefined | V, any, any> 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 Entry<K, V> | undefined Most recently-used entry.  
oldest Entry<K, V> | undefined Least recently-used entry.  
size number Current number of items  

Defined in

Last Updated: 14 November, 2024