7.0.1 • Published 10 months ago

@tadashi/cache-redis v7.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
10 months ago

cache-redis

NPM version Node.js CI Coverage Status Snyk badge

Making cache with Redis

Install

$ npm i @tadashi/cache-redis

API

new Cache( [opts])

parametertyperequireddefaultdescription
optsObjectnosee belowOptions for configuring the cache.

opts

parametertyperequireddefaultdescription
addressString|String[]no127.0.0.1:6379The address of the Redis server.
namespaceStringnoappA namespace for the cache keys.
redisOptionsObjectno-See configuration options

Cluster

To use Cluster, set addresses separated by commas or an array and set clusterOptions.

import Cache from '@tadashi/cache-redis'

const cache = new Cache({
  address: '127.0.0.1:6379, 127.0.0.1:6380, 127.0.0.1:6381',
  // or
  address: ['127.0.0.1:6379', '127.0.0.1:6380', '127.0.0.1:6381'],
  // and
  redisOptions: {
    clusterOptions: {
      retryDelayOnClusterDown: 500,
      // ...
    }
  }
})

Usage

import Cache from '@tadashi/cache-redis'

const _cache = new Cache({
  redisOptions: {
    keyPrefix: 'api'
  },
  namespace: 'example'
})

async function find(key) {
  try {
    const cache = await _cache.get(key)
    if (cache) {
      return cache
    }
    const result = await getDataFromSomeWhere(key)
    await _cache.set(key, result, 'PX', 3600)
    return result
  } catch (err) {
    throw err
  }
}


await find('foo')
// => data from getDataFromSomeWhere

await find('foo')
// => data from cache

!IMPORTANT
Buy me a coffee!
BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4

License

MIT © Thiago Lagden

7.0.0

10 months ago

7.0.1

10 months ago

6.4.1

2 years ago

6.4.0

2 years ago

6.3.1

3 years ago

6.3.0

3 years ago

6.2.0

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.3.0

4 years ago

4.2.0

5 years ago

4.1.5

5 years ago

4.1.4

5 years ago

4.1.3

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

6 years ago

4.0.0

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.0.0

6 years ago

1.1.0

7 years ago

1.0.0

8 years ago