Minimalist and performant Map object that is fully compatible with the ES6 Map object (it extends from it) whose values are deleted after a TTL of being set, like a cache.
npm install volatile-mapimport VolatileMap from 'volatile-map';
const cache = new VolatileMap(3000); // Values expire after 3 seconds
cache.set('foo', 'bar');
cache.set('baz', 'qux', 5000);
console.log(cache.get('foo'));
// => 'bar'
console.log(cache.get('baz'));
// => 'qux'
setTimeout(function () {
console.log(cache.get('foo'));
// => undefined
console.log(cache.get('baz'));
// => 'qux'
}, 4000);Constructor.
Type: Number
Time to live of the values and keys of the map, after that time, keys and values are deleted.
This method behaves the same as the ES6 Map.set() method, but adds an extra optional argument ttl that allows to set a specific time to live to each key. By default uses the ttl supplied when constructing the object.
See the ES6 Map object documentation to see all the methods.
Contributions are always welcome! Please run npm test beforehand to ensure everything is ok.
If you use this package please consider starring it :)