Skip to content

Multiple named cachers #1

@icebob

Description

@icebob
  • Start Date: 2019-09-04
  • Target Version: 0.14 or 0.15
  • Reference Issues: -
  • Implementation PR: -

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:

  • implementation cost, both in term of code size and complexity
  • whether the proposed feature can be implemented in user space
  • the impact on teaching people Vue
  • integration of this feature with other existing and planned features
  • cost of migrating existing Vue applications (is it a breaking change?)

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions