redis.ts•1.37 kB
import { RedisOptions } from 'ioredis';
import * as tls from 'tls';
export interface RedisConfig {
host: string;
port: number;
password?: string;
db?: number;
keyPrefix?: string;
tls?: tls.ConnectionOptions;
}
export interface LockOptions {
ttl?: number;
retryDelay?: number;
maxRetries?: number;
}
export const DEFAULT_LOCK_OPTIONS: Required<LockOptions> = {
ttl: 30000, // 30 seconds
retryDelay: 100, // 100ms
maxRetries: 10 // 10 retries
};
export function createRedisOptions(config: RedisConfig): RedisOptions {
return {
host: config.host,
port: config.port,
password: config.password,
db: config.db || 0,
keyPrefix: config.keyPrefix || '',
tls: config.tls,
retryStrategy: (times: number) => {
const delay = Math.min(times * 50, 2000);
return delay;
},
enableReadyCheck: true,
maxRetriesPerRequest: 3
};
}
export class RedisError extends Error {
constructor(message: string, public code: string) {
super(message);
this.name = 'RedisError';
}
}
export const RedisErrorCodes = {
CONNECTION_FAILED: 'CONNECTION_FAILED',
OPERATION_FAILED: 'OPERATION_FAILED',
LOCK_ACQUISITION_FAILED: 'LOCK_ACQUISITION_FAILED',
INVALID_CONFIG: 'INVALID_CONFIG'
} as const;