feat: cache init
This commit is contained in:
parent
3f71f232bb
commit
498af241af
|
|
@ -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> | string | null;
|
||||
set: (key: string, value?: string | Record<string, any>, maxAge?: number) => any;
|
||||
status: {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue