2.0.1 • Published 9 months ago

shared-libraries-nodejs-caching1 v2.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

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

  1. 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
          }]
      ]
      }
  2. 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:

  1. 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);
  1. 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)
  1. 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)
2.0.1

9 months ago

2.0.0

9 months ago

1.0.22

9 months ago

1.0.21

9 months ago

1.0.20

9 months ago

1.0.19

9 months ago

1.0.18

9 months ago

1.0.17

9 months ago

1.0.16

9 months ago

1.0.15

9 months ago

1.0.14

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago