Methods
(async, static) $(key, value, optionsopt) → {any}
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}
clear the global cache
Returns:
- Type
- void
(static) clearSync() → {void}
clear the global cache
Returns:
- Type
- void
(async, static) del(key) → {void}
deletes a value from the global cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- void
(static) delSync(key) → {void}
deletes a value from the global cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- void
(async, static) get(key, defaultValue) → {any}
get value from global cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string | |
defaultValue |
any |
Returns:
- Type
- any
(async, static) getOrSet(key, value, optionsopt) → {any}
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}
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}
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}
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}
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}
returns a global cache instance
Returns:
- Type
- Cache
(async, static) has(key) → {boolean}
checks if value exists in global cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- boolean
(static) hasSync(key) → {boolean}
checks if value exists in global cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- boolean
(static) memoize(key, fn, optionsopt) → {function}
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}
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>}
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}
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}
Returns:
the size of the global cache
- Type
- number
(static) sizeSync() → {number}
Returns:
the size of the global cache
- Type
- number
(async) $(key, value, optionsopt) → {any}
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}
clears the cache (deletes all keys)
Returns:
- Type
- void
clearSync() → {void}
clears the cache (deletes all keys)
Returns:
- Type
- void
(async) del(key) → {void}
deletes a value from the cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- void
delSync(key) → {void}
deletes a value from the cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- void
(async) gc()
delete expired items
NOTE: this method needs to loop over all the items (expensive)
gcSync()
delete expired items synchronously
NOTE: this method needs to loop over all the items (expensive)
(async) get(key, defaultValue)
gets a value from the cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string | |
defaultValue |
any |
(async) getOrSet(key, value, optionsopt) → {any}
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}
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}
gets a value from the cache immediately without waiting
Parameters:
Name | Type | Description |
---|---|---|
key |
string | |
defaultValue |
any |
Returns:
- Type
- any
getStaleSync(key, defaultValue) → {any}
gets a value from the cache immediately without waiting
Parameters:
Name | Type | Description |
---|---|---|
key |
string | |
defaultValue |
any |
Returns:
- Type
- any
getSync(key, defaultValue)
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}
checks if a key exists in the cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- boolean
hasSync(key) → {boolean}
checks if a key exists in the cache
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
Returns:
- Type
- boolean
memoize(key, fn, optionsopt) → {function}
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}
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}
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>}
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}
returns the size of the cache (no. of keys)
NOTE: expired items are returned as part of this count
Returns:
- Type
- number
sizeSync() → {number}
returns the size of the cache (no. of keys)
NOTE: expired items are returned as part of this count
Returns:
- Type
- number