MRUCache Class
Cache which drops items based on "most recently used." Items are dropped when a user-defined criterion is exceeded, e.g., total size or number of items.
The items are stored in a map of {data,mru_item_ref}. The MRU items are stored in a doubly linked list (which stores the map keys) to allow easy re-ordering and dropping of items. Every cache hit moves the associated MRU item to the front of the list.
Constructor
MRUCache
-
config
Parameters:
-
config
Object-
metric
FunctionComputes the metric for an item. It receives the value as an argument and must return a positive number.
-
limit
NumberMaximum allowed value of the metric. Items are dropped off the end of the MRU list until the metric is less than or equal to the limit.
-
[meta]
Function optionalAttaches meta data to an item when it is added to the cache. It receives the value as an argument.
-
[stats]
Boolean optionalPass true if you want to collect basic statistics. Pass a function if you want to control what information is stored for each key. The function receives the key, the value, and the stat object.
-
Methods
clear
()
Remove all values.
dumpStats
()
Object
This resets all the values.
Returns:
the current stats
get
-
key
Retrieve a value.
Parameters:
-
key
Stringthe key of the object to retrieve
Returns:
the stored object, or undefined if the slot is empty
put
-
key
-
value
Store a value.
Parameters:
-
key
Stringthe key of the value
-
value
Objectthe value to store
Returns:
false if the key has already been used
remove
-
key
Remove an value.
Parameters:
-
key
Stringthe key of the value
Returns:
the value that was removed, or undefined if the slot was empty
replace
-
key
-
value
Store a value.
Parameters:
-
key
Stringthe key of the value
-
value
Objectthe value to store
Returns:
the original value that was in the slot, or undefined if the slot is empty