From 0e3e244c46bb1e2263443c32d9ac30404b651ad3 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Thu, 24 Oct 2024 22:32:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 +++ src/app.ts | 10 +++++++++- src/types.d.ts | 1 + wrangler.toml | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 8c1a5b7..ab96bcb 100644 --- a/.env +++ b/.env @@ -18,3 +18,6 @@ LOGFILES=false # RSSHub 实例 的 URL 地址,,使用英文逗号分隔。 # 官方实例 https://rsshub.app 不用列出,默认添加。 RSSHUB_NODE_URLS='https://rsshub.rssforever.com, https://hub.slarker.me, https://rsshub.pseudoyu.com, https://rsshub.ktachibana.party, https://rsshub.woodland.cafe, https://rss.owo.nz, https://yangzhi.app, https://rsshub.henry.wang, https://rss.peachyjoy.top, https://rsshub.speednet.icu' + +# 缓存时间(秒) +CACHE_MAX_AGE=300 diff --git a/src/app.ts b/src/app.ts index 10b195c..7449e5e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,8 +3,9 @@ import { timeout } from 'hono/timeout' import { cors } from 'hono/cors' import { secureHeaders } from 'hono/secure-headers' import { showRoutes } from 'hono/dev' -import { env, getRuntimeKey } from 'hono/adapter' +import { env } from 'hono/adapter' import { bodyLimit } from 'hono/body-limit' +import { cache } from 'hono/cache' import { __DEV__ } from './env' import { loggerMiddleware } from './middlewares/logger' import { errorhandler, notFoundHandler } from './middlewares/error' @@ -21,6 +22,13 @@ app.use((c, next) => { const MAX_BODY_SIZE = parseInt(env(c).MAX_BODY_SIZE) || 100 * 1024 * 1024 // 默认 100MB return bodyLimit({ maxSize: MAX_BODY_SIZE })(c, next) }) +app.use((c, next) => { + const CACHE_MAX_AGE = parseInt(env(c).CACHE_MAX_AGE) || 300 // 默认 5 分钟缓存 + return cache({ + cacheName: 'rsshub-cache', + cacheControl: `max-age=${CACHE_MAX_AGE}`, + })(c, next) +}) app.use(cors()) app.use(secureHeaders()) diff --git a/src/types.d.ts b/src/types.d.ts index 51c561d..a707f61 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -6,4 +6,5 @@ export type Bindings = { TIMEOUT: string MAX_BODY_SIZE: string RSSHUB_NODE_URLS: string + CACHE_MAX_AGE: string } diff --git a/wrangler.toml b/wrangler.toml index aa02cdc..bd692aa 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -10,6 +10,8 @@ compatibility_flags = ["nodejs_compat"] TIMEOUT = 60000 # 最大请求体大小(字节),默认 100MB MAX_BODY_SIZE = 104857600 +# 缓存时间(秒) +CACHE_MAX_AGE = 300 # RSSHub 实例 的 URL 地址,,使用英文逗号分隔。 # 官方实例 https://rsshub.app 不用列出,默认添加。 RSSHUB_NODE_URLS = 'https://rsshub.rssforever.com, https://hub.slarker.me, https://rsshub.pseudoyu.com, https://rsshub.ktachibana.party, https://rsshub.woodland.cafe, https://rss.owo.nz, https://yangzhi.app, https://rsshub.henry.wang, https://rss.peachyjoy.top, https://rsshub.speednet.icu'