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

@ -8,6 +8,8 @@ import type { CacheOptions, CacheProvider, CacheStats } from './types';
*/
export class RedisCache implements CacheProvider {
private redis: Redis;
private readonly ownsConnection: boolean;
private disconnected = false;
private logger: NonNullable<CacheOptions['logger']>;
private defaultTTL: number;
private keyPrefix: string;
@ -15,6 +17,7 @@ export class RedisCache implements CacheProvider {
private startTime = Date.now();
constructor(options: CacheOptions) {
this.ownsConnection = options.shared === false;
this.defaultTTL = options.ttl ?? CACHE_DEFAULTS.TTL;
this.keyPrefix = options.keyPrefix ?? CACHE_DEFAULTS.KEY_PREFIX;
this.logger = options.logger || console;
@ -30,6 +33,15 @@ export class RedisCache implements CacheProvider {
});
}
/** Only nonshared caches own their socket. Shared callers keep manager ownership. */
async disconnect(): Promise<void> {
if (this.ownsConnection && !this.disconnected) {
this.disconnected = true;
// A cache has no flush protocol; disconnect also stops retry handles when Redis is unavailable.
this.redis.disconnect();
}
}
private getKey(key: string): string {
return `${this.keyPrefix}${key}`;
}