memoize-cache
A configurable cache support for functions (https://www.npmjs.com/package/async-deco). It contains 2 different implementations.
- ram-cache: is a lightweight yet complete implementation of an in-ram cache. Suitable for using it in the browser
- cache: it uses a cache manager object to store data in memory/redis/mongodb/file system (https://www.npmjs.com/package/cache-manager)
ram-cache
The constructor takes an option object with 3 optional attributes:
- key: a function used to extract the cache key (used in the push and query method for storing, retrieving the cached value). The key returned should be a string or it will be converted to JSON and then md5. Default: a function returning a fixed key.
- maxAge: the maximum age of the item stored in the cache (in ms). Default: Infinity
- maxLen: the maximum number of items stored in the cache. Default: Infinity. Cache items will be purged using an LRU algorithm
Example:
var Cache = require('memoize-cache/ram-cache');
var cache = new Cache();
var cache = new Cache({key: function (config){
return config.id;
}}, maxLen: 100, maxAge: 20000);
cache
The constructor takes an cache-manager object and an optional "key" function. The function will be used to extract the cache key (used in the push and query method for storing, retrieving the cached value). The key returned should be a string or it will be converted to JSON and then md5. Default: a function returning a fixed key.
Example:
var
var Cache = require('memoize-cache/cache');
var cacheManager = require('cache-manager');
var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 20});
var cache = new Cache(memoryCache, function (config){
return config.id;
});
Methods
Pushing a new cached value
cache.push(args, output);
"args" is an array containing the arguments passed to the function that generated the output.
Querying for cache hit
cache.query(args, function (err, result){
});
"args" is an array containing the arguments passed to the function that generated the output.
resetting the cache
This is implemented only on ram-cache.
cache.reset();
getting the number of item cached
This is implemented only on ram-cache.
cache.len();
getting the size of the cache
This is implemented only on ram-cache.
cache.size(true);
cache.size(false);
If the first argument is true the output will be pretty printed.