fix(core): own cache transports and accept cleanup reviews
This commit is contained in:
parent
bd9332b2f6
commit
b4ac6460ef
24 changed files with 504 additions and 97 deletions
2
libs/core/cache/src/connection-manager.ts
vendored
2
libs/core/cache/src/connection-manager.ts
vendored
|
|
@ -50,6 +50,7 @@ export class RedisConnectionManager {
|
|||
* Create a new Redis connection
|
||||
*/
|
||||
private createConnection(config: RedisConfig): Redis {
|
||||
const retryDelay = config.retryDelay;
|
||||
return new Redis({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
|
|
@ -60,6 +61,7 @@ export class RedisConnectionManager {
|
|||
connectTimeout: config.connectTimeout ?? REDIS_DEFAULTS.CONNECT_TIMEOUT,
|
||||
commandTimeout: config.commandTimeout ?? REDIS_DEFAULTS.COMMAND_TIMEOUT,
|
||||
lazyConnect: false,
|
||||
...(retryDelay === undefined ? {} : { retryStrategy: () => retryDelay }),
|
||||
...(config.tls && { tls: config.tls }),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
12
libs/core/cache/src/redis-cache.ts
vendored
12
libs/core/cache/src/redis-cache.ts
vendored
|
|
@ -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}`;
|
||||
}
|
||||
|
|
|
|||
4
libs/core/cache/src/types.ts
vendored
4
libs/core/cache/src/types.ts
vendored
|
|
@ -9,6 +9,8 @@ export interface RedisConfig {
|
|||
keyPrefix?: string;
|
||||
maxRetriesPerRequest?: number;
|
||||
retryDelayOnFailover?: number;
|
||||
/** Fixed reconnect delay in milliseconds when explicitly configured. */
|
||||
retryDelay?: number;
|
||||
connectTimeout?: number;
|
||||
commandTimeout?: number;
|
||||
keepAlive?: number;
|
||||
|
|
@ -41,6 +43,8 @@ export interface CacheProvider {
|
|||
keys(pattern: string): Promise<string[]>;
|
||||
getStats(): CacheStats;
|
||||
health(): Promise<boolean>;
|
||||
/** Release an owned transport; borrowed/shared providers need not expose this. */
|
||||
disconnect?(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Wait for the cache to be ready and connected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue