RedisCache

RedisCache

Cache backed by Redis

Constructor

new RedisCache(prefix, optionsopt)

Source:
Parameters:
Name Type Attributes Default Description
prefix string
options Redis | RedisCacheOptions <optional>
{}

Cache Options
NOTE: These options can also be set on a global level

Methods

(static) bypass(bypassopt)

Source:
See:

bypass the cache and compute value directly (useful for debugging / testing)
NOTE: RedisCache.bypass will turn on bypassing for all instances of RedisCache
For bypassing a particular instance, use instance.bypass()

Parameters:
Name Type Attributes Default Description
bypass boolean <optional>
true

default true

(async, static) getLocalCacheStats() → {array}

Source:

return local cache stats of all caches

Returns:
Type
array

(static) getRedis(redisConf) → {Array.<Redis>}

Source:
Parameters:
Name Type Description
redisConf RedisConf
Returns:

array of redis instances (size 2), first for data & second for pubsub

Type
Array.<Redis>

(static) isBypassed() → {boolean}

Source:

gets whether the cache is bypassed or not

Returns:
Type
boolean

(static) subscribe(redisConf)

Source:
Parameters:
Name Type Description
redisConf redisConf

(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 number | string | setRedisOpts <optional>
{}

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

Returns:
Type
any

attachArray(key, mapKey) → {Array}

Source:

attach and return a local array to the redis cache key
the local array would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
Returns:
Type
Array

attachCache(key, mapKey, options) → {Cache}

Source:

attach and return a local cache object to the redis cache key
the local cache would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
options object

options for the cache object

Returns:
Type
Cache

attachCustom(key, mapKey, func) → {any}

Source:

attach and return a custom object to the redis cache key
custom object should be returned by the func
the object would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
func function

functions that returns the object

Returns:
Type
any

attachLRU(key, mapKey, options) → {LRU}

Source:

attach and return a local lru map to the redis cache key
the local lru map would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
options object

options for the lru map

Returns:
Type
LRU

attachMap(key, mapKey) → {Map}

Source:

attach and return a local map to the redis cache key
the local map would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
Returns:
Type
Map

attachObject(key, mapKey) → {object}

Source:

attach and return a local object to the redis cache key
the local object would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
Returns:
Type
object

attachSet(key, mapKey) → {Set}

Source:

attach and return a local set to the redis cache key
the local set would be deleted if redis cache key gets deleted

Parameters:
Name Type Description
key string
mapKey string
Returns:
Type
Set

bypass(bypassopt)

Source:

bypass the cache and compute value directly (useful for debugging / testing)
NOTE: this'll be only useful in getOrSet or memoize, get will still return from cache

Example
let i = 0;
const cache = new RedisCache();
await cache.getOrSet('a', () => ++i); // => 1
await cache.getOrSet('a', () => ++i); // => 1 (returned from cache)
cache.bypass(); // turn on bypassing
await cache.getOrSet('a', () => ++i); // => 2 (cache bypassed)
await cache.getOrSet('a', () => ++i); // => 3 (cache bypassed)
cache.bypass(false); // turn off bypassing
await cache.getOrSet('a', () => ++i); // => 1 (previous cache item)
Parameters:
Name Type Attributes Default Description
bypass boolean <optional>
true

whether to bypass the cache or not

(async) clear()

Source:

clears the cache (deletes all keys)
NOTE: this method is expensive, so don't use it unless absolutely necessary

(async) del(key)

Source:

deletes a value from the cache

Parameters:
Name Type Description
key string | Array.<string>

(async) delContains(str) → {number}

Source:

delete everything from cache if the key includes a particular string
to delete everything from cache, use _all_ as string

Parameters:
Name Type Description
str string
Returns:

number of keys deleted

Type
number

deleteAllAttached(key)

Source:

delete all attached objects to a key

Parameters:
Name Type Description
key string

deleteAttached(key, mapKey)

Source:

delete an attached object to a key

Parameters:
Name Type Description
key string
mapKey string

(async) get(key, defaultValueopt, optionsopt) → {Promise.<any>}

Source:

gets a value from the cache

Parameters:
Name Type Attributes Description
key string
defaultValue any <optional>
options getRedisOpts <optional>
Returns:
Type
Promise.<any>

getLocal(key, defaultValueopt)

Source:

gets a value from the local cache without touching redis

Parameters:
Name Type Attributes Description
key string
defaultValue any <optional>

(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 number | string | setRedisOpts <optional>
{}

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

Returns:
Type
any

(async) getStale(key, defaultValueopt, optionsopt) → {Promise.<any>}

Source:

gets a value from the cache immediately without waiting

Parameters:
Name Type Attributes Description
key string
defaultValue any <optional>
options getRedisOpts <optional>
Returns:
Type
Promise.<any>

(async) has(key) → {boolean}

Source:

checks if a key exists in the cache

Parameters:
Name Type Description
key string
Returns:
Type
boolean

isBypassed() → {boolean}

Source:

gets whether the cache is bypassed or not

Returns:
Type
boolean

(async) markDirty(key)

Source:

set the key as dirty (will cause staleTTL to recompute in background)

Parameters:
Name Type Description
key string

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

Source:

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

Example
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 | setRedisOpts <optional>
{}

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

Returns:
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 | setRedisOpts <optional>
{}

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

Returns:
Type
boolean

(async) size() → {number}

Source:

NOTE: this method is expensive, so don't use it unless absolutely necessary

Returns:

the size of the cache (no. of keys)

Type
number