0.0.2 • Published 9 years ago
lazycache v0.0.2
lazycache
lazy inmemory cache for node, automatic reset cache asynchronously when expired
Install
npm i lazycache --save
Usage
lazyCache()
Use the factory method to init a cache object:
var defaultCacheTime = 300000; // 300000 ms = 5 min
var cache = require('lazyCache')(defaultCacheTime);
if defaultCacheTime < 0
, a noCache
object will return, which offer the same API but do NOT cache anything.
cache.get(key)
Get value from cache
- key: cache key
cache.set('key', 'my cache value');
cache.set(key, val , cacheTime)
Set value into cache
- key: cache key
- val: cache value
- cacheTime: cache time (ms)
- reseter: a function which reset the
cache value
var reseter = function (cb) {
// async
setTimeout(function () {
cb(null, 'new value'); // `null` for NO error
}, 30);
};
cache.set('key', 'initial value', reseter)