// Create broker
const broker = new ServiceBroker({
cacher: "Memory"
});
// Create a service
broker.createService({
name: "users",
actions: {
list: {
// Enable caching to this action
cache: true,
handler(ctx) {
this.logger.info("Handler called!");
return [
{ id: 1, name: "John" },
{ id: 2, name: "Jane" }
]
}
}
}
});
The syntax of key is:
The params object can contain properties that are not relevant for
the cache key. Also, it can cause performance
issues if the key is too long. Therefore it is recommended to set an
object for cacheproperty which contains a list of
name: "posts",
actions: {
handler(ctx) {
concatenated params in the key with maxParamsLengthcacher option.
When the key is longer than the configured
limit value, the cacher calculates a hash (SHA256) from the full key
and adds it to the end of the key.
Generate a limited-length key
Conditional caching allows to bypass the cached response and execute
an action in order to obtain “fresh” data. To bypass the cache set
ctx.meta.$cache to falsebefore calling an action.
Example of turning off the caching for the greeter.hello
action
To overwrite the built-in cacher key generator, set your own function
as keygenin cacher options.
}
When you create a new model in your service, you have to clear the
old cached model entries.
Example to clean the cache inside actions
Example
module.exports = {
name: "users",
actions: {
create(ctx) {
// Create new user entity
const user = new User(ctx.params);
cleanCache() {
// Broadcast the event, so all service instances receive it (including
this instance).
this.broker.broadcast("cache.clean.users");
}
}
To make it easier, create a CacheCleanermixin and define in the
dependent services schema.
cache.cleaner.mixin.js
if (this.broker.cacher) {
this.logger.debug(`Clear local '${this.name}' cache`);
events
Cache locking
Moleculer also supports cache locking feature. For detailed info
.
Disable Lock
Example for Redis cacher with redlock library
const broker = new ServiceBroker({
prefix: "MOL",
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 10,
// the time in ms between attempts
retryDelay: 200, // time in ms
Enable memory cacher
Options
|
|
|
|
|
Boolean or Function |
|
|
|
|
|
Custom cache key generator function.
|
|
|
|
Maximum length of params in generated keys.
|
|
|
|
|
instead of a Boolean.
Custom clone function with JSON parse &
stringify
Enable LRU cacher
Redis cacher
RedisCacher is a built-in based distributed cache module. It uses
library.
With options
With MessagePack serializer
You can define a serializer for Redis Cacher. By default, it uses the
JSON serializer.
host: "my-redis"
}
}
}
nodes: [
{ port: 6380, host: "127.0.0.1" },
{ port: 6381, host: "127.0.0.1" },
{ port: 6382, host: "127.0.0.1" }
],
});
Options
|
|
|
|
|
|
|
Time-to-live in seconds. Disabled: 0 or null
|
|
|
|
Enable Redis client . If enabled, every client operation will be
logged (on debug level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Redis Cluster client configuration.
|
|
Boolean or Object |
|
Enable lock feature.
|
|
|
|
|
implement the get, set, del and clean methods.
Create custom cacher