quidol-redis-cache
Quidol Cache used for our real time show and REST API.
- Async-mutex included in this package for a reliable cache.
- Only one connection used per redis Instance, if you instanciante multiple
QuidolCache
this will not open a new connection but use a connection already open.
Install
yarn add @redpill-paris/quidol-redis-cache
Configuration
const QuidolCache = require('@redpill-paris/quidol-redis-cache');
const redisOptions = {
host: 'localhost',
port: '6379',
}
const cache = new QuidolCache({ redisOptions });
Parameters available:
- redisOptions: compatible with all options used in the connect from ioRedis.
- defaultTTL: default expiration key in seconds(default 60).
Methods
cache.get(key, storeFunction(optionnal))
return a Promise.
If the cache is invalid or null the storeFunction will be executed and the result of this function will be stored in the cache.
const userInfo = await cache.get(
`userInfo:${userId}`,
async () => {
...
return userInfo
}
);
cache.del(key)
Return a Promise.
await cache.del(`userInfo:${userId}`);
cache.set(key, value)
Return a Promise
await cache.set(`userInfo:${userId}`, {
admin: true,
nickname: 'Kubessandra',
});
Exemple:
const QuidolCache = require('@redpill-paris/quidol-redis-cache');
const redisOptions = {
host: 'localhost',
port: '6379',
}
const cache = new QuidolCache({ redisOptions });
const userId = '123456';
const userInfo = await cache.get(
`userInfo:${userId}`,
async () => {
...
return userInfo
}
);
console.log(userInfo);
await cache.del(`userInfo:${userId}`);