Using Redis as cache
$ npm i @tadashi/cache-redis
Name | Type | Default | Description |
---|---|---|---|
opts | object | {namespace: 'cache', redis: {}} | See bellow |
Addresses to connect (separated by commas)
The namespace for all cache members
See ioredis options
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'})
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
BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4
MIT © Thiago Lagden