diff --git a/lib/utils/cache/base.ts b/lib/utils/cache/base.ts index ce9659701..aaf9b8ccc 100644 --- a/lib/utils/cache/base.ts +++ b/lib/utils/cache/base.ts @@ -2,6 +2,7 @@ import type Redis from 'ioredis'; import type { LRUCache } from 'lru-cache'; type CacheModule = { + init: () => void; get: (key: string, refresh?: boolean) => Promise | string | null; set: (key: string, value?: string | Record, maxAge?: number) => any; status: { diff --git a/lib/utils/cache/index.ts b/lib/utils/cache/index.ts index 6dc0bd77c..fd7b793f0 100644 --- a/lib/utils/cache/index.ts +++ b/lib/utils/cache/index.ts @@ -15,6 +15,7 @@ let cacheModule: CacheModule if (config.cache.type === 'redis') { cacheModule = redis; + cacheModule.init(); const { redisClient } = cacheModule.clients; globalCache.get = async (key) => { if (key && cacheModule.status.available && redisClient) { @@ -25,6 +26,7 @@ if (config.cache.type === 'redis') { globalCache.set = cacheModule.set; } else { cacheModule = memory; + cacheModule.init(); const { memoryCache } = cacheModule.clients; globalCache.get = (key) => { if (key && cacheModule.status.available && memoryCache) { diff --git a/lib/utils/cache/memory.ts b/lib/utils/cache/memory.ts index 57f0e5574..519e42615 100644 --- a/lib/utils/cache/memory.ts +++ b/lib/utils/cache/memory.ts @@ -4,14 +4,16 @@ import type CacheModule from './base'; const status = { available: false }; -const memoryCache = new LRUCache({ - ttl: config.cache.routeExpire * 1000, - max: config.memory.max, -}); - -status.available = true; +let memoryCache: LRUCache<{}, {}>; export default { + init: () => { + memoryCache = new LRUCache({ + ttl: config.cache.routeExpire * 1000, + max: config.memory.max, + }); + status.available = true; + }, get: (key: string, refresh = true) => { if (key && status.available) { let value = memoryCache.get(key, { updateAgeOnGet: refresh }) as string | undefined; diff --git a/lib/utils/cache/redis.ts b/lib/utils/cache/redis.ts index 7e6708557..c27fd7d5a 100644 --- a/lib/utils/cache/redis.ts +++ b/lib/utils/cache/redis.ts @@ -3,22 +3,10 @@ import Redis from 'ioredis'; import logger from '@/utils/logger'; import type CacheModule from './base'; -const redisClient = new Redis(config.redis.url); +let redisClient: Redis; const status = { available: false }; -redisClient.on('error', (error) => { - status.available = false; - logger.error('Redis error: ', error); -}); -redisClient.on('end', () => { - status.available = false; -}); -redisClient.on('connect', () => { - status.available = true; - logger.info('Redis connected.'); -}); - const getCacheTtlKey = (key: string) => { if (key.startsWith('rsshub:cacheTtl:')) { throw new Error('"rsshub:cacheTtl:" prefix is reserved for the internal usage, please change your cache key'); // blocking any attempt to get/set the cacheTtl @@ -27,6 +15,23 @@ const getCacheTtlKey = (key: string) => { }; export default { + init: () => { + redisClient = new Redis(config.redis.url); + + const status = { available: false }; + + redisClient.on('error', (error) => { + status.available = false; + logger.error('Redis error: ', error); + }); + redisClient.on('end', () => { + status.available = false; + }); + redisClient.on('connect', () => { + status.available = true; + logger.info('Redis connected.'); + }); + }, get: async (key: string, refresh = true) => { if (key && status.available) { const cacheTtlKey = getCacheTtlKey(key);