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

@axa/bautajs-decorator-cache

v2.3.0

Published

A bautaJS cache decorator

Downloads

6

Readme

BautaJS cache decorator

A cache decorator using moize for Bauta.js pipelines.

How to install

  npm install @axa/bautajs-decorator-cache

Usage

Include it on your pipeline as follows:

  import { pipe, createContext } from '@axa/bautajs-core';
  import { cache } from '@axa/bautajs-decorator-cache';

  function createAKey(prev, ctx, bautajs) {
   ctx.data.myKey = 'mykey';
  }

  function doSomethingHeavy(prev, ctx, bautajs) {
   let acc = 0;
   for(let i=0; i < 1000000000; i++) {
     acc += i;
   }

   return acc;
  }

  const myPipeline = pipe(
   createAKey,
   doSomethingHeavy
  );

  const cacheMyPipeline = cache(myPipeline, (prev, ctx) => ctx.data.myKey, { maxSize:3 });

  const result = await cacheMyPipeline(null, createContext({req:{}}), {});
  console.log(result);
  • Cache only accept executable pipeline (pipe) as a first parameter
  • Normalize should use a synchronous function to improve performance and it should be quick (O(1)) to make sure that there are no performance penalties.

Normalize

Normalize functions must return an identifier key in the form of a primitive type that is used to determine if a new value requires cache or not. If you need to use more than one field to generate a key, concatenate or stringify those fields that you need.

There are two main use cases in normalize:

  • you want to use only fields from the context to generate the key
  • you want to use at least one field from a previously generated object

Normalize with only context fields

It is straightforward and you can do the following:

const normalizer = (_, ctx) => ctx.whatever_field;

Normalize uses at least one field from a previously generated object

This is trickier because you have to take into account that you will not have the result of the pipeline to be passed as the object to be used as the key. For example, this will not work:

  const { pipe } = require('@axa/bautajs-core');
  const { cache } = require('@axa/bautajs-decorator-cache');
  const { someHeavyOperation } = require('./my-helper');

  const myPipeline = pipe( someHeavyOperation, (result) => ({...result, iWantToUseAsKeyThis:1}))

  module.exports = resolver((operations)=> {
      const normalizer = (value) => value.iWantToUseAsKeyThis;
      operations.v1.op1.setup(p =>
        p.pipe(
            cache(
                myPipeline,
                normalizer, // When normalizer is called, result from pipeline is not yet there
                { maxSize:5 }
            )
        )
    );
  })

This will not work because iWantToUseAsKeyThis is the result of the pipeline that you are trying to cache and it is not executed yet when the cache decorator is being called. Thus, the normalizer instruction will result in an error.

To use the object as a key in the cache normalizer, this object needs to be set in the previous pipeline of the cache, like in the following example:

        const normalizer = (prev) => {
          return prev.iAmTheKey;  // prev may be a primitive
        };

        const pp = pipe((_, ctx) => {
              return 'test';
            },
            value => ({ a: '123', b: value }),
            result => ({ ...result, new: 1 })
          )

        bautaJS.operations.v1.operation2.setup(p =>
          p
            .pipe((_, ctx) => {
              return { iAmTheKey: 'test' };
            },
            cache(pp, normalizer, { maxSize: 5 })
        );

In here you can see that we have an decorator function that returns an object with a key iAmTheKey that is passed to normalizer previous to the decorator function of the cache.

Legal Notice

Copyright (c) AXA Group. All rights reserved. Licensed under the (MIT / Apache 2.0) License.

Third party dependencies licenses

Production

Development