fix(core): own cache transports and accept cleanup reviews

This commit is contained in:
Boki 2026-10-26 23:57:39 -04:00
parent bd9332b2f6
commit b4ac6460ef
24 changed files with 504 additions and 97 deletions

View file

@ -61,13 +61,15 @@ export class QueueManager {
this.redisConnection = getRedisConnection(config.redis);
// Create ioredis connection for rate limiter
const retryDelay = config.redis.retryDelay;
this.ioredisConnection = new Redis({
host: config.redis.host,
port: config.redis.port,
password: config.redis.password,
username: config.redis.username,
db: config.redis.db || 0,
maxRetriesPerRequest: 3,
maxRetriesPerRequest: config.redis.maxRetriesPerRequest ?? 3,
...(retryDelay === undefined ? {} : { retryStrategy: () => retryDelay }),
enableReadyCheck: true,
});
@ -205,6 +207,7 @@ export class QueueManager {
if (!this.caches.has(queueName)) {
const cacheProvider = createCache({
redisConfig: this.config.redis,
shared: false,
keyPrefix: `batch:${queueName}:`,
ttl: 86400, // 24 hours default
enableMetrics: true,
@ -499,6 +502,8 @@ export class QueueManager {
await cache.clear();
} catch (error) {
this.logger.warn('Error clearing cache', { error: (error as Error).message });
} finally {
await cache.disconnect?.();
}
});

View file

@ -15,7 +15,8 @@ export class ServiceCache implements CacheProvider {
serviceName: string,
redisConfig: RedisConfig,
isGlobalCache: boolean = false,
logger?: LoggerLike
logger?: LoggerLike,
options: { shared?: boolean; ttl?: number } = {}
) {
// Determine Redis DB and prefix
let db: number;
@ -37,6 +38,8 @@ export class ServiceCache implements CacheProvider {
...redisConfig,
db,
},
shared: options.shared ?? true,
...(options.ttl === undefined ? {} : { ttl: options.ttl }),
keyPrefix: prefix + ':',
logger,
};
@ -45,6 +48,10 @@ export class ServiceCache implements CacheProvider {
this.prefix = prefix;
}
async disconnect(): Promise<void> {
await this.cache.disconnect?.();
}
// Implement CacheProvider interface
async get<T = unknown>(key: string): Promise<T | null> {
return this.cache.get<T>(key);
@ -173,7 +180,7 @@ export class ServiceCache implements CacheProvider {
export function createServiceCache(
serviceName: string,
redisConfig: RedisConfig,
options: { global?: boolean; logger?: LoggerLike } = {}
options: { global?: boolean; logger?: LoggerLike; shared?: boolean; ttl?: number } = {}
): ServiceCache {
return new ServiceCache(serviceName, redisConfig, options.global, options.logger);
return new ServiceCache(serviceName, redisConfig, options.global, options.logger, options);
}

View file

@ -44,6 +44,8 @@ export interface RedisConfig {
password?: string;
username?: string;
db?: number;
maxRetriesPerRequest?: number;
retryDelay?: number;
}
// Extended job options specific to this queue implementation

View file

@ -9,6 +9,6 @@ export function getRedisConnection(config: RedisConfig) {
connectTimeout: 3000,
lazyConnect: false,
keepAlive: 10000,
retryStrategy: (times: number) => Math.min(times * 100, 3000),
retryStrategy: (times: number) => config.retryDelay ?? Math.min(times * 100, 3000),
};
}