fix(core): misc fixes of disabling cache (#15036)

* fix(core): CACHE_TYPE='' does not disable cache

The documentation of config always states that CACHE_TYPE being empty
will disable the cache.

The regression was introduced in 847724ca. The corresponding test case
was mistakenly "fixed" in 719af907.

Signed-off-by: Rongrong <i@rong.moe>

* fix(core): false RSS <ttl> when cache unavailable

Some RSS readers may probe <ttl> to determine the minimum interval of
polling. However, it is always set to `config.cache.routeExpire / 60`
even when cache is unavailable. This may not be expected when the cache
is explicitly disabled.

Set it to 1 (minute) under such a circumstance. The minimum <ttl> (when
cache is available) is also limited to 1 to avoid potential misuse.

The Cache-Control header remains untouched as caching can be done by a
reverse proxy.

Signed-off-by: Rongrong <i@rong.moe>

---------

Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
Rongrong
2024-04-02 01:21:38 +08:00
committed by GitHub
parent e9025a76da
commit 045eaf664c
3 changed files with 56 additions and 41 deletions
+1 -1
View File
@@ -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), // 不变内容缓存时间,单位为秒
+47 -39
View File
@@ -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');
});
});
+8 -1
View File
@@ -8,6 +8,13 @@ import { Data } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
// Set RSS <ttl> (minute) according to the availability of cache
// * available: max(config.cache.routeExpire / 60, 1)
// * unavailable: 1
// The minimum <ttl> 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,
};