npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

81

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

  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)