Skip to content

Commit

Permalink
feat: 新增 缓存配置
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Oct 24, 2024
1 parent 0ec79a2 commit 0e3e244
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export type Bindings = {
TIMEOUT: string
MAX_BODY_SIZE: string
RSSHUB_NODE_URLS: string
CACHE_MAX_AGE: string
}
2 changes: 2 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 0e3e244

Please sign in to comment.