From cdc94dc76c3bf28fa68af01dcf3ebaf83843c665 Mon Sep 17 00:00:00 2001 From: Rongrong <15956627+Rongronggg9@users.noreply.github.com> Date: Mon, 28 Mar 2022 22:55:29 +0800 Subject: [PATCH] refactor(core): unified memory cache (#9395) Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com> --- lib/middleware/cache/index.js | 6 +++--- lib/middleware/cache/memory.js | 16 +++++----------- lib/routes/amd/graphicsdrivers.js | 2 +- lib/routes/weibo/timeline.js | 2 +- lib/v2/test/index.js | 2 +- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/middleware/cache/index.js b/lib/middleware/cache/index.js index 178f700d650081..05bf29a2e853a5 100644 --- a/lib/middleware/cache/index.js +++ b/lib/middleware/cache/index.js @@ -27,10 +27,10 @@ if (config.cache.type === 'redis') { globalCache.set = cacheModule.set; } else if (config.cache.type === 'memory') { cacheModule = require('./memory'); - const { pageCache } = cacheModule.clients; + const { memoryCache } = cacheModule.clients; globalCache.get = (key) => { if (key && cacheModule.status.available) { - return pageCache.get(key); + return memoryCache.get(key, { updateAgeOnGet: false }); } }; globalCache.set = (key, value, maxAge) => { @@ -41,7 +41,7 @@ if (config.cache.type === 'redis') { value = JSON.stringify(value); } if (key) { - return pageCache.set(key, value, { ttl: maxAge * 1000 }); + return memoryCache.set(key, value, { ttl: maxAge * 1000 }); } }; } else { diff --git a/lib/middleware/cache/memory.js b/lib/middleware/cache/memory.js index 1e7efe3a6cc86e..671ac3cf134675 100644 --- a/lib/middleware/cache/memory.js +++ b/lib/middleware/cache/memory.js @@ -3,30 +3,24 @@ const config = require('@/config').value; const status = { available: false }; -const pageCache = new Lru({ +const memoryCache = new Lru({ ttl: config.cache.routeExpire * 1000, max: config.memory.max, }); -const routeCache = new Lru({ - ttl: config.cache.routeExpire * 1000, - max: config.memory.max, - updateAgeOnGet: true, -}); - status.available = true; module.exports = { get: (key, refresh = true) => { if (key && status.available) { - let value = (refresh ? routeCache : pageCache).get(key); + let value = memoryCache.get(key, { updateAgeOnGet: refresh }); if (value) { value = value + ''; } return value; } }, - set: (key, value, maxAge = config.cache.contentExpire, refresh = true) => { + set: (key, value, maxAge = config.cache.contentExpire) => { if (!value || value === 'undefined') { value = ''; } @@ -34,9 +28,9 @@ module.exports = { value = JSON.stringify(value); } if (key && status.available) { - return (refresh ? routeCache : pageCache).set(key, value, { ttl: maxAge * 1000 }); + return memoryCache.set(key, value, { ttl: maxAge * 1000 }); } }, - clients: { pageCache, routeCache }, + clients: { memoryCache }, status, }; diff --git a/lib/routes/amd/graphicsdrivers.js b/lib/routes/amd/graphicsdrivers.js index 5adbe36cb2c545..268b019b8a2113 100644 --- a/lib/routes/amd/graphicsdrivers.js +++ b/lib/routes/amd/graphicsdrivers.js @@ -27,7 +27,7 @@ module.exports = async (ctx) => { if (!data) { const res = await got.get(url); data = res.data; - ctx.cache.set(url, data, config.cache.contentExpire, false); + ctx.cache.set(url, data, config.cache.contentExpire); } const $ = cheerio.load(data, { xmlMode: true }); diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js index 15c0aa08994bb9..593a25fac0f386 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/routes/weibo/timeline.js @@ -130,7 +130,7 @@ module.exports = async (ctx) => { const token = rep.data.access_token; const uid = rep.data.uid; const expires_in = rep.data.expires_in; - await ctx.cache.set('weibotimelineuid' + uid, token, expires_in, false); + await ctx.cache.set('weibotimelineuid' + uid, token, expires_in); ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', diff --git a/lib/v2/test/index.js b/lib/v2/test/index.js index de003ee3cc96f9..dc3396bcd4957e 100644 --- a/lib/v2/test/index.js +++ b/lib/v2/test/index.js @@ -62,7 +62,7 @@ module.exports = async (ctx) => { } if (!noRefresh) { noRefresh = '0'; - await ctx.cache.set('noRefreshCache', '1', undefined, false); + await ctx.cache.set('noRefreshCache', '1', undefined); } item.push({ title: 'Cache Title',