volatile-map
Version:
ES6 Map object whose values are deleted after a TTL
71 lines (47 loc) • 2.7 kB
Markdown
[](https://www.npmjs.com/package/volatile-map)
[](https://github.com/alvarocastro/volatile-map/actions?query=workflow%3Abuild)
[](https://codeclimate.com/github/alvarocastro/volatile-map/maintainability)
[](https://coveralls.io/github/alvarocastro/volatile-map?branch=master)
[](https://bundlephobia.com/result?p=volatile-map)
[](https://github.com/xojs/xo)
[](https://github.com/semantic-release/semantic-release)
Minimalist and performant `Map` object that is fully compatible with the [ES6 `Map` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) (it extends from it) whose values are deleted after a TTL of being set, like a cache.
- [Install](
- [Usage](
- [Contributing](
- [Support](
```bash
npm install volatile-map
```
```js
import 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set), 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) 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 :)