ProductPromotion
Logo

Node.JS

made by https://0x3d.site

GitHub - nodeca/promise-memoize: Memoize promise-returning functions. Includes cache expire and prefetch.
Memoize promise-returning functions. Includes cache expire and prefetch. - nodeca/promise-memoize
Visit Site

GitHub - nodeca/promise-memoize: Memoize promise-returning functions. Includes cache expire and prefetch.

GitHub - nodeca/promise-memoize: Memoize promise-returning functions. Includes cache expire and prefetch.

promise-memoize

Build Status NPM version Coverage Status

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

npm install promise-memoize --save

(*) IE9 and below will require setTimeout polyfill for correct work.

Usage example

// 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

Articles
to learn more about the nodejs concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about NodeJS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory