promise-memorize
v1.1.0
Published
cache prmoise result for high-performance
Downloads
6
Maintainers
Readme
promise-memorize
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 keyttl
cache ttl, default is0
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 eventlistener
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