安装
yarn add memory-cache
使用
var cache = require('memory-cache');
// now just use the cache
cache.put('foo', 'bar');
console.log(cache.get('foo'));
// that wasn't too interesting, here's the good part
cache.put('houdini', 'disappear', 100, function(key, value) {
console.log(key + ' did ' + value);
}); // Time in ms
console.log('Houdini will now ' + cache.get('houdini'));
setTimeout(function() {
console.log('Houdini is ' + cache.get('houdini'));
}, 200);
// create new cache instance
var newCache = new cache.Cache();
newCache.put('foo', 'newbaz');
setTimeout(function() {
console.log('foo in old cache is ' + cache.get('foo'));
console.log('foo in new cache is ' + newCache.get('foo'));
}, 200);
console.log(cache.keys())
image.png
var cache = require('memory-cache')
cache.put('name', 'jack', 60000, function (key, value) {
console.log(key, value)
})
setTimeout(function () {
console.log(cache.get('name'))
console.log(cache.keys())
}, 60000)
/**
* 执行结果:
*
* name jack
* null
* []
*
*/
cache.put(key: any, value: any, time, timeoutCallback)
- Simply stores a value
- If time isn't passed in, it is stored forever
- Will actually remove the value in the specified time in ms (via setTimeout)
- timeoutCallback is optional function fired after entry has expired with key and value passed (function(key, value) {})
- Returns the cached value
cache.get(key)
- Retrieves a value for a given key
- If value isn't cached, returns null
cache.del(key)
- Deletes a key, returns a boolean specifying whether or not the key was deleted
cache.clear()
- Deletes all keys
cache.size()
- Returns the current number of entries in the cache
cache.memsize()
- Returns the number of entries taking up space in the cache
- Will usually == size() unless a setTimeout removal went wrong
cache.debug()
- Turns on or off debugging
cache.hits()
- Returns the number of cache hits (only monitored in debug mode)
cache.misses()
- Returns the number of cache misses (only monitored in debug mode)
cache.keys()
- Returns all the cache keys
cache.exportJson()
- Returns a JSON string representing all the cache data
- Any timeoutCallbacks will be ignored
cache.importJson(json: string, options: {skipDuplicates: boolean})
- Merges all the data from a previous call to export into the cache
- Any existing entries before an import will remain in the cache
- Any duplicate keys will be overwritten, unless skipDuplicates is true
- Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
- Available options:
- skipDuplicates: If true, any duplicate keys will be ignored when importing them. Defaults to false.
- Returns the new size of the cache
cache.Cache()
- Cache constructor
- note that require('cache') would return the default instance of Cache
- while require('cache').Cache is the actual class
网友评论