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

exp-asynccache

v3.2.0

Published

A small async cache

Downloads

4,362

Readme

asynccache

An async cache with a lookup function per key for node js with a different interface than async-cache.

Errors are not cached and the callback function is always called asynchronously even if the value is resolved synchronously.

Installation

npm install --save exp-asynccache

Usage

Callback usage:

var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();

cache.lookup("foo", function (resolve) {
  // Find foo asynchronously (or synchronously) then call resolve with the value
  resolve(null, "baz");
}, function (err, value) {
  console.log(value); // value will be "baz"
});

Promise usage:

var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();

var hit = cache.lookup("foo", function (resolve) {
  resolve(null, "baz");
});

hit.then(function (value) {
  console.log(value); // value will be "baz"
});

By default a lru-cache cache with default settings is used to store cached objects but you can provide your own.

var AsyncCache = require("exp-asynccache");
var LRU = require("lru-cache"); // any lru-cache compatible cache will do

var cache = new AsyncCache(new LRU({
  max: 500,
  maxAge: 1000 * 60 * 60
}));

The resolve function can take more arguments than error and key. It will pass these to the underlying cache's set method. So when using lru-cache you can provide a max age per key:

var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();

cache.lookup("foo", function (resolve) {
  resolve(null, "baz", 1000); // Let foo live for one second
});

Cache interface

The undelying cache should adhere to the same interface as LRUCache:

get(key)        -> lookup value. undefined return value indicates cache miss.
set(key, value) -> set value
has(key)        -> return true if key exists

If the cache impmlemntation is asynchronous, promises can be returned

Warning

Don't use more data from the closure than what is used to construct the cache key:

var AsyncCache = require("exp-asynccache");
var cache = new AsyncCache();

function getPerson(name, location, callback) {

  cache.lookup(name, function (resolve) { // <-- Only name is used

    personRepo.get(name, location, resolve); // <-- Both name and location is used

  }, callback);

}

In the above example there might be several different objects returned by personRepo for the same name but with different locations but they are all cached only by the name. The correct code would be to construct the cache key from both name and location.