algo.Cache()
Description
Creates an instance of Cache.
You can provide two optional callbacks:
- add - Will be called with the key when a requested entry cannot be found and its return value will be used to fill in that entry
- remove - Will be called with the key and value when an entry is about to be erased
You can set the maximum number of entries by option size in the options parameter. When entries are more than that number, old entries will be purged on the LRU (least recently used) basis.
You can also specify the TTL (time to live) for all entries by option ttl in the options parameter.
Syntax
new algo.Cache()new algo.Cache((k) => getEntryValue(k))new algo.Cache((k) => getEntryValue(k),(k, v) => onRemoveEntry(k, v),{size,ttl,})
Parameters
new algo.Cache(onAllocate?, onFree?, options?)
onAllocate?onFree?options?
A function to be called when a queried entry does not exist. It receives the key of the entry and is supposed to return the value of that entry.
A function to be called when an entry is deleted. It receives 2 parameters: the key and the value of the entry being deleted.
Options including:
- size - Maximum number of entries allowed in the cache.
- ttl - Time-to-live for the entries in the cache. Can be a number in seconds or a string with one of the time unit suffixes such as 's', 'm' and 'h'.
Return Value
An empty Cache object.