Class yii\redis\Connection
Inheritance | yii\redis\Connection » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-redis/blob/master/Connection.php |
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 |
$redirectConnectionString | string | If the query gets redirected, use this as the temporary new hostname | 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 |
$retryInterval | integer | The retry interval in microseconds to wait between retry. | yii\redis\Connection |
$socketClientFlags | integer | Bitmask field which may be set to any combination of connection flags passed to [stream_socket_client()](https://www. | yii\redis\Connection |
$unixSocket | string | The unix socket path (e.g. `/var/run/redis/redis. | yii\redis\Connection |
$useSSL | boolean | Send sockets over SSL protocol. | yii\redis\Connection |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Allows issuing all supported commands via magic methods. | yii\redis\Connection |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__sleep() | Closes the connection when this component is being serialized. | yii\redis\Connection |
__unset() | Sets a component property to be null. | yii\base\Component |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
close() | Closes the currently active DB connection. | yii\redis\Connection |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
executeCommand() | Executes a redis command. | yii\redis\Connection |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getConnectionString() | Return the connection string used to open a socket connection. During a redirect (cluster mode) this will be the target of the redirect. | 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 | |
getSocket() | Return the connection resource if a connection to the target has been established before, false otherwise. |
yii\redis\Connection |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\base\BaseObject |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
open() | Establishes a DB connection. | yii\redis\Connection |
trigger() | Triggers an event. | yii\base\Component |
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 https://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.
If the query gets redirected, use this as the temporary new hostname
List of available redis commands.
See also https://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.
The retry interval in microseconds to wait between retry. This is used in executeCommand() when a yii\redis\SocketException is thrown. Defaults to 0 meaning no wait.
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 https://www.php.net/manual/en/function.stream-socket-client.php.
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.
Send sockets over SSL protocol. Default state is false.
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 https://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. |
Return the connection string used to open a socket connection. During a redirect (cluster mode) this will be the target of the redirect.
public string getConnectionString ( ) | ||
return | string | Socket connection string |
---|
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 ( ) |
Return the connection resource if a connection to the target has been established before, false
otherwise.
public resource|false getSocket ( ) |
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 |
---|