API 参考
    正在准备搜索索引...

    接口 RedisConfig

    Redis 建立连接配置

    interface RedisConfig {
        autoPipeliningIgnoredCommands?: string[];
        autoResendUnfulfilledCommands?: boolean;
        autoResubscribe?: boolean;
        clusterNodes?: ClusterNode[];
        clusterOptions?: ClusterOptions;
        commandQueue?: boolean;
        commandTimeout?: number;
        connectionName?: string;
        Connector?: ConnectorConstructor;
        connectTimeout?: number;
        db?: number;
        disconnectTimeout?: number;
        enableAutoPipelining?: boolean;
        enableOfflineQueue?: boolean;
        enableReadyCheck?: boolean;
        enableTLSForSentinelMode?: boolean;
        failoverDetector?: boolean;
        family?: number;
        host?: string;
        keepAlive?: number;
        keyPrefix?: string;
        lazyConnect?: boolean;
        maxLoadingRetryTime?: number;
        maxRetriesPerRequest?: null | number;
        monitor?: boolean;
        name?: string;
        natMap?: NatMap;
        noDelay?: boolean;
        offlineQueue?: boolean;
        password?: string;
        path?: string;
        port?: number;
        preferredSlaves?: PreferredSlaves;
        readOnly?: boolean;
        reconnectOnError?: null | ReconnectOnError;
        retryStrategy?: (times: number) => null | number | void;
        role?: "master" | "slave";
        scripts?: Record<
            string,
            { lua: string; numberOfKeys?: number; readOnly?: boolean },
        >;
        sentinelCommandTimeout?: number;
        sentinelMaxConnections?: number;
        sentinelPassword?: string;
        sentinelReconnectStrategy?: (retryAttempts: number) => null | number | void;
        sentinelRetryStrategy?: (retryAttempts: number) => null | number | void;
        sentinels?: Partial<SentinelAddress>[];
        sentinelTLS?: ConnectionOptions;
        sentinelUsername?: string;
        showFriendlyErrorStack?: boolean;
        socketTimeout?: number;
        stringNumbers?: boolean;
        tls?: ConnectionOptions;
        updateSentinels?: boolean;
        url?: string;
        username?: string;
    }

    层级 (查看层级一览)

    索引

    属性

    autoPipeliningIgnoredCommands?: string[]
    []
    
    autoResendUnfulfilledCommands?: boolean

    Whether or not to resend unfulfilled commands on reconnect. Unfulfilled commands are most likely to be blocking commands such as brpop or blpop.

    true
    
    autoResubscribe?: boolean

    When the client reconnects, channels subscribed in the previous connection will be resubscribed automatically if autoResubscribe is true.

    true
    
    clusterNodes?: ClusterNode[]
    clusterOptions?: ClusterOptions
    commandQueue?: boolean
    commandTimeout?: number

    If a command does not return a reply within a set number of milliseconds, a "Command timed out" error will be thrown.

    connectionName?: string

    Set the name of the connection to make it easier to identity the connection in client list.

    Connector?: ConnectorConstructor
    connectTimeout?: number

    How long the client will wait before killing a socket due to inactivity during initial connection.

    10000
    
    db?: number

    Database index to use.

    0
    
    disconnectTimeout?: number
    enableAutoPipelining?: boolean
    false
    
    enableOfflineQueue?: boolean

    By default, if the connection to Redis server has not been established, commands are added to a queue and are executed once the connection is "ready" (when enableReadyCheck is true, "ready" means the Redis server has loaded the database from disk, otherwise means the connection to the Redis server has been established). If this option is false, when execute the command when the connection isn't ready, an error will be returned.

    true
    
    enableReadyCheck?: boolean

    The client will sent an INFO command to check whether the server is still loading data from the disk ( which happens when the server is just launched) when the connection is established, and only wait until the loading process is finished before emitting the ready event.

    true
    
    enableTLSForSentinelMode?: boolean
    failoverDetector?: boolean
    family?: number
    host?: string
    keepAlive?: number

    Enable/disable keep-alive functionality.

    keyPrefix?: string
    lazyConnect?: boolean

    When a Redis instance is initialized, a connection to the server is immediately established. Set this to true will delay the connection to the server until the first command is sent or redis.connect() is called explicitly.

    false
    
    maxLoadingRetryTime?: number
    10000
    
    maxRetriesPerRequest?: null | number

    The commands that don't get a reply due to the connection to the server is lost are put into a queue and will be resent on reconnect (if allowed by the retryStrategy option). This option is used to configure how many reconnection attempts should be allowed before the queue is flushed with a MaxRetriesPerRequestError error. Set this options to null instead of a number to let commands wait forever until the connection is alive again.

    20
    
    monitor?: boolean

    This option is used internally when you call redis.monitor() to tell Redis to enter the monitor mode when the connection is established.

    false
    
    name?: string

    Master group name of the Sentinel

    natMap?: NatMap
    noDelay?: boolean

    Enable/disable the use of Nagle's algorithm.

    offlineQueue?: boolean
    password?: string

    If set, client will send AUTH command with the value of this option when connected.

    path?: string
    port?: number
    preferredSlaves?: PreferredSlaves
    readOnly?: boolean
    false
    
    reconnectOnError?: null | ReconnectOnError

    Whether or not to reconnect on certain Redis errors. This options by default is null, which means it should never reconnect on Redis errors. You can pass a function that accepts an Redis error, and returns:

    • true or 1 to trigger a reconnection.
    • false or 0 to not reconnect.
    • 2 to reconnect and resend the failed command (who triggered the error) after reconnection.
    const redis = new Redis({
    reconnectOnError(err) {
    const targetError = "READONLY";
    if (err.message.includes(targetError)) {
    // Only reconnect when the error contains "READONLY"
    return true; // or `return 1;`
    }
    },
    });
    null
    
    retryStrategy?: (times: number) => null | number | void
    role?: "master" | "slave"
    "master"
    
    scripts?: Record<
        string,
        { lua: string; numberOfKeys?: number; readOnly?: boolean },
    >
    undefined
    
    sentinelCommandTimeout?: number
    sentinelMaxConnections?: number
    10
    
    sentinelPassword?: string
    sentinelReconnectStrategy?: (retryAttempts: number) => null | number | void
    sentinelRetryStrategy?: (retryAttempts: number) => null | number | void
    sentinels?: Partial<SentinelAddress>[]
    sentinelTLS?: ConnectionOptions
    sentinelUsername?: string
    showFriendlyErrorStack?: boolean
    socketTimeout?: number

    If the socket does not receive data within a set number of milliseconds:

    1. the socket is considered "dead" and will be destroyed
    2. the client will reject any running commands (altought they might have been processed by the server)
    3. the reconnect strategy will kick in (depending on the configuration)
    stringNumbers?: boolean

    When enabled, numbers returned by Redis will be converted to JavaScript strings instead of numbers. This is necessary if you want to handle big numbers (above Number.MAX_SAFE_INTEGER === 2^53).

    false
    
    tls?: ConnectionOptions
    updateSentinels?: boolean
    url?: string
    username?: string

    If set, client will send AUTH command with the value of this option as the first argument when connected. This is supported since Redis 6.