PHP Class yii\redis\Connection

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 Connection::executeCommand.
Since: 2.0
Author: Carsten Brandt ([email protected])
Inheritance: extends yii\base\Component
Show file Open project: yiisoft/yii2-redis Class Usage Examples

Public Properties

Property Type Description
$connectionTimeout timeout to use for connection to redis. If not set the timeout set in php.ini will be used: ini_get("default_socket_timeout")
$dataTimeout timeout to use for redis socket when reading and writing data. If not set the php default value will be used.
$database 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.
$hostname 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.
$password the password for establishing DB connection. Defaults to null meaning no AUTH command is send. See http://redis.io/commands/auth
$port the port to use for connecting to the redis server. Default port is 6379. If [[unixSocket]] is specified, hostname and port will be ignored.
$redisCommands List of available redis commands http://redis.io/commands
$socketClientFlags 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.
$unixSocket 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 Methods

Method Description
__call ( string $name, array $params ) : mixed Allows issuing all supported commands via magic methods.
__sleep ( ) : array Closes the connection when this component is being serialized.
close ( ) Closes the currently active DB connection.
executeCommand ( string $name, array $params = [] ) : array | boolean | null | string Executes a redis command.
getDriverName ( ) : string Returns the name of the DB driver for the current [[dsn]].
getIsActive ( ) : boolean Returns a value indicating whether the DB connection is established.
getLuaScriptBuilder ( ) : yii\redis\LuaScriptBuilder
open ( ) Establishes a DB connection.

Protected Methods

Method Description
initConnection ( ) Initializes the DB connection.

Private Methods

Method Description
parseResponse ( string $command ) : mixed

Method Details

__call() public method

php $redis->hmset(['test_collection', 'key1', 'val1', 'key2', 'val2'])
public __call ( string $name, array $params ) : mixed
$name string name of the missing method to execute
$params array method call arguments
return mixed

__sleep() public method

Closes the connection when this component is being serialized.
public __sleep ( ) : array
return array

close() public method

It does nothing if the connection is already closed.
public close ( )

executeCommand() public method

For a list of available commands and their parameters see http://redis.io/commands.
public executeCommand ( string $name, array $params = [] ) : array | boolean | null | string
$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](http://redis.io/topics/protocol) for details on the mentioned reply types.

getDriverName() public method

Returns the name of the DB driver for the current [[dsn]].
public getDriverName ( ) : string
return string name of the DB driver

getIsActive() public method

Returns a value indicating whether the DB connection is established.
public getIsActive ( ) : boolean
return boolean whether the DB connection is established

getLuaScriptBuilder() public method

public getLuaScriptBuilder ( ) : yii\redis\LuaScriptBuilder
return yii\redis\LuaScriptBuilder

initConnection() protected method

This method is invoked right after the DB connection is established. The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
protected initConnection ( )

open() public method

It does nothing if a DB connection has already been established.
public open ( )

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 $connectionTimeout

$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 $dataTimeout

$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 $database

$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 $hostname

$password public property

the password for establishing DB connection. Defaults to null meaning no AUTH command is send. See http://redis.io/commands/auth
public $password

$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 $port

$redisCommands public property

List of available redis commands http://redis.io/commands
public $redisCommands

$socketClientFlags public property

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.
See also: http://php.net/manual/en/function.stream-socket-client.php
Since: 2.0.5
public $socketClientFlags

$unixSocket public property

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.
Since: 2.0.1
public $unixSocket