moleculerjs / rfcs

Request-For-Comments collection for Moleculer microservices framework
https://moleculer.services
MIT License
2 stars 1 forks source link

Multiple named cachers #1

Open icebob opened 5 years ago

icebob commented 5 years ago

Summary

Add multiple cacher configuration feature.

Examples

Example to define two cacher.

// moleculer.config.js
module.exports = {
    nodeID: "node-1",

    cachers: [
        "Memory",
        {
            type: "Redis",
            options: {
                ttl: 30
            }
        }
    ]
};

Access to the cachers:

// Backward compatible mode
this.broker.cacher.get(); // use the first cacher, in this case, the "Memory" cacher.

// New named path. Name comes from the lowercased type of cacher.
this.broker.cachers.memory.get(); 
this.broker.cachers.redis.get();

Example to define two named cacher.

// moleculer.config.js
module.exports = {
    nodeID: "node-1",

    cachers: [
        {
            type: "Redis",
            options: {
                name: "first",
                ttl: 30,
                redis: {
                    db: 0
                }
            }
        },
        {
            type: "Redis",
            options: {
                name: "second",
                ttl: 3600,
                redis: {
                    db: 1
                }
            }
        }
    ]
};

Access to the cachers:

this.broker.cachers.first.get(); 
this.broker.cachers.second.get();

Usage in services

//posts.service.js
module.exports = {
    name: "posts",

    actions: {
        list: {
            // It will use the `this.broker.cachers.memory`
            cache: {
                cacher: "memory"
            }
            handler(ctx) {
                /// ...
            }
        },

        find: {
            cache: {
                // Set cacher by name
            // It will use the `this.broker.cachers.redis`
                cacher: "redis",
                keys: ["limit", "offset", "#user.id"]
            },
            handler(ctx) {
                /// ...
            }
        }
    }
}

Motivation

In the big projects, certain services need a centralized Redis cacher but some simple services need only a local memory cacher. So, it would be good to add capabilities to ServiceBroker to support multiple cacher configuration.

Detailed design

This is the bulk of the RFC. Explain the design in enough detail for somebody familiar with Vue to understand, and for somebody familiar with the implementation to implement. This should get into specifics and corner-cases, and include examples of how the feature is used. Any new terminology should be defined here.

Drawbacks

Why should we not do this? Please consider:

There are tradeoffs to choosing any path. Attempt to identify them here.

Alternatives

What other designs have been considered? What is the impact of not doing this?

Adoption strategy

If we implement this proposal, how will existing developers adopt it? Is this a breaking change? Can we write a codemod? Can we provide a runtime adapter library for the original API it replaces?

Unresolved questions

Optional, but suggested for first drafts. What parts of the design are still TBD?