volatile-map

ES6 Map object whose values are deleted after a TTL

Usage no npm install needed!

<script type="module">
  import volatileMap from 'https://cdn.skypack.dev/volatile-map';
</script>

README

VolatileMap

NPM Build status Maintainability status Coverage status Bundle size Code style: XO Release: Semantic

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.

Install

npm install volatile-map

Usage

const VolatileMap = require('volatile-map').default;

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);

VolatileMap([ttl = 600000])

Constructor.

ttl

Type: Number

Time to live of the values and keys of the map, after that time, keys and values are deleted.

VolatileMap.set(key, value[, ttl])

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.

Other methods

See the ES6 Map object documentation to see all the methods.

Contributing

Contributions are always welcome! Please run npm test beforehand to ensure everything is ok.

Support

If you use this package please consider starring it :)