GithubHelp home page GithubHelp logo

isabella232 / exp-asynccache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bonniernews/exp-asynccache

0.0 0.0 0.0 125 KB

A pluggable async cache for Node.JS

License: MIT License

JavaScript 100.00%

exp-asynccache's Introduction

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.

exp-asynccache's People

Contributors

gabrielf avatar andolf avatar theneubeck avatar nordbergm avatar sajmoon avatar norla avatar spathon avatar love-expressen avatar carsotho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.