Memoize promise-returning functions. Includes cache expire and prefetch.
npm install promise-memoize --save
(*) IE9 and below will require setTimeout polyfill for correct work.
// 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));promiseMemoize(fn [, options]) -> memoizedFn
Memoize function fn
.
Infinity
, i.e. cache forever).0
, i.e. don't cache).fn
arguments. (default: simple
). Possible values:
simple
(string) — convert each param to string & join those.json
(string) — JSON.stringify each param & join results.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:
fn
call is executed in parallelSo your application will not have to wait for data fetch after cache expire.
memoizedFn(params...) -> promiseReturns result as cached promise (errors are not cached by default). If maxAge
used, tries to prefetch new value before expire to replace cache transparently.
Remove all cached data.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4