shared-libraries-nodejs-caching1
v2.0.1
Published
Cache the output of a Service function for the specified ttl. For example, @cached(10) adding this decorator on a function will cache the o/p for 10 seconds.
Downloads
8
Readme
Introduction
Cache the output of a Service function for the specified ttl. For example: Adding @cached(10) as a decorator on a function will cache result of the function for 10 seconds.
Getting Started
Installation process
- Install shared-libraries-nodejs-caching in your application by executing the command below,
npm install shared-libraries-nodejs-caching
- Add a file named .babelrc on the root of your application and add the content below,
{ "presets": [ "@babel/preset-env" ], "plugins": [ ["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }] ] }
- Install shared-libraries-nodejs-caching in your application by executing the command below,
Usage Add @cached(ttlInSeconds:number) as a decorator to the function you want to cache
Adding @cached(10) as mentioned below, as a decorator to the function, will cache the result for 10 seconds
import { cached } from "shared-libraries-nodejs-caching-abc"; export default class Service1 { greet() { return 'Hello1'; } say(toSay) { console.log(toSay) return 'The app said ' + toSay } @cached(10) getCurrentCities() { return [ { id: 1, name: "New York", }, { id: 2, name: "Berlin", }, { id: 3, name: "London", }, { id: 4, name: Date(), }, ]; } }
Adding @cached(10) as mentioned below, as a decorator to the class, will cache the results of all the functions in the class for 10 seconds
import { cached } from "shared-libraries-nodejs-caching-abc"; @cached(10) export default class Service1 { greet() { return 'Hello1'; } say(toSay) { console.log(toSay) return 'The app said ' + toSay } getCurrentCities() { return [ { id: 1, name: "New York", }, { id: 2, name: "Berlin", }, { id: 3, name: "London", }, { id: 4, name: Date(), }, ]; } }
Cache Key
The cache key attribute is used to specify which argument passed to the service should be used to compute the cache key. For example, of an agrument with a model below, the field 'kind' can be considered as key for Single key strategy and 'colors' can be used for Multiple key strategy.
class Car {
kind = "hatch";
colors = ['red', 'green'];
}
Key Strategy
Determines how the cache entry keys are computed. There are three available options:
- Request The Request strategy generates a cache key based on the service's name without requiring a request argument. This suits services without arguments or when arguments don't impact cache validity.
[Cached(5, 'request')]
Task<NoKeyResponse> DoSomethingWithoutKey(NoKeyRequest value, CancellationToken cancellationToken = default);
- Single The Single strategy uses a specified input argument as the caching key.
[Cached(5, 'single', 'kind')]
Task<SingleKeyResponse> DoSomething(SingleKeyRequest value, CancellationToken cancellationToken = default)
- Multiple The Multiple strategy allows for a list or an array of input arguments to be used as the caching key.
[Cached(5, 'multiple', 'colors')]
Task<MultiKeyResponse> DoSomethingWithMultiKeys(MultiKeyRequest value, CancellationToken cancellationToken = default)