lru-memoizer
Memoize functions results using an lru-cache.
Last updated 3 months ago by jfromaniello .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install lru-memoizer 
SYNC missed versions from official npm registry.

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 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);
  },

  //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

Current Tags

  • 2.1.2                                ...           latest (3 months ago)

23 Versions

  • 2.1.2                                ...           3 months ago
  • 2.1.1                                ...           3 months ago
  • 2.1.0                                ...           5 months ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.13.0                                ...           a year ago
  • 1.12.0                                ...           2 years ago
  • 1.11.2                                ...           2 years ago
  • 1.11.1                                ...           3 years ago
  • 1.11.0                                ...           3 years ago
  • 1.10.1                                ...           4 years ago
  • 1.10.0                                ...           4 years ago
  • 1.9.0                                ...           4 years ago
  • 1.8.0                                ...           4 years ago
  • 1.7.0                                ...           4 years ago
  • 1.6.0                                ...           4 years ago
  • 1.5.0                                ...           4 years ago
  • 1.4.0                                ...           4 years ago
  • 1.3.1                                ...           4 years ago
  • 1.3.0                                ...           4 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago

Copyright 2014 - 2016 © taobao.org |