import Emittery from 'emittery'; import type { Logger } from '@adonisjs/core/logger'; import RedisConnection from './connections/redis_connection.ts'; import RedisClusterConnection from './connections/redis_cluster_connection.ts'; import type { GetConnectionType, IORedisBaseCommands, PubSubChannelHandler, PubSubPatternHandler, RedisConnectionsList } from './types.ts'; /** * Redis Manager exposes the API to manage multiple redis connections * based upon user defined config. * * All connections are long-lived until they are closed explictly * * @example * ```ts * const redis = new RedisManager({ * connection: 'main', * connections: { * main: { * host: 'localhost', * port: 6379 * } * } * }, logger) * * const connection = redis.connection('main') * await connection.set('key', 'value') * ``` */ declare class RedisManager extends Emittery<{ connection: RedisConnection | RedisClusterConnection; }> { #private; managerConfig: { connection: keyof ConnectionsList; connections: ConnectionsList; }; /** * Reference to "import('ioredis').Redis.Command" */ Command: typeof import("ioredis").Command; /** * A copy of live connections. We avoid re-creating a new connection * everytime and re-use connections. */ activeConnections: { [K in keyof ConnectionsList]?: GetConnectionType; }; /** * Returns the length of active connections */ get activeConnectionsCount(): number; /** * Create a new RedisManager instance * * @param managerConfig - Configuration for connections * @param logger - Logger instance for error reporting * * @example * ```ts * const manager = new RedisManager({ * connection: 'main', * connections: { * main: { host: 'localhost', port: 6379 } * } * }, logger) * ``` */ constructor(managerConfig: { connection: keyof ConnectionsList; connections: ConnectionsList; }, logger: Logger); /** * Disable error logging of redis connection errors. You must * handle the errors manually, otheriwse the app will crash * * @example * ```ts * redis.doNotLogErrors() * redis.connection().on('error', (error) => { * // Handle error manually * }) * ``` */ doNotLogErrors(): this; /** * Returns redis factory for a given named connection * * @param connectionName - Optional connection name, defaults to default connection * * @example * ```ts * const connection = redis.connection('cache') * await connection.set('key', 'value') * ``` */ connection(connectionName?: ConnectionName): GetConnectionType; /** * Subscribe to a given channel to receive Redis pub/sub events. A * new subscriber connection will be created/managed automatically. * * @param channel - The channel name to subscribe to * @param handler - Function to handle received messages * * @example * ```ts * redis.subscribe('notifications', (message) => { * console.log('Received:', message) * }) * ``` */ subscribe(channel: string, handler: PubSubChannelHandler): void; /** * Unsubscribe from a channel * * @param channel - The channel name to unsubscribe from * * @example * ```ts * await redis.unsubscribe('notifications') * ``` */ unsubscribe(channel: string): Promise; /** * Make redis subscription for a pattern * * @param pattern - The pattern to subscribe to * @param handler - Function to handle received pattern messages * * @example * ```ts * redis.psubscribe('news.*', (channel, message) => { * console.log(`Channel ${channel}:`, message) * }) * ``` */ psubscribe(pattern: string, handler: PubSubPatternHandler): void; /** * Unsubscribe from a given pattern * * @param pattern - The pattern to unsubscribe from * * @example * ```ts * await redis.punsubscribe('news.*') * ``` */ punsubscribe(pattern: string): Promise; /** * Publish the pub/sub message * * @param channel - The channel to publish to * @param message - The message to publish * @param callback - Optional callback for completion * * @example * ```ts * // Promise-based * const count = await redis.publish('notifications', 'Hello World') * * // Callback-based * redis.publish('notifications', 'Hello World', (err, count) => { * console.log('Published to', count, 'subscribers') * }) * ``` */ publish(channel: string, message: string, callback: (error: Error | null | undefined, count: number | undefined) => void): void; publish(channel: string, message: string): Promise; /** * Define a custom command using LUA script. You can run the * registered command using the "runCommand" method. * * @param name - The name of the command * @param definition - Command definition with lua script * * @example * ```ts * redis.defineCommand('incrementBy', { * lua: 'return redis.call("incrby", KEYS[1], ARGV[1])', * numberOfKeys: 1 * }) * ``` */ defineCommand(name: string, definition: { lua: string; numberOfKeys?: number; readOnly?: boolean; }): this; /** * Run a pre registered command on the default command * * @param command - The name of the registered command * @param args - Arguments to pass to the command * * @example * ```ts * const result = await redis.runCommand('incrementBy', 'key1', 5) * ``` */ runCommand(command: string, ...args: any[]): any; /** * Quit a named connection or the default connection when no * name is defined. * * @param name - Optional connection name to quit * * @example * ```ts * await redis.quit('cache') * await redis.quit() // quits default connection * ``` */ quit(name?: ConnectionName): Promise; /** * Disconnect a named connection or the default connection when no * name is defined. * * @param name - Optional connection name to disconnect * * @example * ```ts * await redis.disconnect('cache') * await redis.disconnect() // disconnects default connection * ``` */ disconnect(name?: ConnectionName): Promise; /** * Quit all connections * * @example * ```ts * await redis.quitAll() * ``` */ quitAll(): Promise; /** * Disconnect all connections * * @example * ```ts * await redis.disconnectAll() * ``` */ disconnectAll(): Promise; } interface RedisManager extends IORedisBaseCommands { } export default RedisManager;