Cache

Cache methods results using appolo-cache with optional redis store

Installation#

npm i @appolo/cache

Options#

keyDescriptionTypeDefault
idcacheProvider injector idstringcacheProvider
connectionredis connection stringstring``
memorytrue to use memory storebooleantrue
dbtrue to use redis storebooleanfalse
maxSizemax memory store itemsnumber1000
keyPrefixredis prefix keystringc

all option are optional and will be added as defaults to cache options in config/modules/all.ts

import {CacheModule} from '@appolo/cache';
export = async function (app: App) {
await app.module.use(CacheModule.for({maxSize:100}));
// or with redis store
app.module.use(CacheModule.for({db:true,connection:"redis://redis-connection-string"}));
}

Cache Options#

keyDescriptionTypeDefault
idcustom cache idstringclassName_methodName
maxSizemax cache sizenumber1000
maxAgeset maximum age in ms of all cache itemsnumberunlimited
cloneclone the cache resultbooleanfalse
intervalset cache refresh interval in msnumberundefined
resolverfunction to get the cache key by default the first argument will be used as the cache key.functionundefined
multiif no resolver defined use all the arguments as key else use the first argument as keybooleanfalse
peekuse peek method instead of getbooleanfalse
refreshrefresh cache on half maxAge expirebooleanfalse
keyPrefixredis prefix keystringc
memorytrue to use memory storebooleantrue
dbtrue to use redis storebooleanfalse
dbMaxAgeset maximum age in ms of all cache items in db if not defined maxAge will be usednumberunlimited
cacheNullcache null resultsbooleanfalse

Usage#

import { define } from '@appolo/inject';
import { cache } from '@appolo/cache';
@define()
export class SomeClass {
private counter = 0;
@cache()
method() {
return ++this.counter
}
// will be refreshed every 5 sec
@cache({interval:5000})
async method2(key:string) {
let result = await doSomeThingAsync(key)
return result;
}
// will try to get items from memroy with expire
// of 1 minute then from redis with expire of one hour
@cache({db:true,maxAge:60*1000,dbMaxAge:60*1000*60})
async method3(key:string) {
let result = await doSomeThingAsync(key)
return result;
}
// cache with custom key resolver
@cache({resolver:(key:string,key2:string)=>`${key}_${key2}`})
async method3(key:string,key2:string) {
let result = await doSomeThingAsync(key)
return result;
}
}

CacheProvider#

import { define,inject } from '@appolo/inject';
import { cacheProvider } from '@appolo/cache';
@define()
export class SomeClass {
@inject() cacheProvider:CacheProvider
@cache({id:"someId"})
method() {
return ++this.counter
}
someMethod(){
this.cacheProvider.getCacheById("someId").clear()
}
}

createCache#

createCache(options: ICacheOptions, valueFn: Function, scope?: any)#

create new cache wrapper

  • options - cache options
  • valueFn - value function will be called to get the value
  • scope - scope of the value function

getCacheById#

getCacheById(id:string):Cache#

return cache wrapper by id

Cache#

cache wrapper instance

get#

get<T>(...args: any[]): Promise<T> | T#

get value from cache if not found the value fn will be called

cache#

cache:Cache#

getter return appolo-cache​ instance