Skip to content

Commit

Permalink
refactor: optimize redis operation
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 20, 2019
1 parent d0fb625 commit caf9f46
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/middleware/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,24 @@ 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 = '';
}
if (typeof value === 'object') {
value = JSON.stringify(value);
}
if (key) {
await redisClient.setex(key, maxAge, value);
redisClient.setex(key, maxAge, value);
}
},
client: redisClient,
Expand Down

0 comments on commit caf9f46

Please sign in to comment.