https://github.com/mrhooray/ttl
Simple in-memory cache for JavaScript
https://github.com/mrhooray/ttl
cache in-memory javascript ttl ttl-cache
Last synced: 18 days ago
JSON representation
Simple in-memory cache for JavaScript
- Host: GitHub
- URL: https://github.com/mrhooray/ttl
- Owner: mrhooray
- Created: 2014-10-21T07:25:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-11-01T08:43:00.000Z (over 5 years ago)
- Last Synced: 2025-03-25T17:22:40.404Z (about 1 month ago)
- Topics: cache, in-memory, javascript, ttl, ttl-cache
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 12
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ttl [](https://travis-ci.org/mrhooray/ttl)
> Simple in-memory cache for JavaScript## Installation
```shell
$ npm install ttl --save
```## Usage
```js
var Cache = require('ttl');
var cache = new Cache({
ttl: 10 * 1000,
capacity: 3
});cache.on('put', function(key, val, ttl) { });
cache.on('del', function(key, val) { });
cache.on('drop', function(key, val, ttl) { });
cache.on('hit', function(key, val) { });
cache.on('miss', function(key) { });cache.put('foo', 'bar');
cache.put('ping', 'pong', 20 * 1000);
cache.put('yo', 'yo', 30 * 1000);
cache.put('whats', 'up'); // emit 'drop' eventcache.get('foo'); // > 'bar'
cache.get('yo'); // > 'yo'
cache.get('ping'); // > 'pong'
cache.get('lol'); // > undefined// after 10 seconds
cache.get('foo'); // > undefined
cache.size(); // > 2
cache.del('ping') // > 'pong'
cache.get('ping'); // > undefined
cache.size(); // > 1
cache.clear();
cache.size(); // > 0
```## License
MIT