Cache
Source: index.js:36
A tiny in-memory, least-recently-used (LRU) cache.
This class is a demo of how clean-jsdoc-theme renders a documented class: the signature, the constructor, properties, and each method get their own section with parameters, return types, and examples.
Example
Basic usage
const cache = new Cache({ maxSize: 2 });
cache.set('a', 1);
cache.set('b', 2);
cache.get('a'); // => 1- See:
Constructor
Parameters
options(CacheOptions, optional, default: "{}") — Configuration for the cache.
Instance Methods
set(key, value) -> Cache
Store a value under a key. If the cache is full, the least-recently-used entry is evicted first.
Parameters
key(string) — The key to store the value under.value(*) — The value to cache.
Returns
Cache— The cache instance, for chaining.
Example
cache.set('user:1', { name: 'Ada' }).set('user:2', { name: 'Linus' });get(key) -> *
Look up a value by key, marking it as recently used.
Parameters
key(string) — The key to look up.
Returns
*— The cached value, orundefinedif the key is not present.
has(key) -> boolean
Check whether a key is present without affecting its recency.
Parameters
key(string) — The key to test.
Returns
boolean—trueif the key is cached.
clear() -> void
Remove every entry from the cache.
Returns
void
Instance Fields
maxSize
The maximum number of entries this cache will hold.
Type
number
ttl
Time-to-live per entry, in milliseconds. 0 means entries never expire.
Type
number
size
The number of entries currently stored.