GithubHelp home page GithubHelp logo

lru-memoizer's Introduction

Memoize functions results using an lru-cache.

Installation

npm i lru-memoizer --save

Intro

This module uses an lru-cache internally to cache the results of an async function.

The load function can have N parameters and the last one must be a callback. The callback should be an errback (first parameter is err).

The hash function purpose is generate a custom hash for storing results. It has all the arguments applied to it minus the callback, and must return an string synchronous.

The disable function allows you to conditionally disable the use of the cache. Useful for test environments.

The freeze option (defaults to false) allows you to deep-freeze the result of the async function.

The clone option (defaults to false) allows you to deep-clone the result every time is returned from the cache.

Usage

var memoizer = require('lru-memoizer');

var memoizedGet = memoizer({
  //defines how to load the resource when
  //it is not in the cache.
  load: function (options, callback) {
    request.get(options, callback);
  },

  //defines how to create a cache key from the params.
  hash: function (options) {
    return options.url + qs.stringify(options.qs);
  },

  //don't cache in test environment
  disable: isTestEnv(),

  //all other params for the LRU cache.
  max: 100,
  maxAge: 1000 * 60
});

memoizedGet({
  url: 'https://google.com',
  qs: { foo: 123 }
}, function (err, result, body) {
 //console.log(body);
})

Sync lru-memoizer

Use memoizer.sync to cache things that are slow to calculate or methods returning promises.

var memoizer = require('lru-memoizer');
var memoizedGet = memoizer.sync({
  //defines how to load the resource when
  //it is not in the cache.
  load: function (params) {
    //return something_hard_to_compute;s
  },

  //defines how to create a cache key from the params.
  hash: function (params) {
    return params.foo;
  },

  //all other params for the LRU cache.
  max: 100,
  maxAge: 1000 * 60
});

Similar modules

This module is very similar to async-cache, the main difference is the hash function.

License

MIT 2016 - José F. Romaniello

lru-memoizer's People

Contributors

abhi347 avatar coreylight avatar darkyen avatar dependabot[bot] avatar elbuo8 avatar jfromaniello avatar jmcabrera avatar josh-cain avatar panga avatar sandrinodimattia avatar santiagoaguiar avatar vanhumbeecka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lru-memoizer's Issues

cb type should expect error as Error | undefined

Current type for load function's cb is:

export type INodeStyleCallBack<SuccessArg> = (err: Error, result?: SuccessArg) => void;

But the err can also be null to indicate that the callback has no errors. If trying to invoke the callback with null or undefined, typescript complains with:

Argument of type 'null' is not assignable to parameter of type 'Error'.ts(2345)

I think the type should be

export type INodeStyleCallBack<SuccessArg> = (err: Error | null, result?: SuccessArg) => void;

See for example in node types package:

    export function open(path: PathLike, flags: OpenMode | undefined, mode: Mode | undefined | null, callback: (err: NodeJS.ErrnoException | null, fd: number) => void): void;

"@types/lru-cache" should be a regular dependency

Not all options are available in TypeScript out of the box at the moment, e.g.

memoizer({
    itemMaxAge: 1337,
    max: 666 // not available without adding @types/lru-cache to dev dependencies
});

This is because IParamsBase is extending LRU.Options, which is only in the dev dependencies.

nextjs usage fails in middleware functions because of dynamic functions

@jfromaniello

NextJS builds fails to use it in middleware function..
Not sure if updating the lodash clonedeep will resolve this.
Perhaps clonedeep can be now replaced with structuredClone https://developer.mozilla.org/en-US/docs/Web/API/structuredClone

../../node_modules/.pnpm/[email protected]/node_modules/lodash.clonedeep/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime 
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

Import trace for requested module:
../../node_modules/.pnpm/[email protected]/node_modules/lodash.clonedeep/index.js
../../node_modules/.pnpm/[email protected]/node_modules/lru-memoizer/lib/async.js
../../node_modules/.pnpm/[email protected]/node_modules/lru-memoizer/lib/index.js
../../node_modules/.pnpm/[email protected]/node_modules/jwks-rsa/src/wrappers/cache.js
../../node_modules/.pnpm/[email protected]/node_modules/jwks-rsa/src/wrappers/index.js
../../node_modules/.pnpm/[email protected]/node_modules/jwks-rsa/src/JwksClient.js
../../node_modules/.pnpm/[email protected]/node_modules/jwks-rsa/src/index.js

lru-cache 5

Hi 👋

I'm wondering if the lru-cache dependency can be upgraded to 5.0.0 or if there's a specific reason to stick with ~4.0.0? If not I'd be happy to submit a PR with the update. Would you be willing to do this in a new major?

Return type of exported function has or is using name 'IMemoized' from external module "lru-memoizer" but cannot be named

Error in new Typescript 3.4:

clwy-node-jwks-rsa/wrappers/cache.ts(7,17): error TS4058: Return type of exported function has or is using name 'IMemoized' from external module "lru-memoizer" but cannot be named.

Example (from clwy-node-jwks-rsa):

export function cacheSigningKey(client: JwksClient, cacheMaxEntries: number = 5, options = {cacheMaxAge: ms('10h') }) {
  const logger = debug('jwks');
  const getSigningKey = client.getSigningKey.bind(client);

  logger(`Configured caching of singing keys. Max: ${cacheMaxEntries} / Age: ${options.cacheMaxAge}`);

  return memoizer({
    load: (kid, callback) => {
      getSigningKey(kid, (err, key) => {
        if (err) {
          return callback(err);
        }

        logger(`Caching signing key for '${kid}':`, key);
        return callback(null, key);
      });
    },
    hash: kid => kid,
    maxAge: options.cacheMaxAge,
    max: cacheMaxEntries
  });
}

As interfaces aren't really exported in typings - typescript guesses return type correctly as IMemoized here but cannot really use it.

So, as temporary fix

export function cacheSigningKey(client: JwksClient, cacheMaxEntries: number = 5, options = {cacheMaxAge: ms('10h') }): any {

works, but type info is obviously lost

Syntax error when run on Heroku

I added jwks-rsa to my project, and it seems to have included this module. When I try to run it on Heroku, I get this error:

/app/node_modules/jwks-rsa/node_modules/lru-memoizer/index.js:18
2017-05-22T23:49:15.508350+00:00 app[web.1]:       _.extend(load, { del }, options);
2017-05-22T23:49:15.508350+00:00 app[web.1]:                            ^
2017-05-22T23:49:15.508351+00:00 app[web.1]: SyntaxError: Unexpected token }

Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.