Moleculer has a built-in caching solution. To enable it:
- Set the
cacherbroker option. - Set the
cache: truein action definition.
let { ServiceBroker } = require("moleculer"); |
Console messages:
[2017-08-18T13:04:33.845Z] INFO dev-pc/BROKER: Broker started. |
Cache keys
The cacher creates keys by service name, action name, and the hash of params of context.
The syntax of key is:
<serviceName>.<actionName>:<parameters or hash of parameters> |
So if you call the posts.list action with params { limit: 5, offset: 20 }, the cacher calculates a hash from the params. So the next time, when you call this action with the same params, it will find the entry in the cache by key.
// Example hashed cache key for "posts.find" action |
However, the hash generation is an expensive operation. Therefore it is recommended to set an object for cache property which contains a list of essential parameter names under the keys property.
{ |
PerformanceThis solution is pretty fast, so we recommend to use it in production instead of hashing.
Cache meta keys
To use meta keys in cache keys use the # prefix.
broker.createService({ |
TTL
You can override the cacher default TTL setting in action definition.
let broker = new ServiceBroker({ |
Custom key-generator
You can change the built-in cacher keygen function to your own one.
let broker = new ServiceBroker({ |
Manual caching
You can also use the cacher module manually. Just call the get, set, del methods of broker.cacher.
// Save to cache |
Clear cache
When you create a new model in your service, sometimes you have to clear the old cached model entries.
Example to clean the cache inside actions
{ |
Clear cache among multiple service instances
The best practice to clear cache entries among multiple service instances is that use broadcast events.
Example
module.exports = { |
Clear cache among different services
Common way is that your service depends on other ones. E.g. posts service stores information from users service in cached entries.
Example cache entry in posts service
{ |
So the author field is received from users service. So if the users service clears cache entries, the posts service has to clear own cache entries as well. Therefore you should also subscribe to the cache.clear.users event in posts service.
To make it better, create a CacheCleaner mixin and define in constructor the dependent services.
cache.cleaner.mixin.js
module.exports = function(serviceNames) { |
posts.service.js
const CacheCleaner = require("./cache.cleaner.mixin"); |
With this solution if the users service emits a cache.clean.users event, the posts service will also clear the own cache entries.
Built-in cachers
Memory cacher
MemoryCacher is a built-in memory cache module. It stores entries in the heap memory.
const broker = new ServiceBroker({ |
Redis cacher
RedisCacher is a built-in Redis based distributed cache module. It uses ioredis library.
const broker = new ServiceBroker({ |
DependenciesTo be able to use this cacher, install the
ioredismodule with thenpm install ioredis --savecommand.
Custom cacher
You can also create your custom cache module. We recommend to copy the source of MemoryCacher or RedisCacher and implement the get, set, del and clean methods.
Use custom cacher
const { ServiceBroker } = require("moleculer"); |