node-cache-service
v0.0.0
Published
A node util library for caching your services
Downloads
3
Maintainers
Readme
node-cache-service
Description: This library offers a cache()
function that proxifies your service's method calls
Notes:
- This library works with
async
and non-async
methods - This library uses
node-cache
as an implementation, by default TTL is disabled, but you can provide your own configuredNodeCache
object as the second argument ofcache()
- This library does not support eviction yet (maybe later)
- For Java developpers, it works like Spring's
@Cacheable
annotation on a class (aka bean)
How to use this library
Example:
import cache from 'node-cache-service/index';
class SuperCalculatorService {
// Mutable attribute for demo
counter: number = 0;
method1(someArg?: string): number {
return ++this.counter;
}
async method2(someArg?: string): Promise<number> {
return ++this.counter;
}
}
// Initialize your service
const superCalculatorService = new SuperCalculatorService();
// Cache your service
const cachedService = cache(superCalculatorService);
// Call your methods
const callNumber1 = cachedService.method1('call'); // 1
const callNumber2 = cachedService.method1('call'); // 1
const callNumber3 = cachedService.method1('call number 3'); // 2
const callNumber4 = await cachedService.method2('call'); // 3
const callNumber5 = await cachedService.method2('call'); // 3
const callNumber6 = await cachedService.method2('call number 6'); // 4