clean-cache
v1.0.2
Published
Simple single-file, in-memory cache.
Downloads
621
Readme
Clean Cache
Simple single-file, in-memory javascript cache fully tested and without any dependencies.
Initially part of statg.
Install
npm install clean-cache
Use
const { Cache } = require('clean-cache');
const myCache = new Cache(30000); // ttl in ms, if omitted defaults to 60s
// adding items
myCache.add('1', { foo: 'bar' }); // adds object under key '1' for 30s
myCache.add('2', { foo: 'bar' }, 1000); // expires in 1s instead of 30s
myCache.add(null, { foo: 'bar' }); // throws error
myCache.add(undefined, { foo: 'bar' }); // throws error
// retrieving items
myCache.retrieve('1'); // returns { foo: 'bar' }
myCache.retrieve('non-existent'); // returns null
myCache.retrieve(null); // throws error
myCache.retrieve(undefined); // throws error
// other
myCache.count(); // returns number of objects in cache
myCache.tidy(); // removes all expired items from cache
License
MIT