Node Memoization/Caching Library
A memoization and caching library for NodeJS.
Installation
$ npm install memo-cache
Usage
var memoCache = require('memo-cache');
API
####Caching:
#####memoCache.cache.create(cacheName, options)
cacheName
String - name of the cacheoptions
Object - options for the cache, specifying any of the following:-
cloneValues
Boolean - should returned values be clones of the original? [Default: false]
-
maxSize
Integer - maximum number of keys to store in this cache; if null, then unlimited [Default: null]
Return Value: An object with the following functions to modify the created cache. Please note that these functions do not require the cacheName.
- set : function (key, value)
- get : function (key)
- getAll : function ()
- exists : function (key)
- clear : function ()
- size : function ()
- options : function ()
var myCache = memoCache.cache.create('myCache');
console.log(myCache);
#####memoCache.cache.set(cacheName, key, value)
cacheName
String - name of the cachekey
String - key to be used to store the valuevalue
Any - value to be stored in the cache
Return Value: If the item is stored, then the stored value is returned, otherwise null
is returned.
memoCache.cache.set('myCache', 'isExample', true);
myCache.set('isExample', true);
#####memoCache.cache.get(cacheName, key)
cacheName
String - name of the cachekey
String - key to be used to retrieve the value
Return Value: If there is an item stored at the given key, then the stored value is returned, otherwise null
is returned.
memoCache.cache.get('myCache', 'isExample');
memoCache.cache.get('myCache', 'isNotExample');
myCache.get('isExample');
myCache.get('isNotExample');
#####memoCache.cache.getAll(cacheName)
cacheName
String - name of the cache
Return Value: If there are items stored in the cache, they will be returned as a JS document.
memoCache.cache.getAll('myCache');
memoCache.cache.get('notAValidCache');
myCache.getAll('isExample');
myCache.getAll('notAValidCache');
#####memoCache.cache.remove(cacheName, key)
cacheName
String - name of the cachekey
String - key to be deleted
Return Value: If there is an item stored at the given key, then the stored value is returned, otherwise null
is returned.
memoCache.cache.remove('myCache', 'isExample');
memoCache.cache.remove('myCache', 'isNotExample');
myCache.remove('isExample');
myCache.remove('isNotExample');
#####memoCache.cache.exists(cacheName, key)
cacheName
String - name of the cachekey
String - key to be checked
Return Value: If there is an item stored at the given key, then true
is returned, otherwise false
is returned.
memoCache.cache.exists('myCache', 'isExample');
memoCache.cache.exists('myCache', 'isNotExample');
myCache.exists('isExample');
myCache.exists('isNotExample');
#####memoCache.cache.clear(cacheName)
cacheName
String - name of the cache
Return Value: If the cache is cleared, then true
is returned, otherwise false
is returned.
memoCache.cache.clear('myCache');
myCache.clear();
#####memoCache.cache.size(cacheName)
cacheName
String - (optional) name of the cache; If not specified, the size of all caches is returned
Return Value: The size of the cache(s).
memoCache.cache.size('myCache');
memoCache.cache.size();
myCache.size();
#####memoCache.cache.options(cacheName)
cacheName
String - name of the cache
Return Value: If the cache exists, then the options object is returned, otherwise null
is returned.
memoCache.cache.options('myCache');
myCache.options();
####Memoization:
#####memoCache.memoize(function, options)
cacheName
String - name of the cacheoptions
Object - options for the cache, specifying any of the following:cloneValues
Boolean - should returned values be clones of the original? [Default: false]maxSize
Integer - maximum number of keys to store in this cache; if null, then unlimited [Default: null]memoHashFunction
Function - used to map the input arguments to a String. The result of this function becomes the key for the function result value
require memoCache = require('memo-cache');
var myFunction = function (aString) { console.log('cache miss!'); return aString; };
var myFunctionMemoized = memoCache.memoize(myFunction, {maxSize: 10});
myFunctionMemoized('testing');
myFunctionMemoized('testing');
Tests
$ npm test
Note: This requires mocha
, should
, async
, and underscore
.
Features
- Cache Functionality via memoCache.cache
- Can create multiple caches (allowing 'namespaces')
- Memoization Functionality via memoCache.memoize()
- Least Recently Used implementation when options.maxSize specified
- Caching and Memoization included in one module (usually separate)