diff --git a/docs/en/install/README.md b/docs/en/install/README.md index 268c11af5bd003..58b117500b4a75 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -388,6 +388,8 @@ RSSHub supports two caching methods: memory and redis `REDIS_URL`: Redis target address (invalid when `CACHE_TYPE` is set to memory), default to `redis://localhost:6379/` +`MEMORY_MAX`: maximum number of cached items (invalid when `CACHE_TYPE` is set to redis), default to `256` + ### Proxy Configurations Partial routes have a strict anti-crawler policy, and can be configured to use proxy. diff --git a/docs/install/README.md b/docs/install/README.md index ed5d4009b2b23c..0ab94995158291 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -392,6 +392,8 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式 `REDIS_URL`: Redis 连接地址(redis 缓存类型时有效),默认为 `redis://localhost:6379/` +`MEMORY_MAX`: 最大缓存数量(memory 缓存类型时有效),默认 `256` + ### 代理配置 部分路由反爬严格,可以配置使用代理抓取。 diff --git a/lib/config.js b/lib/config.js index 8e86710e8a5abe..73135c26bbad8a 100644 --- a/lib/config.js +++ b/lib/config.js @@ -49,6 +49,10 @@ const calculateValue = () => { routeExpire: parseInt(envs.CACHE_EXPIRE) || 5 * 60, // 路由缓存时间,单位为秒 contentExpire: parseInt(envs.CACHE_CONTENT_EXPIRE) || 1 * 60 * 60, // 不变内容缓存时间,单位为秒 }, + memory: { + max: envs.MEMORY_MAX || Math.pow(2, 8), // The maximum number of items that remain in the cache. This must be a positive finite intger. + // https://github.com/isaacs/node-lru-cache#options + }, redis: { url: envs.REDIS_URL || 'redis://localhost:6379/', }, diff --git a/lib/middleware/cache/index.js b/lib/middleware/cache/index.js index 9aa7628b3d8d99..7e6bb926e68139 100644 --- a/lib/middleware/cache/index.js +++ b/lib/middleware/cache/index.js @@ -41,7 +41,7 @@ if (config.cache.type === 'redis') { value = JSON.stringify(value); } if (key) { - return pageCache.set(key, value, maxAge * 1000); + return pageCache.set(key, value, { ttl: maxAge * 1000 }); } }; } else { diff --git a/lib/middleware/cache/memory.js b/lib/middleware/cache/memory.js index 6eff393aa3b39c..1e7efe3a6cc86e 100644 --- a/lib/middleware/cache/memory.js +++ b/lib/middleware/cache/memory.js @@ -4,13 +4,13 @@ const config = require('@/config').value; const status = { available: false }; const pageCache = new Lru({ - maxAge: config.cache.routeExpire * 1000, - max: Infinity, + ttl: config.cache.routeExpire * 1000, + max: config.memory.max, }); const routeCache = new Lru({ - maxAge: config.cache.routeExpire * 1000, - max: Infinity, + ttl: config.cache.routeExpire * 1000, + max: config.memory.max, updateAgeOnGet: true, }); @@ -34,7 +34,7 @@ module.exports = { value = JSON.stringify(value); } if (key && status.available) { - return (refresh ? routeCache : pageCache).set(key, value, maxAge * 1000); + return (refresh ? routeCache : pageCache).set(key, value, { ttl: maxAge * 1000 }); } }, clients: { pageCache, routeCache }, diff --git a/package.json b/package.json index 64ec7b88646fc4..0ccb0b36d9dfca 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,7 @@ "koa-basic-auth": "4.0.0", "koa-favicon": "2.1.0", "koa-mount": "4.0.0", - "lru-cache": "6.0.0", + "lru-cache": "7.7.1", "lz-string": "1.4.4", "mailparser": "3.4.0", "markdown-it": "12.3.2", diff --git a/yarn.lock b/yarn.lock index e0adbec44acaf1..f05c639a1e7f61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9005,12 +9005,10 @@ lowercase-keys@^2.0.0: resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479" integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA== -lru-cache@6.0.0, lru-cache@^6.0.0: - version "6.0.0" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" - integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA== - dependencies: - yallist "^4.0.0" +lru-cache@7.7.1: + version "7.7.1" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.7.1.tgz#03d2846b1ad2dcc7931a9340b8711d9798fcb0c6" + integrity sha512-cRffBiTW8s73eH4aTXqBcTLU0xQnwGV3/imttRHGWCrbergmnK4D6JXQd8qin5z43HnDwRI+o7mVW0LEB+tpAw== lru-cache@^4.0.1, lru-cache@^4.1.2: version "4.1.5" @@ -9027,6 +9025,13 @@ lru-cache@^5.1.1: dependencies: yallist "^3.0.2" +lru-cache@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" + integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA== + dependencies: + yallist "^4.0.0" + lru_map@^0.3.3: version "0.3.3" resolved "https://registry.yarnpkg.com/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd"