lagden / cache-redis

Using redis as cache

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cache-redis

NPM version Node.js CI Coverage Status

XO code style Snyk badge

Using Redis as cache

Install

$ npm i -S @tadashi/cache-redis

API

new Cache( [opts])

Name Type Default Description
opts object {namespace: 'cache', redis: {}} See bellow

opts.addresses:String

Addresses to connect (separated by commas)

opts.namespace:String

The namespace for all cache members

opts.redis:Object

See ioredis options


Cluster

To use Redis.Cluster, set addresses separated by commas:

const cache = new Cache({addresses: '127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381'})

Usage

import Cache from '@tadashi/cache-redis'

const _cache = new Cache({
  redis: {
    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

License

MIT © Thiago Lagden

About

Using redis as cache

License:MIT License


Languages

Language:JavaScript 67.8%Language:Shell 32.2%