cache-redis
Using Redis as cache
Install
$ npm i @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
Donate ❤️
BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4
License
MIT © Thiago Lagden