A finite key-value cache support multi cache algorithm (LRU, FIFO, LFU...).
strategy | description | todo |
---|---|---|
fifo | First in First out (FIFO) | |
lru | Least Recently Used (LRU) | |
lru-k | Least Recently Used K (LRU-K) | todo |
2q | Two queues (2Q) | todo |
lfu | Least Frequently Used (LFU) | |
... | ... | ... |
$ npm install x-cache --save
import {LruXCache, XCache} from 'x-cache';
const cache = new LruXCache(1000);
// or
var XCache = require('x-cache').XCache;
var cache = new XCache('lru', {
limit: 1000
});
cache.put('elwin', 1215);
cache.put('leon', 1233);
cache.put('tom', 1234);
cache.get('elwin'); // 1215
cache.find('elwin'); // true
cache.remove('elwin'); // true
cache.getSize(); // 2
cache.getArray(); // [1234, 1233]
cache.shift();
cache.forEach((v, k, obj) => console.log(v, k)); // 1234, tom
cache.removeAll();
Create a new cache instance.
- strategy: cache algorithm, such as lru, fifo..., you can see support cache algorithm for detail.
- option: it is not always same. if you use lru strategy, the option is {limit: 1000}.
Create a new cache instance using LRU cache algorithm.
- limit: limit the size of cache
Create a new cache instance using LFU cache algorithm.
- limit: limit the size of cache
Create a new cache instance using FIFO cache algorithm.
- limit: limit the size of cache
Put the value into the cache associated with key.
- key:
- value:
Returns the value associated with key or undefined if not in the cache.
- key:
Such as array.shift().
Check the key is whether in cache or not.
- key:
Remove the value associated with key, return true if remove successful.
- key:
Clear the cache.
Returns the current size of the cache.
Returns array {key, value}.
- fromHead: default false
Iterator, callback(key, value, obj).
- fromHead: default false
$ npm install
$ npm run build
$ npm run test
If you any questions, use Issues.
Sina Weibo: @赵小小峰
MIT Licence.