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

promise-memorize

v1.1.0

Published

cache prmoise result for high-performance

Downloads

6

Readme

promise-memorize

Build Status Coverage Status npm Github Releases

Cache promise result for high-performance

Examples

View the ./examples directory for working examples.

Installation

$ npm install promise-memorize

API

memorize

  • hasher generate cache key, default is use the first parameter for key

  • ttl cache ttl, default is 0

return a new function for cache call, the function extends EventEmitter.

Cache promise for parallel call

const memorize = require('promise-memorize');
const fs = require('fs');
const path = require('path');
const readFile = function(file, encoding) {
  return new Promise((resolve, reject) => {
    fs.readFile(file, encoding, (err, data) => {
      console.info(`get file:${file} encoding:${encoding}`);
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

const readFileMemo = memorize(readFile);
const file = path.join(__dirname, './fs.js');
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
});
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  return readFileMemo(file);
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:39:52.663Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:39:52.666Z buf size:818
[info] 2016-05-26T14:39:52.667Z buf size:818
[info] 2016-05-26T14:39:52.668Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:39:52.668Z buf size:818

Cache promise with ttl

const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
});
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  return readFileMemo(file)
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:40:16.117Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:40:16.121Z buf size:829
[info] 2016-05-26T14:40:16.122Z buf size:829
[info] 2016-05-26T14:40:16.123Z buf size:829

Cache promise with hasher and ttl

const readFileMemo = memorize(readFile, (file, encoding) => {
  return `${file}-${encoding}`;
}, 10 * 1000);
const file = path.join(__dirname, './fs.js');
readFileMemo(file, 'utf8').then(buf => {
  console.info(`buf size:${buf.length}`);
});
readFileMemo(file, 'ascii').then(buf => {
  console.info(`buf size:${buf.length}`);
  return readFileMemo(file, 'ascii');
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:40:39.429Z get file:/promise-memorize/examples/fs.js encoding:utf8
[info] 2016-05-26T14:40:39.432Z buf size:912
[info] 2016-05-26T14:40:39.433Z get file:/promise-memorize/examples/fs.js encoding:ascii
[info] 2016-05-26T14:40:39.433Z buf size:912
[info] 2016-05-26T14:40:39.433Z buf size:912

setTTL

set the ttl for promise cache

const readFileMemo = memorize(readFile, 10 * 1000);
// 10000
console.info(readFileMemo.getTTL());
readFileMemo.setTTL(20 * 1000);
// 20000
console.info(readFileMemo.getTTL());

setStale

set the stale ttle for promise cache

const readFileMemo = memorize(readFile, 10 * 1000);
// 0
console.info(readFileMemo.getStale());
readFileMemo.setStale(2 * 1000);
// 2000
console.info(readFileMemo.getStale());

memorize.unmemorized

get original function

const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');
console.info(readFileMemo.unmemorized === readFile);
readFileMemo.unmemorized(file).then(buf => {
  console.info(`buf size:${buf.length}`);
});
readFileMemo.unmemorized(file).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:41:22.741Z true
[info] 2016-05-26T14:41:22.746Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:22.748Z buf size:821
[info] 2016-05-26T14:41:22.748Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:22.748Z buf size:821

get

get the cache promise item

  • key
const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo.on('hit', key => {
  // {promise: Promise, hit: 1, createdAt: 1469200404168}
  const item = readFileMemo.get(key);
});
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  return readFileMemo(file);
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});

delete

delete cache promise

  • key
const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  readFileMemo.delete(file);
  return readFileMemo(file);
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:41:41.459Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:41.461Z buf size:783
[info] 2016-05-26T14:41:41.463Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:41.463Z buf size:783

clear

const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  readFileMemo.clear();
  return readFileMemo(file);
}).then(buf => {
  console.info(`buf size:${buf.length}`);
});
[info] 2016-05-26T14:41:59.607Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:59.610Z buf size:778
[info] 2016-05-26T14:41:59.612Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:41:59.612Z buf size:778

size

const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  console.info(`memorize size:${readFileMemo.size()}`);
});
[info] 2016-05-26T14:42:16.935Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:42:16.937Z buf size:722
[info] 2016-05-26T14:42:16.938Z memorize size:1

periodicClear

Set interval to check whether the promise is expired in order to avoid memory leak and the function's cache won't be clear by global periodicClear.

  • interval chcek interval
const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo.periodicClear(1 * 1000);
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  console.info(`memorize size:${readFileMemo.size()}`);
});

setTimeout(() => {
  console.info(`memorize size:${readFileMemo.size()}`);
}, 12 * 1000);
[info] 2016-05-26T14:42:32.977Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:42:32.979Z buf size:851
[info] 2016-05-26T14:42:32.980Z memorize size:1
[info] 2016-05-26T14:42:44.981Z memorize size:0

events

Adds the listener function to the end of the listeners array for the event named eventName.

  • eventName The name of the event

  • listener The callback function

const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');

readFileMemo.on('hit', key => {
  console.info(`hit:${key}`);
});
readFileMemo.on('add', key => {
  console.info(`add:${key}`);
});
readFileMemo.on('delete', key => {
  console.info(`delete:${key}`);
});
readFileMemo.on('resolve', key => {
  console.info(`resolve:${key}`);
});
readFileMemo.on('reject', key => {
  console.info(`reject:${key}`);
});

readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  readFileMemo.delete(file);
  readFileMemo('a.js');
});
[info] 2016-05-26T14:43:02.699Z add:/promise-memorize/examples/fs.js
[info] 2016-05-26T14:43:02.703Z get file:/promise-memorize/examples/fs.js encoding:undefined
[info] 2016-05-26T14:43:02.705Z resolve:/promise-memorize/examples/fs.js
[info] 2016-05-26T14:43:02.705Z buf size:1004
[info] 2016-05-26T14:43:02.705Z delete:/promise-memorize/examples/fs.js
[info] 2016-05-26T14:43:02.706Z add:a.js
[info] 2016-05-26T14:43:02.706Z get file:a.js encoding:undefined
[info] 2016-05-26T14:43:02.707Z reject:a.js

periodicClear

global periodic clear for all memorize function, please call it before any promise-memorize(If use this).

memorize.periodicClear(60 * 1000);
const readFileMemo = memorize(readFile, 10 * 1000);
const file = path.join(__dirname, './fs.js');
// promise-memorize function can not call periodicClear because global periodicClear has been called.
// readFileMemo.periodicClear(1 * 1000);
readFileMemo(file).then(buf => {
  console.info(`buf size:${buf.length}`);
  console.info(`memorize size:${readFileMemo.size()}`);
});

License

MIT