refactor: optimize redis operation

This commit is contained in:
DIYgod 2019-09-20 15:57:50 +08:00
parent d0fb6254ff
commit caf9f461b3
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
1 changed files with 3 additions and 7 deletions

View File

@ -38,20 +38,16 @@ module.exports = function(app, options = {}) {
if (key && available) {
let value = await redisClient.get(key);
if (value) {
await redisClient.expire(key, config.cache.contentExpire);
redisClient.expire(key, config.cache.contentExpire);
value = value + '';
}
return value;
}
},
set: async function(key, value, maxAge = config.cache.contentExpire) {
set: function(key, value, maxAge = config.cache.contentExpire) {
if (!available) {
return;
}
if (await redisClient.exists(key)) {
logger.warn(`repeated key: ${key}, ${value}`);
return;
}
if (!value || value === 'undefined') {
value = '';
}
@ -59,7 +55,7 @@ module.exports = function(app, options = {}) {
value = JSON.stringify(value);
}
if (key) {
await redisClient.setex(key, maxAge, value);
redisClient.setex(key, maxAge, value);
}
},
client: redisClient,