mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 10:23:34 +00:00
Provide new util caching method for strapi-redis
This commit is contained in:
parent
488343374b
commit
c76c4a9cd2
@ -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;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user