stale-multi-cache
Multi level cache that always prefers stale data
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 = ;var Redis = ;var redis = ; // Create the cachevar cache = max:500 redis; { // 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. { return cache;} // Now we can use the cachedGetUserapp; // We can also use the middleware to automatically cache responsesapp;app;
Stores
Store | Description |
---|---|
NoopStore |
Store that never stores anything, for testing purposes |
ErrorStore |
Store that never stores anything and errors on sets, for testing purposes |
SimpleMemoryStore |
Object store, for testing purposes |
LRUStore |
Memory store that uses lru-cache |
RedisStore |
Redis 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.