Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

zcong1993/redis-cache

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •