Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nodeca/promise-memoize
Memoize promise-returning functions. Includes cache expire and prefetch.
https://github.com/nodeca/promise-memoize
Last synced: 1 day ago
JSON representation
Memoize promise-returning functions. Includes cache expire and prefetch.
- Host: GitHub
- URL: https://github.com/nodeca/promise-memoize
- Owner: nodeca
- License: mit
- Created: 2016-05-20T15:41:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-05T12:15:57.000Z (about 6 years ago)
- Last Synced: 2024-04-14T04:22:26.953Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 58
- Watchers: 5
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs-cn - promise-memoize - 拥有过期时间和预加载功能的 Promise 返回值缓存工具 (包 / 流程控制)
- awesome-nodejs - promise-memoize - Memoize promise-returning functions, with expire and prefetch. (Repository / Control flow)
- awesome-nodejs-cn - promise-memoize - **star:62** 使用过期和预取来记忆承诺返回函数 (包 / 控制流)
- awesome-nodejs - promise-memoize - Memoize promise-returning functions, with expire and prefetch. (Packages / Control flow)
- awesome-nodejs - promise-memoize - Memoize promise-returning functions. Includes cache expire and prefetch. - ★ 37 (Control flow)
- awesome-node - promise-memoize - Memoize promise-returning functions, with expire and prefetch. (Packages / Control flow)
README
# promise-memoize
[![Build Status](https://img.shields.io/travis/nodeca/promise-memoize/master.svg?style=flat)](https://travis-ci.org/nodeca/promise-memoize)
[![NPM version](https://img.shields.io/npm/v/promise-memoize.svg?style=flat)](https://www.npmjs.org/package/promise-memoize)
[![Coverage Status](https://coveralls.io/repos/github/nodeca/promise-memoize/badge.svg?branch=master)](https://coveralls.io/github/nodeca/promise-memoize?branch=master)> Memoize promise-returning functions. Includes cache expire and prefetch.
- When data expire mode enabled, new values are fetched in advance. Cache
will be always valid, without "gaps".
- Prefetch happens only for items in use. Inactive ones will be GC-ed as usual.
- Errors are not cached
- You still can enable cache with separate expire time for errors, to avoid
specific peak loads. For example, set 120s for good result and 1s on fail.## Install
```bash
npm install promise-memoize --save
```(\*) IE9 and below will require [setTimeout polyfill](https://developer.mozilla.org/docs/Web/API/WindowTimers/setTimeout)
for correct work.## Usage example
```js
// Pseudo code
let db = require('mongoose').createConnection('mongodb://localhost/forum');function lastPosts(limit) {
return db.model('Post').find().limit(limit).orderBy('-_id').lean(true).exec(); // <- Promise
}let cachedLastPosts = require('promise-memoize')(lastPosts, { maxAge: 60000 });
// Later...
cachedLastPosts(10).then(posts => console.log(posts));
```## API
### promiseMemoize(fn [, options]) -> memoizedFn
Memoize function `fn`.
- **fn(params...)** — function, returning a promise (or any "thenable").
It can have any number of arguments, but arguments should be uniquely
castable to strings (see below).
- **options** — options for memoization (optional)
- **maxAge** — an amount of milliseconds it should cache resolved
values for (default: `Infinity`, i.e. cache forever).
- **maxErrorAge** — an amount of milliseconds it should cache
rejected values for (default: `0`, i.e. don't cache).
- **resolve** — serialiser to build unique key from `fn` arguments.
(default: `simple`). Possible values:
- `simple` (string) — convert each param to string & join those.
- `json` (string) — JSON.stringify each param & join results.
- function(Array) — custom function, with `fn` params as array on input
- `[ String, Boolean, 'json', function ]` — array with custom functions,
specific for each `fn` param position (text shortcuts as above are allowed).Return value is a function with the same signature as *fn*.
**Note. How prefetch works.**
If `maxAge` used and request to cached data happens after `0.7 * maxAge` time, then:
- cached data returned
- `fn` call is executed in parallel
- cached data will be substituted with new one on success, timeouts will be extended.So your application will not have to wait for data fetch after cache expire.
### memoizedFn(params...) -> promise
Returns result as cached promise (errors are not cached by default). If `maxAge`
used, tries to prefetch new value before expire to replace cache transparently.### memoizedFn.clear()
Remove all cached data.
## License
[MIT](https://github.com/nodeca/promise-memoize/blob/master/LICENSE)