0.6.0 • Published 3 years ago

@zcong/redis-cache v0.6.0

Weekly downloads
71
License
MIT
Repository
-
Last release
3 years ago

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

0.6.0

3 years ago

0.5.1

3 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago