diff --git a/lib/config.ts b/lib/config.ts index 2be41d9e5..8008e63c7 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -348,7 +348,7 @@ const calculateValue = () => { allowOrigin: envs.ALLOW_ORIGIN, // cache cache: { - type: envs.CACHE_TYPE || 'memory', // 缓存类型,支持 'memory' 和 'redis',设为空可以禁止缓存 + type: envs.CACHE_TYPE || (envs.CACHE_TYPE === '' ? '' : 'memory'), // 缓存类型,支持 'memory' 和 'redis',设为空可以禁止缓存 requestTimeout: toInt(envs.CACHE_REQUEST_TIMEOUT, 60), routeExpire: toInt(envs.CACHE_EXPIRE, 5 * 60), // 路由缓存时间,单位为秒 contentExpire: toInt(envs.CACHE_CONTENT_EXPIRE, 1 * 60 * 60), // 不变内容缓存时间,单位为秒 diff --git a/lib/middleware/cache.test.ts b/lib/middleware/cache.test.ts index 393d8153e..703da060c 100644 --- a/lib/middleware/cache.test.ts +++ b/lib/middleware/cache.test.ts @@ -11,6 +11,24 @@ afterEach(() => { vi.resetModules(); }); +const noCacheTestFunc = async () => { + const app = (await import('@/app')).default; + + const response1 = await app.request('/test/cache'); + const response2 = await app.request('/test/cache'); + + const parsed1 = await parser.parseString(await response1.text()); + const parsed2 = await parser.parseString(await response2.text()); + + expect(response2.status).toBe(200); + expect(response2.headers).not.toHaveProperty('rsshub-cache-status'); + + expect(parsed1.items[0].content).toBe('Cache1'); + expect(parsed2.items[0].content).toBe('Cache2'); + + expect(parsed1.ttl).toEqual('1'); +}; + describe('cache', () => { it('memory', async () => { process.env.CACHE_TYPE = 'memory'; @@ -33,6 +51,8 @@ describe('cache', () => { expect(response2.status).toBe(200); expect(response2.headers.get('rsshub-cache-status')).toBe('HIT'); + expect(parsed1.ttl).toEqual('1'); + await wait(1 * 1000 + 100); const response3 = await app.request('/test/cache'); expect(response3.headers).not.toHaveProperty('rsshub-cache-status'); @@ -81,6 +101,8 @@ describe('cache', () => { expect(response2.status).toBe(200); expect(response2.headers.get('rsshub-cache-status')).toBe('HIT'); + expect(parsed1.ttl).toEqual('1'); + await wait(1 * 1000 + 100); const response3 = await app.request('/test/cache'); expect(response3.headers).not.toHaveProperty('rsshub-cache-status'); @@ -114,57 +136,25 @@ describe('cache', () => { process.env.CACHE_TYPE = 'redis'; const cache = (await import('@/utils/cache')).default; await cache.clients.redisClient!.quit(); - const app = (await import('@/app')).default; - - const response1 = await app.request('/test/cache'); - const response2 = await app.request('/test/cache'); - - const parsed1 = await parser.parseString(await response1.text()); - const parsed2 = await parser.parseString(await response2.text()); - - expect(response2.status).toBe(200); - expect(response2.headers).not.toHaveProperty('rsshub-cache-status'); - - expect(parsed1.items[0].content).toBe('Cache1'); - expect(parsed2.items[0].content).toBe('Cache2'); + await noCacheTestFunc(); }); it('redis with error', async () => { process.env.CACHE_TYPE = 'redis'; process.env.REDIS_URL = 'redis://wrongpath:6379'; - const app = (await import('@/app')).default; - - const response1 = await app.request('/test/cache'); - const response2 = await app.request('/test/cache'); - - const parsed1 = await parser.parseString(await response1.text()); - const parsed2 = await parser.parseString(await response2.text()); - - expect(response2.status).toBe(200); - expect(response2.headers).not.toHaveProperty('rsshub-cache-status'); - - expect(parsed1.items[0].content).toBe('Cache1'); - expect(parsed2.items[0].content).toBe('Cache2'); - + await noCacheTestFunc(); const cache = (await import('@/utils/cache')).default; await cache.clients.redisClient!.quit(); }); it('no cache', async () => { process.env.CACHE_TYPE = 'NO'; - const app = (await import('@/app')).default; + await noCacheTestFunc(); + }); - const response1 = await app.request('/test/cache'); - const response2 = await app.request('/test/cache'); - - const parsed1 = await parser.parseString(await response1.text()); - const parsed2 = await parser.parseString(await response2.text()); - - expect(response2.status).toBe(200); - expect(response2.headers).not.toHaveProperty('rsshub-cache-status'); - - expect(parsed1.items[0].content).toBe('Cache1'); - expect(parsed2.items[0].content).toBe('Cache2'); + it('no cache (empty string)', async () => { + process.env.CACHE_TYPE = ''; + await noCacheTestFunc(); }); it('throws URL key', async () => { @@ -178,4 +168,22 @@ describe('cache', () => { expect(error.message).toContain('Cache key must be a string'); } }); + + it('RSS TTL (no cache)', async () => { + process.env.CACHE_TYPE = ''; + process.env.CACHE_EXPIRE = '600'; + const app = (await import('@/app')).default; + const response = await app.request('/test/cache'); + const parsed = await parser.parseString(await response.text()); + expect(parsed.ttl).toEqual('1'); + }); + + it('RSS TTL (w/ cache)', async () => { + process.env.CACHE_TYPE = 'memory'; + process.env.CACHE_EXPIRE = '600'; + const app = (await import('@/app')).default; + const response = await app.request('/test/cache'); + const parsed = await parser.parseString(await response.text()); + expect(parsed.ttl).toEqual('10'); + }); }); diff --git a/lib/middleware/template.tsx b/lib/middleware/template.tsx index 61b39fce6..85bc7cf39 100644 --- a/lib/middleware/template.tsx +++ b/lib/middleware/template.tsx @@ -8,6 +8,13 @@ import { Data } from '@/types'; import { getCurrentPath } from '@/utils/helpers'; const __dirname = getCurrentPath(import.meta.url); +// Set RSS (minute) according to the availability of cache +// * available: max(config.cache.routeExpire / 60, 1) +// * unavailable: 1 +// The minimum is limited to 1 minute to prevent potential misuse +import cacheModule from '@/utils/cache/index'; +const ttl = (cacheModule.status.available && Math.trunc(config.cache.routeExpire / 60)) || 1; + const middleware: MiddlewareHandler = async (ctx, next) => { await next(); @@ -74,7 +81,7 @@ const middleware: MiddlewareHandler = async (ctx, next) => { const result = { lastBuildDate: currentDate.toUTCString(), updated: currentDate.toISOString(), - ttl: Math.trunc(config.cache.routeExpire / 60), + ttl, atomlink: ctx.req.url, ...data, };