Skip to content

Latest commit

 

History

History
492 lines (338 loc) · 41.1 KB

API.md

File metadata and controls

492 lines (338 loc) · 41.1 KB

Classes

RedisEventEmitter
ClusterEventEmitter
Commander

Redis ⇐ EventEmitter

Kind: global class
Extends: EventEmitter, Commander

new Redis([port], [host], [options])

Creates a Redis instance

Param Type Default Description
[port] number | string | Object 6379 Port of the Redis server, or a URL string(see the examples below), or the options object(see the third argument).
[host] string | Object "localhost" Host of the Redis server, when the first argument is a URL string, this argument is an object represents the options.
[options] Object Other options.
[options.port] number 6379 Port of the Redis server.
[options.host] string "localhost" Host of the Redis server.
[options.family] string 4 Version of IP stack. Defaults to 4.
[options.path] string null Local domain socket path. If set the port, host and family will be ignored.
[options.keepAlive] number 0 TCP KeepAlive on the socket with a X ms delay before start. Set to a non-number value to disable keepAlive.
[options.noDelay] boolean true Whether to disable the Nagle's Algorithm. By default we disable it to reduce the latency.
[options.connectionName] string null Connection name.
[options.db] number 0 Database index to use.
[options.password] string null If set, client will send AUTH command with the value of this option when connected.
[options.username] string null Similar to password. Provide this for Redis ACL support.
[options.dropBufferSupport] boolean false Drop the buffer support for better performance. This option is recommended to be enabled when handling large array response and you don't need the buffer support.
[options.enableReadyCheck] boolean true When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server not respond to any commands. To work around this, when this option is true, ioredis will check the status of the Redis server, and when the Redis server is able to process commands, a ready event will be emitted.
[options.enableOfflineQueue] boolean true By default, if there is no active connection to the Redis server, 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.
[options.connectTimeout] number 10000 The milliseconds before a timeout occurs during the initial connection to the Redis server.
[options.disconnectTimeout] number 2000 The milliseconds before socket.destroy() is called after socket.end() if the connection remains half-open during disconnection.
[options.commandTimeout] number The milliseconds before a timeout occurs when executing a single command. By default, there is no timeout and the client will wait indefinitely. The timeout is enforced only on the client side, not server side. The server may still complete the operation after a timeout error occurs on the client side.
[options.autoResubscribe] boolean true After reconnected, if the previous connection was in the subscriber mode, client will auto re-subscribe these channels.
[options.autoResendUnfulfilledCommands] boolean true If true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected.
[options.lazyConnect] boolean false By default, When a new Redis instance is created, it will connect to Redis server automatically. If you want to keep the instance disconnected until a command is called, you can pass the lazyConnect option to the constructor: javascript var redis = new Redis({ lazyConnect: true }); // No attempting to connect to the Redis server here. // Now let's connect to the Redis server redis.get('foo', function () { });
[options.tls] Object TLS connection support. See https://github.com/luin/ioredis#tls-options
[options.keyPrefix] string "''" The prefix to prepend to all keys in a command.
[options.retryStrategy] function See "Quick Start" section
[options.maxRetriesPerRequest] number See "Quick Start" section
[options.reconnectOnError] function See "Quick Start" section
[options.readOnly] boolean false Enable READONLY mode for the connection. Only available for cluster mode.
[options.stringNumbers] boolean false Force numbers to be always returned as JavaScript strings. This option is necessary when dealing with big numbers (exceed the [-2^53, +2^53] range).
[options.enableAutoPipelining] boolean false When enabled, all commands issued during an event loop iteration are automatically wrapped in a pipeline and sent to the server at the same time. This can improve performance by 30-50%.
[options.autoPipeliningIgnoredCommands] string[] [] The list of commands which must not be automatically wrapped in pipelines.
[options.maxScriptsCachingTime] number 60000 Default script definition caching time.

Example

var Redis = require("ioredis");

var redis = new Redis();

var redisOnPort6380 = new Redis(6380);
var anotherRedis = new Redis(6380, "192.168.100.1");
var unixSocketRedis = new Redis({ path: "/tmp/echo.sock" });
var unixSocketRedis2 = new Redis("/tmp/echo.sock");
var urlRedis = new Redis("redis://user:password@redis-service.com:6379/");
var urlRedis2 = new Redis("//localhost:6379");
var authedRedis = new Redis(6380, "192.168.100.1", { password: "password" });

redis.connect([callback]) ⇒ Promise.<void>

Create a connection to Redis. This method will be invoked automatically when creating a new Redis instance unless lazyConnect: true is passed.

When calling this method manually, a Promise is returned, which will be resolved when the connection status is ready.

Kind: instance method of Redis

Access: public

Param Type
[callback] function

redis.disconnect()

Disconnect from Redis.

This method closes the connection immediately, and may lose some pending replies that haven't written to client. If you want to wait for the pending replies, use Redis#quit instead.

Kind: instance method of Redis Access: public

redis.end()

Deprecated

Disconnect from Redis.

Kind: instance method of Redis

redis.duplicate()

Create a new instance with the same options as the current one.

Kind: instance method of Redis
Access: public

Example

var redis = new Redis(6380);
var anotherRedis = redis.duplicate();

redis.monitor([callback])

Listen for all requests received by the server in real time.

This command will create a new connection to Redis and send a MONITOR command via the new connection in order to avoid disturbing the current connection.

Kind: instance method of Redis

Access: public

Param Type Description
[callback] function The callback function. If omit, a promise will be returned.

Example

