node-cache-deep
Node.js cache library with deep clone
Installation
npm install cache-deep --save
Note: cache-deep requires Node.js version >= 6.x
Features
- deep clone option
Usage
- normal
const CacheDeep = require('cache-deep');
const cache = new CacheDeep();
cache.put('data', 123, 1000 * 60 * 20); // put 123 to key 'data' and 20 minutes to live
let data = cache.get('data'); // data = 123
cache.del('data');
data = cache.get('data'); // data = null
cache.clear();
- with deep-clone option
const CacheDeep = require('cache-deep');
const cache = new CacheDeep({
deepClone: true
});
let obj = {
a: 1
};
cache.put('data', obj);
let data = cache.get('data').a; // data = 1
obj.a = 2;
data = cache.get('data').a; // data = 1
API
new CacheDeep([options])
Added in: v1.0.0
Cache constructor
Param:
-
options <Object> cache config options
- deepClone <boolean> if true, use the lodash.deepClone to clone to cached value
Returns:
instance of cache
const CacheDeep = require('cache-deep');
const cache = new CacheDeep({
deepClone: true
});
put(key, value, ttl)
Added in: v1.0.0
pput a value to the cache by key
Param:
- key <string> cache key to save the value to cache
- value <*> the value want to cached
- ttl <number> time to live, the cache should be delete after this millisecond, if not given, keep the cache forever
Returns:
undefined
Throws:
- TypeError when the key is not string
cache.put('data', 123, 1000 * 60 * 20); // put 123 to key 'data' and 20 minutes to live
get(key)
Added in: v1.0.0
get a cached value by key
Param:
- key <string> cache key to get the value
Returns:
cached value, or null if not hit
Throws:
- TypeError when the key is not string
let data = cache.get('data');
del(key)
Added in: v1.0.0
delete a cached value by key
Param:
- key <string> cache key to delete the cache
Returns:
undefined
Throws:
- TypeError when the key is not string
cache.del('data');
clear()
Added in: v1.0.0
empty the cache
cache.clear();
statistics()
Added in: v1.1.0
get the cache hit rate info
Returns:
- <Object>
- count <number> count of get cache
- hit <number> the number of get cache hit the right key
const stat = cache.statistics();
const cacheHitRate = stat.hit / stat.count;