Creates a new instance of LRUCache
.
The optional [[sizeFunction]] can be used to fine tune the memory consumption of all cached elements, thus [[cacheCapacity]] means then memory used (in MBs). Otherwise, if [[sizeFunction]] is not specified, the [[cacheCapacity]] accounts for the maximum number of elements stored.
Number used to configure the maximum cache size, may express number of entries or memory consumed in megabytes depending on [[sizeFunction]].
A function determining the size per element.
Optional callback that is called on every item that should be evicted from the cache to determine if it can be removed, or should be locked in the cache.
It returns true
if the item can be removed from cache, false
otherwise. Locking items in
the cache should be a temporary measure, since if the cache is filled with non-evictable
items only, it may grow beyond its capacity.
Note: This callback is not called when an item is explicitly deleted from the map via delete or clear.
Returns the maximum capacity of the cache, i.e. the maximum number of elements this cache can contain or the total amount of memory that may be consumed by cache if element size function was specified in cache c-tor.
The capacity of the cache.
The internal map object.
Returns the newest entry in the cache.
Newest entry in the cache.
Returns the oldest entry in the cache.
Note: Does not promote the oldest item as most recently used item.
Oldest entry in the cache.
The size of the cache, i.e. the sum of all the sizes of all the objects in the cache.
The size of the cache.
Clears the cache and removes all stored key-value pairs.
Does not call the eviction callback. Use evictAll to clear the cache and call the eviction callback.
Explicitly removes a key-value pair from the cache.
Note: This is an explicit removal, thus, the eviction callback will not be called.
The key of the key-value pair to delete.
true
if the key-value pair existed and was deleted, false
otherwise.
Evicts all items from the cache, calling the eviction callback on each item.
Use clear to remove all items without calling the eviction callback.
Evict selected elements from the cache using [[selector]] function.
The function for selecting elements for eviction.
Optional this object reference.
Iterates over all items from the most recently used item to the least recently used one.
Note: Results are undefined if the entire cache is modified during iteration. You may although modify the current element in [[callbackfn]] function.
The callback to call for each item.
Optional this argument for the callback.
Looks up key in the cache and returns the associated value.
The key to look up.
The associated value, or undefined
if the key-value pair is not in the cache.
Test if a key/value pair is in the cache.
The key to look up.
true
if the key-value pair is in the cache, false
otherwise.
Inserts or updates a key/value pair in the cache.
If the key already existed in the cache, it will be updated and promoted to the most recently used item.
If the key didn't exist in the cache, it will be inserted as most recently used item. An eviction of the least recently used item takes place if the cache exceeded its capacity.
The key for the key-value pair to insert or update.
The value for the key-value pair to insert or update.
Resets the capacity of this cache. If newCapacity
is smaller than the current cache size,
all items will be evicted until the cache shrinks to newCapacity
.
The new capacity of this cache.
Resets the cache capacity and function used to measure the element size.
The new capacity masured in units returned from [[sizeMeasure]] funtion.
Function that defines the size of element, if you want to measure number of elements only always return 1 from this function (default), you may also specify own function that measures entries by memory consumed, nubmer of sub-elements, etc.
Updates the size of all elements in this cache. If their aggregated size is larger than the capacity, items will be evicted until the cache shrinks to fit the capacity.
Generated using TypeDoc
Fixed size cache that evicts its entries in least-recently-used order when it overflows. Modeled after standard JavaScript
Map
otherwise.