var redis = new Redis();
redis.monitor(function (err, monitor) {
  // Entering monitoring mode.
  monitor.on("monitor", function (time, args, source, database) {
    console.log(time + ": " + util.inspect(args));
  });
});

// supports promise as well as other commands
redis.monitor().then(function (monitor) {
  monitor.on("monitor", function (time, args, source, database) {
    console.log(time + ": " + util.inspect(args));
  });
});

redis.getBuiltinCommands() ⇒ Array.<string>

Return supported builtin commands

Kind: instance method of Redis
Returns: Array.<string> - command list
Access: public

redis.addBuiltinCommand(commandName) ⇒ object

Adds a builtin command

Kind: instance method of Redis
Returns: void
Access: public

Param Type Description
commandName string command name

redis.createBuiltinCommand(commandName) ⇒ object

Create a builtin command

Kind: instance method of Redis
Returns: object - functions

Access: public

Param Type Description
commandName string command name

redis.defineCommand(name, definition)

Define a custom command using lua script

Kind: instance method of Redis

Param Type Default Description
name string the command name
definition object
definition.lua string the lua code
[definition.numberOfKeys] number the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command

Redis.createClient()

Deprecated

Create a Redis instance

Kind: static method of Redis

Cluster ⇐ EventEmitter

Kind: global class
Extends: EventEmitter, Commander

new Cluster(startupNodes, options)

Creates a Redis Cluster instance

Param Type Default Description
startupNodes Array.<Object> An array of nodes in the cluster, [{ port: number, host: string }]
options Object
[options.clusterRetryStrategy] function See "Quick Start" section
[options.dnsLookup] function(hostname, function(err, addr, family)) dns.lookup Function used to resolve DNS hostnames of Redis cluster members.
[options.enableOfflineQueue] boolean true See Redis class
[options.enableReadyCheck] boolean true When enabled, ioredis only emits "ready" event when CLUSTER INFO command reporting the cluster is ready for handling commands.
[options.scaleReads] string "master" Scale reads to the node with the specified role. Available values are "master", "slave" and "all".
[options.maxRedirections] number 16 When a MOVED or ASK error is received, client will redirect the command to another node. This option limits the max redirections allowed to send a command.
[options.retryDelayOnFailover] number 100 When an error is received when sending a command(e.g. "Connection is closed." when the target Redis node is down),
[options.retryDelayOnClusterDown] number 100 When a CLUSTERDOWN error is received, client will retry if retryDelayOnClusterDown is valid delay time.
[options.retryDelayOnTryAgain] number 100 When a TRYAGAIN error is received, client will retry if retryDelayOnTryAgain is valid delay time.
[options.slotsRefreshTimeout] number 1000 The milliseconds before a timeout occurs while refreshing slots from the cluster.
[options.slotsRefreshInterval] number 5000 The milliseconds between every automatic slots refresh.
[options.redisOptions] Object Passed to the constructor of Redis.

cluster.connect() ⇒ Promise

Connect to a cluster

Kind: instance method of Cluster Access: public

cluster.disconnect([reconnect])

Disconnect from every node in the cluster.

Kind: instance method of Cluster

Access: public

Param Type
[reconnect] boolean

cluster.quit([callback]) ⇒ Promise

Quit the cluster gracefully.

Kind: instance method of Cluster
Returns: Promise - return 'OK' if successfully

Access: public

Param Type
[callback] function

cluster.nodes([role]) ⇒ Array.<Redis>

Get nodes with the specified role

Kind: instance method of Cluster
Returns: Array.<Redis> - array of nodes

Access: public

Param Type Default Description
[role] string "all" role, "master", "slave" or "all"

cluster.getBuiltinCommands() ⇒ Array.<string>

Return supported builtin commands

Kind: instance method of Cluster
Returns: Array.<string> - command list
Access: public

cluster.addBuiltinCommand(commandName) ⇒ object

Adds a builtin command

Kind: instance method of Cluster
Returns: void
Access: public

Param Type Description
commandName string command name

cluster.createBuiltinCommand(commandName) ⇒ object

Create a builtin command

Kind: instance method of Cluster
Returns: object - functions

Access: public

Param Type Description
commandName string command name

cluster.defineCommand(name, definition)

Define a custom command using lua script

Kind: instance method of Cluster

Param Type Default Description
name string the command name
definition object
definition.lua string the lua code
[definition.numberOfKeys] number the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command

cluster.sendCommand()

Send a command

Kind: instance abstract method of Cluster Overrides: sendCommand Access: public

Commander

Kind: global class

new Commander()

Commander

This is the base class of Redis, Redis.Cluster and Pipeline

Param Type Default Description
[options.showFriendlyErrorStack] boolean false Whether to show a friendly error stack. Will decrease the performance significantly.

commander.getBuiltinCommands() ⇒ Array.<string>

Return supported builtin commands

Kind: instance method of Commander
Returns: Array.<string> - command list
Access: public

commander.addBuiltinCommand(commandName) ⇒ object

Adds a builtin command

Kind: instance method of Commander
Returns: void
Access: public

Param Type Description
commandName string command name

commander.createBuiltinCommand(commandName) ⇒ object

Create a builtin command

Kind: instance method of Commander
Returns: object - functions

Access: public

Param Type Description
commandName string command name

commander.defineCommand(name, definition)

Define a custom command using lua script

Kind: instance method of Commander

Param Type Default Description
name string the command name
definition object
definition.lua string the lua code
[definition.numberOfKeys] number the number of keys. If omit, you have to pass the number of keys as the first argument every time you invoke the command

commander.sendCommand()

Send a command

Kind: instance abstract method of Commander

Access: public