Caches function calls results
npm install function-cacheThis module uses Promises (provided by Q).
npm install --save function-cache
var cache = require("function-cache");
function foo(param) {
console.log("foo has been called.");
return "Foo" + param;
}
var cachedFoo = cache(foo);
cachedFoo("Bar") // Prints "foo has been called." because no cache is available yet.
.then(function(result) {
console.log(result); // Prints "FooBar"
return cachedFoo("Bar"); // Prints nothing because the result is already cached.
})
.then(function(result) {
console.log(result); // Prints "FooBar"
return cachedFoo("Plop"); // Prints "foo has been called." because no cache is available yet for this argument.
})
.done();
The cache function can take a second parameter :
var cachedFoo = cache(foo, {
tmpDir: os.tmpdir(),
useMemoryCache: true,
useFileCache: true,
serializer: JSON.stringify,
unserializer: JSON.parse,
hasher: farmhash.hash32,
tmpPrefix: "function-cache"
});
true.true.os.tmpdir()."function-cache".string-hash.JSON.stringify.JSON.parse.