Download as:
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Language:EN
Pages: 17

Offset and meta user the cache key will posts

// 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" }
]
}
}
}
});

[2017-08-18T13:04:33.849Z] INFO dev-pc/BROKER: Users count from cache: 2

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) {

// If params is { limit: 10, offset: 30 } and meta is { user: { id: 123 } },

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.

const broker = new ServiceBroker({

}

// Get from cache (async)

// Clean all entries

// set values in cache

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

// Clear all cache entries

// Delete an entry

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");
}
}

_id: 1,

avatar: "https://..."

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`);

return {

events

name: "posts",

//...

Cache locking

Moleculer also supports cache locking feature. For detailed info .

lock: true, // Set to true to enable cache locks. Default is disabled.

lock: {

Disable Lock

enable: false, // Set to false to disable.

Example for Redis cacher with redlock library

const broker = new ServiceBroker({

prefix: "MOL",

// set Time-to-live to 30sec.

// 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

const broker = new ServiceBroker({

cacher: true

ttl: 30// Set Time-to-live to 30sec. Disabled: 0 or null

Options

Name

Default
Number
clone
Boolean or Function
keygen
null

Custom cache key generator function.

Number

Maximum length of params in generated keys.

lock

instead of a Boolean.

Custom clone function with JSON parse & stringify

}

Enable LRU cacher

cacher: {

ttl: 3

Redis cacher

RedisCacher is a built-in based distributed cache module. It uses library.

cacher: "Redis"

With options

With MessagePack serializer
You can define a serializer for Redis Cacher. By default, it uses the JSON serializer.

host: "my-redis"
}
}
}

cluster: {

nodes: [
{ port: 6380, host: "127.0.0.1" },
{ port: 6381, host: "127.0.0.1" },
{ port: 6382, host: "127.0.0.1" }
],

});

Options

Default
String
ttl
null

Time-to-live in seconds. Disabled: 0 or null

Boolean

Enable Redis client . If enabled, every client operation will be logged (on debug level)

redis
null
Function
maxParamsLength
null
String
cluster
null

Redis Cluster client configuration.

Boolean or Object
null

Enable lock feature.

Number

implement the get, set, del and clean methods.

Create custom cacher

async del(key) { /*...*/ }

cacher: new MyCacher()

Copyright © 2009-2023 UrgentHomework.com, All right reserved.