moleculerjs / rfcs

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

Service-level cacher #4

Open icebob opened 4 years ago

icebob commented 4 years ago

Summary

We should add a new service-level cacher solution besides broker-based cacher. It can be useful when you don't want to use caching in all services, only just a few. In this case, you don't define cacher in broker options but in service schema instead.

Nevertheless, you can use different cacher implementations for different services.

Basic example

// Create broker
const broker = new ServiceBroker({
    cacher: false
});

broker.createService({
    name: "greeter",
    // Service-level LRU cacher
    cacher: {
        type: "MemoryLRU",
        options: {
            max: 100,
            ttl: 3
        }
    },
    actions: {
        hello: {
            cache: true,
            handler(ctx) {
                return `Hello ${ctx.params.name}`;
            }
        }
    }
});

broker.createService({
    name: "products",
    // Service-level Redis cacher
    cacher: {
        type: "Redis",
        options: {
            ttl: 30
        }
    },
    actions: {
        list: {
            cache: true,
            handler(ctx) {
                return [];
            }
        }
    }
});

Detailed design

Drawbacks

Alternatives

Adoption strategy

Unresolved questions