Class yii\redis\Connection

Inheritanceyii\redis\Connection » yii\base\Component
Available since version2.0

The redis connection class is used to establish a connection to a redis server.

By default it assumes there is a redis server running on localhost at port 6379 and uses the database number 0.

It is possible to connect to a redis server using $hostname and $port or using a $unixSocket.

It also supports the AUTH command of redis. When the server needs authentication, you can set the $password property to authenticate with the server after connect.

The execution of redis commands is possible with via executeCommand().

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$connectionTimeout float Timeout to use for connection to redis. yii\redis\Connection
$dataTimeout float Timeout to use for redis socket when reading and writing data. yii\redis\Connection
$database integer The redis database to use. yii\redis\Connection
$hostname string The hostname or ip address to use for connecting to the redis server. yii\redis\Connection
$password string The password for establishing DB connection. yii\redis\Connection
$port integer The port to use for connecting to the redis server. yii\redis\Connection
$redisCommands array List of available redis commands. yii\redis\Connection
$retries integer The number of times a command execution should be retried when a connection failure occurs. yii\redis\Connection
$socketClientFlags integer Bitmask field which may be set to any combination of connection flags passed to [stream_socket_client()](http://php. yii\redis\Connection
$unixSocket string The unix socket path (e.g. `/var/run/redis/redis. yii\redis\Connection

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Allows issuing all supported commands via magic methods. yii\redis\Connection
__sleep() Closes the connection when this component is being serialized. yii\redis\Connection
close() Closes the currently active DB connection. yii\redis\Connection
executeCommand() Executes a redis command. yii\redis\Connection
getDriverName() Returns the name of the DB driver for the current \yii\redis\dsn. yii\redis\Connection
getIsActive() Returns a value indicating whether the DB connection is established. yii\redis\Connection
getLuaScriptBuilder() yii\redis\Connection
open() Establishes a DB connection. yii\redis\Connection

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
initConnection() Initializes the DB connection. yii\redis\Connection

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_AFTER_OPEN \yii\redis\Event An event that is triggered after a DB connection is established yii\redis\Connection

Property Details

$connectionTimeout public property

Timeout to use for connection to redis. If not set the timeout set in php.ini will be used: ini_get("default_socket_timeout").

public float $connectionTimeout null
$dataTimeout public property

Timeout to use for redis socket when reading and writing data. If not set the php default value will be used.

public float $dataTimeout null
$database public property

The redis database to use. This is an integer value starting from 0. Defaults to 0. Since version 2.0.6 you can disable the SELECT command sent after connection by setting this property to null.

public integer $database 0
$hostname public property

The hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'. If $unixSocket is specified, hostname and $port will be ignored.

public string $hostname 'localhost'
$password public property

The password for establishing DB connection. Defaults to null meaning no AUTH command is sent. See http://redis.io/commands/auth

public string $password null
$port public property

The port to use for connecting to the redis server. Default port is 6379. If $unixSocket is specified, $hostname and port will be ignored.

public integer $port 6379
$redisCommands public property

List of available redis commands.

See also http://redis.io/commands.

public array $redisCommands = ['APPEND''AUTH''BGREWRITEAOF''BGSAVE''BITCOUNT''BITFIELD''BITOP''BITPOS''BLPOP''BRPOP''BRPOPLPUSH''CLIENT KILL''CLIENT LIST''CLIENT GETNAME''CLIENT PAUSE''CLIENT REPLY''CLIENT SETNAME''CLUSTER ADDSLOTS''CLUSTER COUNTKEYSINSLOT''CLUSTER DELSLOTS''CLUSTER FAILOVER''CLUSTER FORGET''CLUSTER GETKEYSINSLOT''CLUSTER INFO''CLUSTER KEYSLOT''CLUSTER MEET''CLUSTER NODES''CLUSTER REPLICATE''CLUSTER RESET''CLUSTER SAVECONFIG''CLUSTER SETSLOT''CLUSTER SLAVES''CLUSTER SLOTS''COMMAND''COMMAND COUNT''COMMAND GETKEYS''COMMAND INFO''CONFIG GET''CONFIG REWRITE''CONFIG SET''CONFIG RESETSTAT''DBSIZE''DEBUG OBJECT''DEBUG SEGFAULT''DECR''DECRBY''DEL''DISCARD''DUMP''ECHO''EVAL''EVALSHA''EXEC''EXISTS''EXPIRE''EXPIREAT''FLUSHALL''FLUSHDB''GEOADD''GEOHASH''GEOPOS''GEODIST''GEORADIUS''GEORADIUSBYMEMBER''GET''GETBIT''GETRANGE''GETSET''HDEL''HEXISTS''HGET''HGETALL''HINCRBY''HINCRBYFLOAT''HKEYS''HLEN''HMGET''HMSET''HSET''HSETNX''HSTRLEN''HVALS''INCR''INCRBY''INCRBYFLOAT''INFO''KEYS''LASTSAVE''LINDEX''LINSERT''LLEN''LPOP''LPUSH''LPUSHX''LRANGE''LREM''LSET''LTRIM''MGET''MIGRATE''MONITOR''MOVE''MSET''MSETNX''MULTI''OBJECT''PERSIST''PEXPIRE''PEXPIREAT''PFADD''PFCOUNT''PFMERGE''PING''PSETEX''PSUBSCRIBE''PUBSUB''PTTL''PUBLISH''PUNSUBSCRIBE''QUIT''RANDOMKEY''READONLY''READWRITE''RENAME''RENAMENX''RESTORE''ROLE''RPOP''RPOPLPUSH''RPUSH''RPUSHX''SADD''SAVE''SCARD''SCRIPT DEBUG''SCRIPT EXISTS''SCRIPT FLUSH''SCRIPT KILL''SCRIPT LOAD''SDIFF''SDIFFSTORE''SELECT''SET''SETBIT''SETEX''SETNX''SETRANGE''SHUTDOWN''SINTER''SINTERSTORE''SISMEMBER''SLAVEOF''SLOWLOG''SMEMBERS''SMOVE''SORT''SPOP''SRANDMEMBER''SREM''STRLEN''SUBSCRIBE''SUNION''SUNIONSTORE''SWAPDB''SYNC''TIME''TOUCH''TTL''TYPE''UNSUBSCRIBE''UNLINK''UNWATCH''WAIT''WATCH''ZADD''ZCARD''ZCOUNT''ZINCRBY''ZINTERSTORE''ZLEXCOUNT''ZRANGE''ZRANGEBYLEX''ZREVRANGEBYLEX''ZRANGEBYSCORE''ZRANK''ZREM''ZREMRANGEBYLEX''ZREMRANGEBYRANK''ZREMRANGEBYSCORE''ZREVRANGE''ZREVRANGEBYSCORE''ZREVRANK''ZSCORE''ZUNIONSTORE''SCAN''SSCAN''HSCAN''ZSCAN']
$retries public property (available since version 2.0.7)

The number of times a command execution should be retried when a connection failure occurs. This is used in executeCommand() when a yii\redis\SocketException is thrown. Defaults to 0 meaning no retries on failure.

public integer $retries 0
$socketClientFlags public property (available since version 2.0.5)

Bitmask field which may be set to any combination of connection flags passed to stream_socket_client(). Currently the select of connection flags is limited to STREAM_CLIENT_CONNECT (default), STREAM_CLIENT_ASYNC_CONNECT and STREAM_CLIENT_PERSISTENT.

Warning: STREAM_CLIENT_PERSISTENT will make PHP reuse connections to the same server. If you are using multiple connection objects to refer to different redis databases on the same $port, redis commands may get executed on the wrong database. STREAM_CLIENT_PERSISTENT is only safe to use if you use only one database.

You may still use persistent connections in this case when disambiguating ports as described in a comment on the PHP manual e.g. on the connection used for session storage, specify the port as:

'port' => '6379/session'

See also http://php.net/manual/en/function.stream-socket-client.php.

public integer $socketClientFlags STREAM_CLIENT_CONNECT
$unixSocket public property (available since version 2.0.1)

The unix socket path (e.g. /var/run/redis/redis.sock) to use for connecting to the redis server. This can be used instead of $hostname and $port to connect to the server using a unix socket. If a unix socket path is specified, $hostname and $port will be ignored.

public string $unixSocket null

Method Details

__call() public method

Allows issuing all supported commands via magic methods.

$redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2')
public mixed __call ( $name, $params )
$name string

Name of the missing method to execute

$params array

Method call arguments

__sleep() public method

Closes the connection when this component is being serialized.

public array __sleep ( )
close() public method

Closes the currently active DB connection.

It does nothing if the connection is already closed.

public void close ( )
executeCommand() public method

Executes a redis command.

For a list of available commands and their parameters see http://redis.io/commands.

The params array should contain the params separated by white space, e.g. to execute SET mykey somevalue NX call the following:

$redis->executeCommand('SET', ['mykey', 'somevalue', 'NX']);
public array|boolean|null|string executeCommand ( $name, $params = [] )
$name string

The name of the command

$params array

List of parameters for the command

return array|boolean|null|string

Dependent on the executed command this method will return different data types:

  • true for commands that return "status reply" with the message 'OK' or 'PONG'.
  • string for commands that return "status reply" that does not have the message OK (since version 2.0.1).
  • string for commands that return "integer reply" as the value is in the range of a signed 64 bit integer.
  • string or null for commands that return "bulk reply".
  • array for commands that return "Multi-bulk replies".

See redis protocol description for details on the mentioned reply types.

throws \yii\db\Exception

for commands that return error reply.

getDriverName() public method

Returns the name of the DB driver for the current \yii\redis\dsn.

public string getDriverName ( )
return string

Name of the DB driver

getIsActive() public method

Returns a value indicating whether the DB connection is established.

public boolean getIsActive ( )
return boolean

Whether the DB connection is established

getLuaScriptBuilder() public method

public yii\redis\LuaScriptBuilder getLuaScriptBuilder ( )
initConnection() protected method

Initializes the DB connection.

This method is invoked right after the DB connection is established. The default implementation triggers an EVENT_AFTER_OPEN event.

protected void initConnection ( )
open() public method

Establishes a DB connection.

It does nothing if a DB connection has already been established.

public void open ( )
throws \yii\db\Exception

if connection fails

Event Details

EVENT_AFTER_OPEN event of type \yii\redis\Event

An event that is triggered after a DB connection is established