memoli
v1.1.0
Published
Memorize async/sync function result with cache
Maintainers
Readme
memoli.js
Cache the results of async/sync functions. When you call the function with the same parameters, memoli will fetch the previous result for you. Saving time has never been easier..
memoli uses redis and node-cache(for in-memory) caching solutions in background. Which one do you prefer is your choose. memoli provides a interface for all options of redis and node-cache.
memoliis compatible with v4 and higher version of redis and v5 and higher version of node-cache
Also, memoli.cache provides cache API's like: get, set, del, ..
Therefore, you can use caching functionality directly.
Installation
Install memoli with prefered cache solution:
npm i memoli redisnpm i memoli node-cacheAdd below to compilerOptions in tsconfig.json for decorator usages (OPTIONAL):
"experimentalDecorators": trueUsage
First of all, initialize memoli:
const memoli = await Memoli.initialize({
cacheSource: 'redis', // 'redis' or 'in-memory'
// redisOptions: Includes Redis' own options
redisOptions: {
url: 'redis://<username><password>@<host>:<port>/<database>',
ttl: 5, // 5 sec
},
});memoli have memolize function for wrapping the function calls and cache them.
Singular Functions
For example, you have following function declaration:
async function getHospitalFromHospitalService(id: number): Promise<Hospital> {
return hospitalService.getHospital(id); // async function call
}And calling this function like this:
const hospital = await getHospitalFromHospitalService(1);if you wrap this function call with memolize function, you can get result from cache for next function calls:
const hospital = await memoli.memolize({
fn: getHospitalFromHospitalService,
args: [1],
});memoli creates a cache key from your function name and given function parameters. Then, stores your function call result with this cache key. Thus, next function call with same parameters will be returned from cache.
Class Functions
if you are using class functions like this:
class HospitalServiceCaller {
// ...
async getHospitalFromHospitalService(id: number): Promise<Hospital> {
return hospitalService.getHospital(id); // async function call
}
// ...
}You need to give one more parameter called klass to memolize function:
const hospitalServiceCaller = new HospitalServiceCaller();
const hospital = await memoli.memolize({
klass: hospitalServiceCaller,
fn: hospitalServiceCaller.getHospitalFromHospitalService,
args: [1],
});Decorator Usage for Class Functions
memoli provides memolize decorator to make things easier:
class HospitalServiceCaller {
// ...
@memolize()
async getHospitalFromHospitalService(id: number): Promise<Hospital> {
return hospitalService.getHospital(id); // async function call
}
// ...
}With memolize decorator, you can use function calls as you used before:
const hospital = await hospitalServiceCaller.getHospitalFromHospitalService(1);Sync Functions
memoli supports sync function result caching as well.
Cache API
memoli provides usage of cache functionality directly with cache property:
const memoli = await Memoli.initialize({
cacheSource: 'in-memory',
inMemoryCacheOptions: {
stdTTL: 30, // 30 sec
checkperiod: 30, // each 30 sec
},
});
const cache = memoli.cache!;
const result1 = await cache.set('key-1', { value: 'value-1' });
console.log(result1); // { value: 'value-1' }
const result2 = await cache.get('key-1');
console.log(result2); // { value: 'value-1' }
const result3 = await cache.del('key-1');
console.log(result3); // true
const result4 = await cache.get('key-1');
console.log(result4); // undefined
await memoli.quit();Examples
To reach all provided code samples, visit examples
License
Copyright © 2024 Dedeloper.
This project is licensed under the MIT License - see the LICENSE file for details.
