Class yii\redis\Connection
Inheritance | yii\redis\Connection » yii\base\Component |
---|---|
Available since version | 2.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
Property | Type | Description | Defined 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
Method | Description | Defined 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
Method | Description | Defined By |
---|---|---|
initConnection() | Initializes the DB connection. | yii\redis\Connection |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_AFTER_OPEN | \yii\redis\Event | An event that is triggered after a DB connection is established | yii\redis\Connection |
Property Details
Timeout to use for connection to redis. If not set the timeout set in php.ini will be used: ini_get("default_socket_timeout")
.
Timeout to use for redis socket when reading and writing data. If not set the php default value will be used.
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
.
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.
The password for establishing DB connection. Defaults to null meaning no AUTH command is sent. See http://redis.io/commands/auth
The port to use for connecting to the redis server. Default port is 6379. If $unixSocket is specified, $hostname and port will be ignored.
List of available redis commands.
See also http://redis.io/commands.
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.
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.
Method Details
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 |
Closes the connection when this component is being serialized.
public array __sleep ( ) |
Closes the currently active DB connection.
It does nothing if the connection is already closed.
public void close ( ) |
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:
See redis protocol description for details on the mentioned reply types. |
---|---|---|
throws | \yii\db\Exception | for commands that return error reply. |
Returns the name of the DB driver for the current \yii\redis\dsn.
public string getDriverName ( ) | ||
return | string | Name of the DB driver |
---|
Returns a value indicating whether the DB connection is established.
public boolean getIsActive ( ) | ||
return | boolean | Whether the DB connection is established |
---|
public yii\redis\LuaScriptBuilder getLuaScriptBuilder ( ) |
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 ( ) |
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 |
---|