module:cache
- Description:
Cache module for shared cache and subscriptions.
Some drivers (Redis) may support TTL so global options.ttl or local options.ttl can be used for put/incr operations and it will be honored if it is suported.
For caches that support maps, like Redis the 8options.mapName* can be used with get/put/incr/del to work with maps and individual keys inside maps.
All methods use options.cacheName for non-default cache. If it is an array then a client will be picked sequentially by maintaining internal sequence number.
Empty default client always exists, it can be overridden to make default some other driver
To enable stats collection for a cache it must be enabled with config: cache-redis-options-metrics=1
The class module:cache.CacheClient defines the methods that a driver may or may not implement.
The url query parameters that start with bk- will be extracted from the url and placed in the class *options object, this is a way to pass special properties without using cache-options, the rest of the url parameters will be passed to the driver.
- Source:
Example
cache-default=redis://
cache-redis=redis://?bk-enable_offline_queue=1
cache-config={ "limiter": "local://", "redis": "redis://" }
Classes
Members
(inner) args :Array.<ConfigOptions>
Methods
(async, static) aclear(pattern, optionsopt)
- Description:
Async version of module:cache.stats
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
pattern |
string | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.aclear("key:*");
(async, static) adel(key, optionsopt)
- Description:
Async version of module:cache.del
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
key |
string | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.adel("key");
(async, static) aget(key, optionsopt)
- Description:
Async version of module:cache.get
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
key |
string | Array.<string> | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.aget("key");
(async, static) aincr(key, val, optionsopt)
- Description:
Async version of module:cache.incr
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
key |
string | ||
val |
number | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.aincr("key", 10);
(async, static) alock(name, optionsopt)
- Description:
Async version of module:cache.lock
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name |
string | ||
options |
object |
<optional> |
Example
const { err, locked } = await cache.alock("key", { ttl: 1000 });
(async, static) aput(key, val, optionsopt)
- Description:
Async version of module:cache.put
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
key |
string | ||
val |
string | object | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.aput("key", { ... });
(async, static) astats(optionsopt)
- Description:
Async version of module:cache.stats
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
options |
object |
<optional> |
Example
const { err, data } = await cache.astats();
(async, static) aunlock(name, optionsopt)
- Description:
Async version of module:cache.unlock
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name |
string | ||
options |
object |
<optional> |
Example
const { err, data } = await cache.aunlock("key");
(static) checkConfig()
- Description:
Initialize missing or new clients, existing clients stay the same
- Source:
(static) checkLimiter(options)
- Description:
Keep checking the limiter until it is clear to proceed with the operation, if there is no available tokens in the bucket it will wait and try again until the bucket is filled.
The callback will receive the same arguments as module:cache.limiter. options._retries will be set to how many times it tried.
- Source:
Parameters:
| Name | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
options |
object | same as in module:cache.limiter Properties
|
(static) clear(pattern, optionsopt, callbackopt)
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
pattern |
string | ||
options |
object |
<optional> |
|
callback |
function |
<optional> |
(static) createClient()
- Description:
Return a new client for the given host or null if not supported
- Source:
(static) del(key, optionsopt, callbackopt)
- Description:
Delete an item by key(s), if key is an array all keys will be deleted at once atomically if supported
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | ||||||||||||||
options |
object |
<optional> |
Properties
|
||||||||||||
callback |
function |
<optional> |
Example
cache.del("my:key")
cache.del("key1", { mapName: "my:map" })
cache.del("*", { mapName: "my:map" })
cache.del("1", { listName: "my:list" })
(static) get(key, optionsopt, callback)
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | Array.<string> | If the key is an array then it returns an array with values for each key, for non existent keys an empty string will be returned. For maps only if the key is * it will return the whole object, otherwise only value(s) are returned. |
|||||||||||||||||||||||||||||
options |
object |
<optional> |
Properties
|
||||||||||||||||||||||||||||
callback |
function |
Example
cache.get(["my:key1", "my:key2"], function(err, data) { console.log(data) });
cache.get("my:key", function(err, data) { console.log(data) });
cache.get("my:counter", { set: 10 }, function(err, data) { console.log(data) });
cache.get("*", { mapName: "my:map" }, function(err, data) { console.log(data) });
cache.get("key1", { mapName: "my:map" }, function(err, data) { console.log(data) });
cache.get(["key1", "key2"], { mapName: "my:map" }, function(err, data) { console.log(data) });
cache.get(["key1", "key2"], { listName: "my:list" }, function(err, data) { console.log(data) });
cache.get("", { listName: "my:list", del: 1 }, function(err, data) { console.log(data) });
cache.get("", { listName: "my:list", del: 1, ttl: 30000 }, function(err, data) { console.log(data) });
(static) getClient(optionsopt) → {module:cache.CacheClient}
- Description:
Return a cache client by name if specified in the options or use default client which always exists, use cacheName to specify a specific driver. If it is an array it will rotate items sequentially.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
options |
object |
<optional> |
Returns:
| Type | Description |
|---|---|
| module:cache.CacheClient |
(static) incr(key, val, optionsopt, callbackopt)
- Description:
Increase/decrease a counter in the cache by val, non existent items are treated as 0, if a callback is given an error and the new value will be returned.
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | Array.<string> |
|
|||||||||||||||||
val |
int | object |
|
|||||||||||||||||
options |
object |
<optional> |
Properties
|
||||||||||||||||
callback |
function |
<optional> |
Example
cache.incr("my:key", 1)
cache.incr("counter", 1, { mapName: "my:map" })
cache.incr("my:map", { count: 1, name: "aaa", mtime: Date.now().toString() })
cache.incr("", { count: 1, name: "bbb", mtime: Date.now().toString() }, { mapName: "my:map" })
cache.incr("", { "my-key": 1, counter2: 2 })
cache.incr([ "my:key", "my:counter"], 1 }, { ttl: [1000, 30000] })
(static) initClients()
- Description:
Reinitialize a client for cache purposes, previous client will be closed.
- Source:
(static) limiter(options, callback)
- Description:
Check for rate limit using the default or specific cache, by default TokenBucket using local LRU cache is used unless a client provides its own implementation.
- Source:
Parameters:
| Name | Type | Description | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
Properties
|
||||||||||||||||||||||||||||||||
callback |
function | takes 2 arguments:
|
(static) localLimiter(msg) → {object}
- Description:
Uses local limiter returns the same message with consumed set to 1 or 0
- Source:
Parameters:
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
msg |
object |
Properties
|
Returns:
| Type | Description |
|---|---|
| object |
(static) lock(name, optionsopt, callbackopt)
- Description:
Implementation of a lock with optional ttl, only one instance can lock it, can be for some period of time and will expire after timeout.
This is intended to be used for background job processing or something similar when only one instance is needed to run. At the end of the processing module.cache.unlock must be called to enable another instance immediately, otherwise it will be available after the ttl only.
The callback must be passed which will take an error and a boolean value, if true is returned it means the timer has been locked by the caller, otherwise it is already locked by other instance. In case of an error the lock is not supposed to be locked by the caller.
- Source:
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name |
string | unique lock name |
|||||||||||||||||
options |
object |
<optional> |
Properties
|
||||||||||||||||
callback |
function |
<optional> |
Example
cache.lock("my-lock", { ttl: 60000, timeout: 30000 }, (err, locked) => {
if (locked) {
...
cache.unlock("my-lock");
}
});
(static) put(key, val, optionsopt, callbackopt)
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | ||||||||||||||||||||||
val |
string | object | ||||||||||||||||||||||
options |
object |
<optional> |
Properties
|
||||||||||||||||||||
callback |
function |
<optional> |
Example
cache.put("my:key", 2)
cache.put("my:key", 1, { setmax: 1 })
cache.put("key1", 1, { mapName: "my:map" })
cache.put("*", { key1: 1, key2: 2 }, { mapName: "my:map" })
cache.put("", [1,2,3], { listName: "my:list" }, (err, rc) => { })
(static) shutdown()
(static) stats(optionsopt, callbackopt)
- Description:
Returns the cache statistics, the format depends on the cache type used
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
options |
object |
<optional> |
|
callback |
function |
<optional> |
(static) unlock(name, optionsopt, callbackopt)
- Description:
Unconditionally unlock the lock by key, any client can unlock any lock.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
name |
string | ||
options |
object |
<optional> |
|
callback |
function |
<optional> |