https://github.com/alvarocastro/volatile-map
ES6 Map object whose values are deleted after a TTL
https://github.com/alvarocastro/volatile-map
cache map map-cache storage volatile
Last synced: 9 months ago
JSON representation
ES6 Map object whose values are deleted after a TTL
- Host: GitHub
- URL: https://github.com/alvarocastro/volatile-map
- Owner: alvarocastro
- License: mit
- Created: 2021-02-11T01:34:27.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-05T09:58:23.000Z (about 3 years ago)
- Last Synced: 2025-04-12T06:43:56.783Z (about 1 year ago)
- Topics: cache, map, map-cache, storage, volatile
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/volatile-map
- Size: 649 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# VolatileMap
[](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](#install)
- [Usage](#usage)
- [Contributing](#contributing)
- [Support](#support)
## Install
```bash
npm install volatile-map
```
## Usage
```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);
```
### 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](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.
#### Other methods
See the [ES6 `Map` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) 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 :)