3.0.0 • Published 6 years ago

stale-multi-cache v3.0.0

Weekly downloads
59
License
BSD-2-Clause
Repository
github
Last release
6 years ago

stale-multi-cache

Multi level cache that always prefers stale data

Version Build Status Coverage License

The middleware functionality was ported from apicache. If you don't need the other features of this caching library that is a more full-featured library.

Usage

This library is undergoing API changes at the moment. We will follow semantic versioning so once we change the publicly exposed methods you'll see a jump to 2.0.0 and beyond.

Right now the main cache.js file needs a lot of cleanup, messy and too many responsibilities. However the functionality of wrap and middleware are highly tested and probably good to go.

var Cache = require('stale-multi-cache');
var Redis = require('ioredis');
var redis = new Redis();

// Create the cache
var cache = new Cache([
  new Cache.LRUMemoryStore({max:500}),
  new Cache.RedisStore(redis)
]);

function getUser(user) {
  // really really slow function
  return {};
}

// Even with a stale TTL of 10, this cache will always send stale data,
// but do a background update of the data after the data expires.
// We're setting the expire TTL to 86400 at that point we'll HAVE to refresh.
function cachedGetUser(user) {
  return cache.wrap('user:'+user, function() {
    return getUser(user);
  }, {
    staleTTL: 10,
    expireTTL: 86400
  });
}

// Now we can use the cachedGetUser
app.get('/user', function(req, res) {
  return cachedGetUser('kelsin').then(function(user) {
    return res.json({user:user});
  });
});

// We can also use the middleware to automatically cache responses
app.use(cache.middleware());
app.get('/cached', function(req, res) {
  // some long response
});

Stores

StoreDescription
NoopStoreStore that never stores anything, for testing purposes
ErrorStoreStore that never stores anything and errors on sets, for testing purposes
SimpleMemoryStoreObject store, for testing purposes
LRUStoreMemory store that uses lru-cache
RedisStoreRedis store that uses any ioredis compatible client

Why?

I often need caches that are double buffered but where TTL is more of an update interval than a hard expire. I want to use LRU based stores and update at set times but always keep stale data. I never want most web caches to expire. I'd rather have old data on the site than no data on the site. This cache provides that.

3.0.1-SNAPSHOT.1

6 years ago

3.0.0

6 years ago

3.0.0-SNAPSHOT.6

6 years ago

3.0.0-SNAPSHOT.5

6 years ago

3.0.0-SNAPSHOT.4

6 years ago

3.0.0-SNAPSHOT.3

6 years ago

3.0.0-SNAPSHOT.2

6 years ago

3.0.0-beta.1

6 years ago

3.0.0-beta.0

6 years ago

2.1.1

6 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.0

7 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago