@hitorisensei/memoizee-decorator
v1.0.1
Published
Memoization decorator backed by memoizee and hash-sum
Downloads
97
Maintainers
Readme
@hitorisensei/memoizee-decorator
Description
Adds memoization decorator for methods and getters.
Uses memoizee library for memoization.
Uses hash-sum library as a default cache-key generation algorithm.
Usage
import { Memoize } from '@hitorisensei/memoizee-decorator';
class MyClass {
@Memoize()
public getMyValue(): number {
console.log('My value is calculated');
return someExpensiveCalculation();
}
}
const myClass = new MyClass();
myClass.getMyValue(); // console logs 'My value is calculated' only once
myClass.getMyValue(); // returns cached value
Memoizee options
Decorator can be called with all memoizee options.
import { Memoize } from '@hitorisensei/memoizee-decorator';
class MyClass {
@Memoize({
maxAge: 1000,
max: 100,
})
public getMyValue(): number {
console.log('My value is calculated');
return someExpensiveCalculation();
}
}
Clearing memoization cache
import { Memoize, clearMemoization } from '@hitorisensei/memoizee-decorator';
class MyClass {
@Memoize()
public getMyValue(): number {
console.log('My value is calculated');
return someExpensiveCalculation();
}
}
const myClass = new MyClass();
myClass.getMyValue(); // console logs 'My value is calculated' only once
myClass.getMyValue(); // returns cached value
clearMemoization(myClass, 'getMyValue'); // clears cache
myClass.getMyValue(); // console logs 'My value is calculated' again