You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 KiB

Classes

Redis[EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
Cluster[EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
Commander
## Members
defaultOptions

Default options

defaultOptions

Default options

## Redis ⇐ [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) **Kind**: global class **Extends:** [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), [Commander](#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.
[options.connectionName] string null Connection name. Set to a non-number value to disable keepAlive.
[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.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.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 disconnected util a command is called, you can pass the lazyConnect option to the constructor:
[options.keyPrefix] string "''" The prefix to prepend to all keys in a command. 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.retryStrategy] function See "Quick Start" section
[options.reconnectOnError] function See "Quick Start" section

Example

var Redis = require('ioredis');

var redis = new Redis();
// or: var redis = 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

Create a connection to Redis. This method will be invoked automatically when creating a new Redis instance.

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) {
    console.log(time + ": " + util.inspect(args));
  });
});

// supports promise as well as other commands
redis.monitor().then(function (monitor) {
  monitor.on('monitor', function (time, args) {
    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.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.enableOfflineQueue] boolean true See Redis class
[options.lazyConnect] boolean false See Redis class
[options.readOnly] boolean false Connect in READONLY mode
[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.clusterRetryStrategy] function See "Quick Start" section
[options.retryDelayOnFailover] number 2000 When an error is received when sending a command(e.g. "Connection is closed." when the target Redis node is down),
[options.retryDelayOnClusterDown] number 1000 When a CLUSTERDOWN error is received, client will retry if retryDelayOnClusterDown is valid delay time.

cluster.disconnect()

Disconnect from every node in the cluster.

Kind: instance method of Cluster
Access: public

cluster.getBuiltinCommands() ⇒ Array.<string>

Return supported builtin commands

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

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.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

defaultOptions

Default options

Kind: global variable
Access: protected

defaultOptions

Default options

Kind: global variable
Access: protected