$ yarn add @zcong/redis-cache
# or
$ npm i @zcong/redis-cache --save
see ./src/example.ts
const rc = new RedisCache({ client: new Redis(), nonExistsExpire: 100 })
// cache map result: keys -> Map<key, value>
const mapFn = async (keys: string[]): Promise<Map<string, Res>> => {
const mp = new Map<string, Res>()
keys.forEach(k => {
mp.set(k, genRes(k))
})
return mp
}
// cache result 1000s
await rc.batchGet('test-map', mapFn, ['test1', 'test2'], 1000)
// cache array result: keys -> [{ key, value }]
const arrFn = async (keys: string[]): Promise<Res[]> => {
await sleep(1000)
return keys.map(genRes)
}
await rc.batchGetArray('test', arrFn, ['test1', 'test2'], 'k', 1000)
// get one, key => result
const singleFn = async (key: string): Promise<Res> => {
await sleep(1000)
return genRes(key)
}
await rc.getOne('single', singleFn, 'test', 1000)
// cache void function result: () => result
const fn = async (): Promise<Res> => {
await sleep(1000)
return genRes('test')
}
await rc.cacheFn('fn-test', fn, 1000)
MIT © zcong1993