Cache

Cache

Local cache with dogpile prevention, lru, ttl and other goodies

Constructor

new Cache()

Source:

Methods

(async, static) $(key, value, optionsopt) → {any}

Source:

alias for getOrSet

Parameters:
Name Type Attributes Default Description
key string
value any
options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

(async, static) clear() → {void}

Source:

clear the global cache

Returns:
Type
void

(static) clearSync() → {void}

Source:

clear the global cache

Returns:
Type
void

(async, static) del(key) → {void}

Source:

deletes a value from the global cache

Parameters:
Name Type Description
key string
Returns:
Type
void

(static) delSync(key) → {void}

Source:

deletes a value from the global cache

Parameters:
Name Type Description
key string
Returns:
Type
void

(async, static) get(key, defaultValue) → {any}

Source:

get value from global cache

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

(async, static) getOrSet(key, value, optionsopt) → {any}

Source:

get or set a value in the global cache

Parameters:
Name Type Attributes Default Description
key string
value any
options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

(static) getOrSetSync(key, value, optionsopt) → {any}

Source:

get or set a value in the global cache
this is sync version, so value should not be a promise or async function

Parameters:
Name Type Attributes Default Description
key string
value any
options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

(async, static) getStale(key, defaultValue) → {any}

Source:

gets a value from the cache immediately without waiting

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

(static) getStaleSync(key, defaultValue) → {any}

Source:

gets a value from the cache immediately without waiting

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

(static) getSync(key, defaultValue) → {any}

Source:

get value from global cache
this is sync version, so it'll not help with dogpiling issues

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

(static) globalCache() → {Cache}

Source:

returns a global cache instance

Returns:
Type
Cache

(async, static) has(key) → {boolean}

Source:

checks if value exists in global cache

Parameters:
Name Type Description
key string
Returns:
Type
boolean

(static) hasSync(key) → {boolean}

Source:

checks if value exists in global cache

Parameters:
Name Type Description
key string
Returns:
Type
boolean

(static) memoize(key, fn, optionsopt) → {function}

Source:

memoizes a function (caches the return value of the function)

Parameters:
Name Type Attributes Default Description
key string
fn function
options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:

memoized function

Type
function

(static) memoizeSync(key, fn, optionsopt) → {function}

Source:

memoizes a function (caches the return value of the function)
this is sync version, so fn should not be async

Parameters:
Name Type Attributes Default Description
key string
fn function
options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:

memoized function

Type
function

(async, static) set(key, value, optionsopt) → {Promise.<boolean>}

Source:

sets a value in the global cache

Parameters:
Name Type Attributes Default Description
key string
value any
options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
Promise.<boolean>

(static) setSync(key, value, optionsopt) → {boolean}

Source:

sets a value in the global cache
this is sync version, so value should not be a promise or async function

Parameters:
Name Type Attributes Default Description
key string
value any
options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
boolean

(async, static) size() → {number}

Source:
Returns:

the size of the global cache

Type
number

(static) sizeSync() → {number}

Source:
Returns:

the size of the global cache

Type
number

(async) $(key, value, optionsopt) → {any}

Source:

alias for getOrSet

Parameters:
Name Type Attributes Default Description
key string

key to get

value any

value to set if the key does not exist

options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

(async) clear() → {void}

Source:

clears the cache (deletes all keys)

Returns:
Type
void

clearSync() → {void}

Source:

clears the cache (deletes all keys)

Returns:
Type
void

(async) del(key) → {void}

Source:

deletes a value from the cache

Parameters:
Name Type Description
key string
Returns:
Type
void

delSync(key) → {void}

Source:

deletes a value from the cache

Parameters:
Name Type Description
key string
Returns:
Type
void

(async) gc()

Source:

delete expired items
NOTE: this method needs to loop over all the items (expensive)

gcSync()

Source:

delete expired items synchronously
NOTE: this method needs to loop over all the items (expensive)

(async) get(key, defaultValue)

Source:

gets a value from the cache

Parameters:
Name Type Description
key string
defaultValue any

(async) getOrSet(key, value, optionsopt) → {any}

Source:

gets a value from the cache, or sets it if it doesn't exist
this takes care of dogpiling (make sure value is a function to avoid dogpiling)

Parameters:
Name Type Attributes Default Description
key string

key to get

value any

value to set if the key does not exist

options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

getOrSetSync(key, value, optionsopt) → {any}

Source:

gets a value from the cache, or sets it if it doesn't exist
this is sync version, so value should not be a promise or async function

Parameters:
Name Type Attributes Default Description
key string

key to get

value any

value to set if the key does not exist

options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
any

(async) getStale(key, defaultValue) → {any}

Source:

gets a value from the cache immediately without waiting

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

getStaleSync(key, defaultValue) → {any}

Source:

gets a value from the cache immediately without waiting

Parameters:
Name Type Description
key string
defaultValue any
Returns:
Type
any

getSync(key, defaultValue)

Source:

gets a value from the cache
this is sync version, so it'll not help with dogpiling issues

Parameters:
Name Type Description
key string
defaultValue any

(async) has(key) → {boolean}

Source:

checks if a key exists in the cache

Parameters:
Name Type Description
key string
Returns:
Type
boolean

hasSync(key) → {boolean}

Source:

checks if a key exists in the cache

Parameters:
Name Type Description
key string
Returns:
Type
boolean

memoize(key, fn, optionsopt) → {function}

Source:

memoizes a function (caches the return value of the function)

const cachedFn = cache.memoize('expensiveFn', expensiveFn);
const result = cachedFn('a', 'b');
Parameters:
Name Type Attributes Default Description
key string

cache key with which to memoize the results

fn function

function to memoize

options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:

memoized function

Type
function

memoizeSync(key, fn, optionsopt) → {function}

Source:

memoizes a function (caches the return value of the function)

const cachedFn = cache.memoize('expensiveFn', expensiveFn);
const result = cachedFn('a', 'b');

This is sync version, so fn should not be async

Parameters:
Name Type Attributes Default Description
key string

cache key with which to memoize the results

fn function

function to memoize

options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:

memoized function

Type
function

(async) set(key, value, optionsopt) → {boolean}

Source:

sets a value in the cache
avoids dogpiling if the value is a promise or a function returning a promise

Parameters:
Name Type Attributes Default Description
key string
value any
options number | string | setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
boolean

setSync(key, value, optionsopt) → {Promise.<boolean>}

Source:

sets a value in the cache
this is sync version, so value should not be a promise or async function

Parameters:
Name Type Attributes Default Description
key string
value any
options setOpts <optional>
{}

ttl in ms/timestring('1d 3h') or opts (default: 0)

Returns:
Type
Promise.<boolean>

(async) size() → {number}

Source:

returns the size of the cache (no. of keys)
NOTE: expired items are returned as part of this count

Returns:
Type
number

sizeSync() → {number}

Source:

returns the size of the cache (no. of keys)
NOTE: expired items are returned as part of this count

Returns:
Type
number