Provide new util caching method for strapi-redis

This commit is contained in:
Aurélien Georget 2017-02-01 17:17:06 +01:00
parent 488343374b
commit c76c4a9cd2

View File

@ -54,6 +54,28 @@ module.exports = function () {
return process.kill();
});
// Utils function.
// Behavior: Try to retrieve data from Redis, if null
// execute callback and set the value in Redis for this serial key.
redis.cache = async (serial, cb, type) => {
let cache = await redis.get(serial);
if (!cache) {
cache = await cb();
if (cache) {
redis.set(serial, cache);
}
}
switch (type) {
case 'int':
return parseInt(cache);
default:
return cache;
}
};
// Define as new connection.
strapi.connections[name] = redis;