diff --git a/lib/middleware/lru-cache.js b/lib/middleware/lru-cache.js index d8d4f9d901..bbf8fd68e2 100644 --- a/lib/middleware/lru-cache.js +++ b/lib/middleware/lru-cache.js @@ -14,19 +14,28 @@ module.exports = function(options = {}) { ignoreQuery = false, } = options; - const memoryCache = new lru({ + const temporaryCache = new lru({ + maxAge: expire * 1000, + max: maxLength, + }); + + const persistentCache = new lru({ maxAge: expire * 1000, max: maxLength, updateAgeOnGet: true, }); options.app.context.cache = { - get: (key) => { + get: (key, persistent) => { if (key) { - return memoryCache.get(key); + if (persistent) { + return persistentCache.get(key); + } else { + return temporaryCache.get(key); + } } }, - set: (key, value, maxAge) => { + set: (key, value, maxAge, persistent) => { if (!value || value === 'undefined') { value = ''; } @@ -34,7 +43,11 @@ module.exports = function(options = {}) { value = JSON.stringify(value); } if (key) { - memoryCache.set(key, value, maxAge * 1000); + if (persistent) { + return persistentCache.set(key, value, maxAge * 1000); + } else { + return temporaryCache.set(key, value, maxAge * 1000); + } } }, }; @@ -76,13 +89,14 @@ module.exports = function(options = {}) { * getCache */ async function getCache(ctx, key, tkey) { - const value = memoryCache.get(key); let type; + const value = temporaryCache.get(key); + let ok = false; if (value) { ctx.response.status = 200; - type = memoryCache.get(tkey) || 'text/html'; + type = temporaryCache.get(tkey) || 'text/html'; // can happen if user specified return_buffers: true in redis options if (Buffer.isBuffer(type)) { type = type.toString(); @@ -115,7 +129,7 @@ module.exports = function(options = {}) { if (Buffer.byteLength(body) > maxLength) { return; } - memoryCache.set(key, body); + temporaryCache.set(key, body); await cacheType(ctx, tkey); } @@ -126,7 +140,7 @@ module.exports = function(options = {}) { async function cacheType(ctx, tkey) { const type = ctx.response.headers['content-type']; if (type) { - memoryCache.set(tkey, type); + temporaryCache.set(tkey, type); } } }; diff --git a/lib/middleware/redis-cache.js b/lib/middleware/redis-cache.js index 16bd4f0d34..60b7df70e0 100644 --- a/lib/middleware/redis-cache.js +++ b/lib/middleware/redis-cache.js @@ -41,18 +41,14 @@ module.exports = function(options = {}) { }); options.app.context.cache = { - get: async (key) => { + /* eslint-disable no-unused-vars */ + get: async (key, persistent) => { + /* eslint-enable no-unused-vars */ if (key) { - const value = await redisClient.get(key); - if (value) { - let ttl = await redisClient.ttl(key); - ttl = ttl > expire ? ttl : expire * 2; - await redisClient.setex(key, ttl, value); - } - return value; + return await redisClient.get(key); } }, - set: async (key, value, maxAge) => { + set: async (key, value, maxAge, persistent) => { if (!value || value === 'undefined') { value = ''; } @@ -60,7 +56,11 @@ module.exports = function(options = {}) { value = JSON.stringify(value); } if (key) { - await redisClient.setex(key, maxAge, value); + if (persistent) { + await redisClient.expire(key, value); + } else { + await redisClient.setex(key, maxAge, value); + } } }, };