@zcong/redis-cache
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

redis-cache

NPM version NPM downloads CircleCI codecov

Install

$ yarn add @zcong/redis-cache
# or
$ npm i @zcong/redis-cache --save

Usage

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)

License

MIT © zcong1993

Readme

Keywords

none

Package Sidebar

Install

npm i @zcong/redis-cache

Weekly Downloads

1

Version

0.6.0

License

MIT

Unpacked Size

41.6 kB

Total Files

20

Last publish

Collaborators

  • zcong