1.0.6 • Published 7 years ago
frivillig-cache v1.0.6
In-memory cache module
Save stuff in-memory!
Summary
Simple cache for saving object in-memory. Each value gets a timestamp, which marks how long the value is fresh. Freshness defaults to one hour.
Options
Pass in a freshnessDuration
to set how long a value is fresh in the cache.
The cache uses moment
to handle the timestamps. The parameter is an object literal created using the add() method, as shown in MomentJS docs
const cache = require('frivillig-cache')({ year: 1, seconds: 12 });
Tests
Use npm test
to run the tests. Remember to install the dependencies first using npm install
.
Example usage
Below is a short example of how you can use this cache.
const cache = require('frivillig-cache')();
cache.set('key', { foo: bar });
if (cache.hasFresh('key')) {
console.log(`Reading a fresh value from cache: ${cache.get('key')}`);
} else if (cache.has('key')) {
console.log(`Reading a stale value from cache: ${cache.get('key')}`);
}