A simple memory storage based LRU algthorim.
npm install memory-storage
Intialize a LRUCache
instance.
- size {Number}: cache size, default is 10
const LRUCache = require('memory-storage');
const cache = new LRUCache();
or
const LRUCache = require('memory-storage');
const cache = new LRUCache(100);
Set <k, v> to cache.
- key {String}: key name
- value {Any}: To be cached value
cache.set('a', 1); // Set number
cache.set('a', {}); // Set object
cache.set('a', []); // Set array
cache.set('a', 'a'); // Set string
Set <k, v> to cache and add expire time(seconds).
- key {String}: key name
- value {Any}: to be cached value
- expire {Number}: expire seconds
cache.set('a', 1, 100);
Expire key in seconds
- key {String}: key name
- expire {Number}: expire seconds
cache.expire('a', 100);
Get value by key, return real value or undefined.
- key {String}: key name
cache.get('foo');
Push value
to named key
's array.
- key {String}: key name
- value {Any}: To be cached value
cache.rpush('a', 'a');
Pop value
from named key
's array and return it.
- key {String}: key name
cache.rpush('a', 1);
console.log(cache.rpop('a'));
jasmine
- support more apis like redis command.