flexible cache wrapper for js object.
- Node.js v8+
- npm or yarn
$ npm add instance-memoize
instance-memoize
cached results of method invocation on the specified object permanently.
If you want to manage cache purging and lifetime see [custom cache example](#with custom cache).
import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules
const obj = {
calledCounter: 0,
method: function() {
calledCounter++;
return "test value";
}
};
const wrapped = memoize({
instance: obj,
methods: ['method']
})
console.log(wrapped.method()) // output => "test value"
console.log(wrapped.calledCounter) // output => "1"
/*
The result of "method" is stored internal cache,
From the second call, the result is returned via cache.
*/
console.log(wrapped.method()) // output(from cache) => "test value"
console.log(wrapped.calledCounter) // output => "1"
import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules
class Sample {
constructor() {
this.calledCounter = 0;
}
someMethod() {
this.calledCounter++;
return "value";
}
}
const wrapped = memoize({
instance: new Sample(),
methods: ['someMethod']
})
console.log(wrapped.someMethod()) // output => "test value"
console.log(wrapped.calledCounter) // output => "1"
console.log(wrapped.someMethod()) // output(from cache) => "test value"
console.log(wrapped.calledCounter) // output => "1"
instance-memoize
also works with async/await or Promise.
import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules
class Sample {
constructor() {
this.calledCounter = 0;
}
async someMethod() {
this.calledCounter++;
return "value";
}
}
const wrapped = memoize({
instance: new Sample(),
methods: ['someMethod']
})
console.log(await wrapped.someMethod()) // output => "test value"
console.log(wrapped.calledCounter) // output => "1"
console.log(await wrapped.someMethod()) // output(from cache) => "test value"
console.log(wrapped.calledCounter) // output => "1"
You can use your customized cache by passing cache instance via options.
cache
instance must implement three methods;
- get(key: string) :Object
- set(key: string, obj: any)
- reset()
This example shows instance-memoize
working with isaacs's node-lru-cache.
import memoize from 'instance-memoize'; // or const memoize = require('instance-memoize') for commonjs modules
import LRU from 'lru-cache' // import isaacs's node-lru-cache
class Sample {
constructor() {
this.calledCounter = 0;
}
someMethod() {
this.calledCounter++;
return "value";
}
}
const wrapped = memoize({
instance: new Sample(),
methods: ['someMethod'],
options: {
cache: LRU({ ...lruCacheOptions })
}
})
- instance (Object): Source object that wrapped to.
- methodNames (Array): Method name(s) that cached.
- options (Object):
- cache (Object): Custom Cache object
- keygen (function(methodName: string, ...args): string ): cache key generator function
(Object): Wrapped instance that caches the result of specified method invocation.
- Akito Ito - Initial work - akito0107
This project is licensed under the Apache2.0 License - see the LICENSE.md file for details