https://github.com/vicanso/promise-memorize
Generate function for cache promise result
https://github.com/vicanso/promise-memorize
cache-promise
Last synced: about 2 months ago
JSON representation
Generate function for cache promise result
- Host: GitHub
- URL: https://github.com/vicanso/promise-memorize
- Owner: vicanso
- Created: 2016-04-04T13:07:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-06T12:38:24.000Z (over 8 years ago)
- Last Synced: 2025-08-01T05:28:51.479Z (2 months ago)
- Topics: cache-promise
- Language: JavaScript
- Size: 69.3 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
Awesome Lists containing this project
README
# promise-memorize
[](https://travis-ci.org/vicanso/promise-memorize)
[](https://coveralls.io/r/vicanso/promise-memorize?branch=master)
[](https://www.npmjs.org/package/promise-memorize)
[](https://github.com/vicanso/promise-memorize)Cache promise result for high-performance

## Examples
View the [./examples](examples) directory for working examples.
## Installation
```bash
$ 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
```js
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}`);
});
``````log
[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
```js
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}`);
});
``````log
[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
```js
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}`);
});
``````log
[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
```js
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
```js
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
```js
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}`);
});
``````log
[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`
```js
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`
```js
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}`);
});
``````log
[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
```js
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}`);
});
``````log
[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
```js
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()}`);
});
``````log
[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
```js
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);
``````log
[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
```js
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');
});
``````log
[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).
```js
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