From c4cd0ab4957ec622fa2ea8145c4a586fc8b571de Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 13 May 2019 00:24:48 +0800 Subject: [PATCH] fix(chore): contentExpire not work --- docs/en/install/README.md | 2 +- docs/install/README.md | 2 +- lib/config.js | 2 +- lib/middleware/cache.js | 8 ++++---- lib/routes/bangumi/calendar/_base.js | 16 +--------------- lib/routes/guanchazhe/topic.js | 2 +- 6 files changed, 9 insertions(+), 23 deletions(-) diff --git a/docs/en/install/README.md b/docs/en/install/README.md index c08eabeb3..3facf24b8 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -268,7 +268,7 @@ Use environment variables is recommended to avoid conflicts during upgrade. `CACHE_EXPIRE`: route cache expiry time in seconds, default to `5 * 60` -`CACHE_CONTENT_EXPIRE`: content cache expiry time in seconds, default to `24 * 60 * 60` +`CACHE_CONTENT_EXPIRE`: content cache expiry time in seconds, it will be recalculated when it is accessed, default to `1 * 60 * 60` `LISTEN_INADDR_ANY`: open up for external access, default to `1` diff --git a/docs/install/README.md b/docs/install/README.md index 4a5f6b84a..62e0f1545 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -313,7 +313,7 @@ $ docker run -d --name rsshub -p 1200:1200 rsshub:arm32v7 `CACHE_EXPIRE`: 路由缓存过期时间, 单位为秒, 默认 `5 * 60` -`CACHE_CONTENT_EXPIRE`: 内容缓存过期时间,单位为秒, 默认 `24 * 60 * 60` +`CACHE_CONTENT_EXPIRE`: 内容缓存过期时间,每次访问会重新计算过期时间,单位为秒, 默认 `1 * 60 * 60` `LISTEN_INADDR_ANY`: 是否允许公网连接, 默认 `1` diff --git a/lib/config.js b/lib/config.js index a6971a684..6d3d38ed3 100644 --- a/lib/config.js +++ b/lib/config.js @@ -15,7 +15,7 @@ module.exports = { cache: { type: process.env.CACHE_TYPE || 'memory', // 缓存类型,支持 'memory' 和 'redis',设为空可以禁止缓存 routeExpire: parseInt(process.env.CACHE_EXPIRE) || 5 * 60, // 路由缓存时间,单位为秒 - contentExpire: parseInt(process.env.CACHE_CONTENT_EXPIRE) || 24 * 60 * 60, // 不变内容缓存时间,单位为秒 + contentExpire: parseInt(process.env.CACHE_CONTENT_EXPIRE) || 1 * 60 * 60, // 不变内容缓存时间,单位为秒 }, ua: process.env.UA || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', listenInaddrAny: parseInt(process.env.LISTEN_INADDR_ANY) || 1, // 是否允许公网连接,取值 0 1 diff --git a/lib/middleware/cache.js b/lib/middleware/cache.js index f9cc1958b..9f509e10a 100644 --- a/lib/middleware/cache.js +++ b/lib/middleware/cache.js @@ -38,13 +38,13 @@ module.exports = function(app, options = {}) { if (key) { let value = await redisClient.get(key); if (value) { - await redisClient.expire(key, config.cache.routeExpire); + await redisClient.expire(key, config.cache.contentExpire); value = value + ''; } return value; } }, - set: async function(key, value, maxAge = config.cache.routeExpire) { + set: async function(key, value, maxAge = config.cache.contentExpire) { if (await redisClient.exists(key)) { logger.warn(`repeated key: ${key}, ${value}`); return; @@ -89,7 +89,7 @@ module.exports = function(app, options = {}) { return value; } }, - set: (key, value, maxAge = config.cache.routeExpire) => { + set: (key, value, maxAge = config.cache.contentExpire) => { if (!value || value === 'undefined') { value = ''; } @@ -125,7 +125,7 @@ module.exports = function(app, options = {}) { }; } - app.context.cache.tryGet = async function(key, getValueFunc, maxAge = config.cache.routeExpire) { + app.context.cache.tryGet = async function(key, getValueFunc, maxAge = config.cache.contentExpire) { let v = await this.get(key); if (!v) { v = await getValueFunc(); diff --git a/lib/routes/bangumi/calendar/_base.js b/lib/routes/bangumi/calendar/_base.js index 4af04917f..f181db126 100644 --- a/lib/routes/bangumi/calendar/_base.js +++ b/lib/routes/bangumi/calendar/_base.js @@ -1,19 +1,5 @@ const axios = require('../../../utils/axios'); -/** @type { (timezoneOffset: number) => Date } */ -const tomorrowMidnight = (timezoneOffset) => { - const result = new Date(Date.now()); - const localHour = result.getHours() + timezoneOffset; - if (localHour < 0) { - result.setUTCHours(-timezoneOffset, 0, 0, 0); - } else if (localHour < 24) { - result.setUTCHours(24 - timezoneOffset, 0, 0, 0); - } else { - result.setUTCHours(48 - timezoneOffset, 0, 0, 0); - } - return result; -}; - module.exports = async (ctx) => { const bgmCalendarUrl = 'https://api.bgm.tv/calendar'; const bgmDataUrl = 'https://cdn.jsdelivr.net/npm/bangumi-data/dist/data.json'; @@ -47,7 +33,7 @@ module.exports = async (ctx) => { data.items = items; } - ctx.cache.set(url[i], JSON.stringify(data), tomorrowMidnight(8).getTime() - Date.now()); + ctx.cache.set(url[i], JSON.stringify(data)); return Promise.resolve(data); } }) diff --git a/lib/routes/guanchazhe/topic.js b/lib/routes/guanchazhe/topic.js index 539c3583d..e067ecced 100644 --- a/lib/routes/guanchazhe/topic.js +++ b/lib/routes/guanchazhe/topic.js @@ -42,7 +42,7 @@ module.exports = async (ctx) => { // 格式化日期 pubDate: date(dateStr), }; - ctx.cache.set(absoluteUrl, JSON.stringify(item), 24 * 60 * 60); + ctx.cache.set(absoluteUrl, JSON.stringify(item)); return Promise.resolve(item); })