npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@forivall/promise-memoize

v1.0.1

Published

Memoize promise-returning functions. Includes cache expire and prefetch.

Downloads

7

Readme

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 @forivall/promise-memoize --save

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('@forivall/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