1.0.2 • Published 5 years ago

clean-cache v1.0.2

Weekly downloads
131
License
MIT
Repository
github
Last release
5 years ago

Clean Cache

Build Status Coverage Status dependencies Status

NPM

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