feat: support no refresh cache

This commit is contained in:
DIYgod
2019-11-04 18:59:00 +08:00
parent 73163db38a
commit 31726d4464
4 changed files with 50 additions and 10 deletions

View File

@@ -34,10 +34,10 @@ module.exports = function(app, options = {}) {
}); });
app.context.cache = { app.context.cache = {
get: async (key) => { get: async (key, refresh = true) => {
if (key && available) { if (key && available) {
let value = await redisClient.get(key); let value = await redisClient.get(key);
if (value) { if (value && refresh) {
redisClient.expire(key, config.cache.contentExpire); redisClient.expire(key, config.cache.contentExpire);
value = value + ''; value = value + '';
} }
@@ -81,16 +81,16 @@ module.exports = function(app, options = {}) {
}); });
app.context.cache = { app.context.cache = {
get: (key) => { get: (key, refresh = true) => {
if (key && available) { if (key && available) {
let value = routeCache.get(key); let value = (refresh ? routeCache : pageCache).get(key);
if (value) { if (value) {
value = value + ''; value = value + '';
} }
return value; return value;
} }
}, },
set: (key, value, maxAge = config.cache.contentExpire) => { set: (key, value, maxAge = config.cache.contentExpire, refresh = true) => {
if (!value || value === 'undefined') { if (!value || value === 'undefined') {
value = ''; value = '';
} }
@@ -98,7 +98,7 @@ module.exports = function(app, options = {}) {
value = JSON.stringify(value); value = JSON.stringify(value);
} }
if (key && available) { if (key && available) {
return routeCache.set(key, value, maxAge * 1000); return (refresh ? routeCache : pageCache).set(key, value, maxAge * 1000);
} }
}, },
client: [pageCache, routeCache], client: [pageCache, routeCache],

View File

@@ -36,6 +36,24 @@ module.exports = async (ctx) => {
link: `https://github.com/DIYgod/RSSHub/issues/0`, link: `https://github.com/DIYgod/RSSHub/issues/0`,
author: `DIYgod0`, 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') { } else if (ctx.params.id === 'complicated') {
item.push({ item.push({
title: `Complicated Title`, title: `Complicated Title`,

View File

@@ -4,7 +4,7 @@ const config = require('@/config').value;
module.exports = async (ctx) => { module.exports = async (ctx) => {
const uid = ctx.params.uid; const uid = ctx.params.uid;
const feature = ctx.params.feature || 0; 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) { if (token) {
const response = await got.get(`https://api.weibo.com/2/statuses/home_timeline.json?access_token=${token}&count=100&feature=${feature}`); 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 token = rep.data.access_token;
const uid = rep.data.uid; const uid = rep.data.uid;
const expires_in = rep.data.expires_in; 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({ ctx.set({
'Content-Type': 'text/html; charset=UTF-8', 'Content-Type': 'text/html; charset=UTF-8',

View File

@@ -64,7 +64,18 @@ describe('cache', () => {
mock: 1, mock: 1,
}); });
expect(await app.context.cache.globalCache.get('mock')).toBe('{"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 () => { it('redis', async () => {
process.env.CACHE_TYPE = 'redis'; 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');
await app.context.cache.set('mock2', '2'); await app.context.cache.set('mock2', '2');
expect(await app.context.cache.get('mock2')).toBe('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 () => { it('redis with quit', async () => {
process.env.CACHE_TYPE = 'redis'; process.env.CACHE_TYPE = 'redis';