From 31726d44643c8b53587759b27862a19c5c2fbc8f Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 4 Nov 2019 18:59:00 +0800 Subject: [PATCH] feat: support no refresh cache --- lib/middleware/cache.js | 12 ++++++------ lib/routes/test/index.js | 18 ++++++++++++++++++ lib/routes/weibo/timeline.js | 4 ++-- test/middleware/cache.js | 26 ++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/lib/middleware/cache.js b/lib/middleware/cache.js index 7b6e6b07a..2b4a99fb2 100644 --- a/lib/middleware/cache.js +++ b/lib/middleware/cache.js @@ -34,10 +34,10 @@ module.exports = function(app, options = {}) { }); app.context.cache = { - get: async (key) => { + get: async (key, refresh = true) => { if (key && available) { let value = await redisClient.get(key); - if (value) { + if (value && refresh) { redisClient.expire(key, config.cache.contentExpire); value = value + ''; } @@ -81,16 +81,16 @@ module.exports = function(app, options = {}) { }); app.context.cache = { - get: (key) => { + get: (key, refresh = true) => { if (key && available) { - let value = routeCache.get(key); + let value = (refresh ? routeCache : pageCache).get(key); if (value) { value = value + ''; } return value; } }, - set: (key, value, maxAge = config.cache.contentExpire) => { + set: (key, value, maxAge = config.cache.contentExpire, refresh = true) => { if (!value || value === 'undefined') { value = ''; } @@ -98,7 +98,7 @@ module.exports = function(app, options = {}) { value = JSON.stringify(value); } if (key && available) { - return routeCache.set(key, value, maxAge * 1000); + return (refresh ? routeCache : pageCache).set(key, value, maxAge * 1000); } }, client: [pageCache, routeCache], diff --git a/lib/routes/test/index.js b/lib/routes/test/index.js index 364cf51aa..1634fbeeb 100644 --- a/lib/routes/test/index.js +++ b/lib/routes/test/index.js @@ -36,6 +36,24 @@ module.exports = async (ctx) => { link: `https://github.com/DIYgod/RSSHub/issues/0`, author: `DIYgod0`, }); + } else if (ctx.params.id === 'refreshCache') { + let refresh = await ctx.cache.get('refreshCache'); + let noRefresh = await ctx.cache.get('noRefreshCache', false); + if (!refresh) { + refresh = '0'; + await ctx.cache.set('refreshCache', '1'); + } + if (!noRefresh) { + noRefresh = '0'; + await ctx.cache.set('noRefreshCache', '1', undefined, false); + } + item.push({ + title: 'Cache Title', + description: refresh + ' ' + noRefresh, + pubDate: new Date(`2019-3-1`).toUTCString(), + link: `https://github.com/DIYgod/RSSHub/issues/0`, + author: `DIYgod0`, + }); } else if (ctx.params.id === 'complicated') { item.push({ title: `Complicated Title`, diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js index f07616c67..2a7c12722 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/routes/weibo/timeline.js @@ -4,7 +4,7 @@ const config = require('@/config').value; module.exports = async (ctx) => { const uid = ctx.params.uid; const feature = ctx.params.feature || 0; - const token = await ctx.cache.get('weibotimelineuid' + uid); + const token = await ctx.cache.get('weibotimelineuid' + uid, false); if (token) { const response = await got.get(`https://api.weibo.com/2/statuses/home_timeline.json?access_token=${token}&count=100&feature=${feature}`); @@ -62,7 +62,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); + await ctx.cache.set('weibotimelineuid' + uid, token, expires_in, false); ctx.set({ 'Content-Type': 'text/html; charset=UTF-8', diff --git a/test/middleware/cache.js b/test/middleware/cache.js index c16f70138..75b6f1f0d 100644 --- a/test/middleware/cache.js +++ b/test/middleware/cache.js @@ -64,7 +64,18 @@ describe('cache', () => { mock: 1, }); expect(await app.context.cache.globalCache.get('mock')).toBe('{"mock":1}'); - }); + + await request.get('/test/refreshCache'); + await wait(1 * 1000 + 100); + const response5 = await request.get('/test/refreshCache'); + const parsed5 = await parser.parseString(response5.text); + await wait(2 * 1000 + 100); + const response6 = await request.get('/test/refreshCache'); + const parsed6 = await parser.parseString(response6.text); + + expect(parsed5.items[0].content).toBe('1 1'); + expect(parsed6.items[0].content).toBe('1 0'); + }, 10000); it('redis', async () => { process.env.CACHE_TYPE = 'redis'; @@ -106,7 +117,18 @@ describe('cache', () => { await app.context.cache.set('mock2', '2'); await app.context.cache.set('mock2', '2'); expect(await app.context.cache.get('mock2')).toBe('2'); - }); + + await request.get('/test/refreshCache'); + await wait(1 * 1000 + 100); + const response5 = await request.get('/test/refreshCache'); + const parsed5 = await parser.parseString(response5.text); + await wait(2 * 1000 + 100); + const response6 = await request.get('/test/refreshCache'); + const parsed6 = await parser.parseString(response6.text); + + expect(parsed5.items[0].content).toBe('1 1'); + expect(parsed6.items[0].content).toBe('1 0'); + }, 10000); it('redis with quit', async () => { process.env.CACHE_TYPE = 'redis';