Optional
config: AntCacheConfigif no arg passed in, use defaultAntCacheConfig
const DEFAULT_CHECK_PERIOD = 30;
export const defaultAntCacheConfig: AntCacheConfig = {
checkPeriod: DEFAULT_CHECK_PERIOD,
ttl: 2 * DEFAULT_CHECK_PERIOD,
maxKeys: 0,
};
Private
_checktrue
if config.checkPeriod
= 0
Private
_createdStore the created timestamp of every key.
Must be synced with the main cache every time a new value is set.
Private
_emitterThe event emitter
Private
_hitsCache hits
Private
_missesCache misses
Private
_timerThe interval timer
Private
_ttlStore the ttl of every key.
Must be synced with the main cache every time a new value is set.
Readonly
closeAlias for dispose
The config of the cache
Initialized once in the constructor, then read-only
The main store that interacts with the outside world.
Methods to get the cache info, like size
and keys
, apply on this.
Private
_delParse the input JSON string and attempt to upsert into the current cache.
JSON string should be the output of method serialize
.
Existing keys will be overwritten both value and TTL. Created timestamps will be reset.
Use with caution as the bigger the string is, the longer it takes to parse.
Leave logic handling long-running task up to you.
Retrieve value for key
undefined
if key
not found
Retrieve values of all keys
an object of { [key]: value }
Retrieve values of key groups
an object contains values of keys
Rest
...args: (string | string[])[]Private
handleAdds a listener at the end of the listeners array for the specified event
an event name comply to AntCacheEvent
a callback that will be call when the event emitted
Rest
...args: any[]Insert or update a key.
const cache = new AntCache({ ttl: 4, checkPeriod: 1 })
// use default TTL
cache.set('default ttl', 'live for 4 seconds');
// use custom TTL
cache.set('custom ttl', 'live for 10 seconds', 10);
// live permanently
cache.set('no ttl', 'this will live forever unless be deleted manually', 0);
should be string
in seconds, only take effect when inserting a new key. If ttl
= 0, there is no TTL at all
Retrieve cache stats
Get all values
an array contains all keys
Generated using TypeDoc
Get stated
ES Module
Common JS
Usage
Use cache with TTL
Use as an enhanced JS
Map
(no TTL)