Commit c0de9cd5 by savvot Committed by Carsten Brandt

yii\redis\Connection - Added support for unix socket

fixes #3714 close #6186
parent 50bc0b05
...@@ -5,6 +5,8 @@ Yii Framework 2 redis extension Change Log ...@@ -5,6 +5,8 @@ Yii Framework 2 redis extension Change Log
----------------------- -----------------------
- Bug #4745: value of simple string returns was ignored by redis client and `true` is returned instead, now only `OK` will result in a `true` while all other values are returned as is (cebe) - Bug #4745: value of simple string returns was ignored by redis client and `true` is returned instead, now only `OK` will result in a `true` while all other values are returned as is (cebe)
- Enh #3714: Added support for connecting to redis server using a unix socket (savvot, robregonm)
2.0.0 October 12, 2014 2.0.0 October 12, 2014
---------------------- ----------------------
......
...@@ -16,6 +16,8 @@ use yii\helpers\Inflector; ...@@ -16,6 +16,8 @@ use yii\helpers\Inflector;
* *
* By default it assumes there is a redis server running on localhost at port 6379 and uses the database number 0. * 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](http://redis.io/commands/auth) of redis. * It also supports [the AUTH command](http://redis.io/commands/auth) of redis.
* When the server needs authentication, you can set the [[password]] property to * When the server needs authentication, you can set the [[password]] property to
* authenticate with the server after connect. * authenticate with the server after connect.
...@@ -42,13 +44,22 @@ class Connection extends Component ...@@ -42,13 +44,22 @@ class Connection extends Component
/** /**
* @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'. * @var string 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 = 'localhost'; public $hostname = 'localhost';
/** /**
* @var integer the port to use for connecting to the redis server. Default port is 6379. * @var integer 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 = 6379; public $port = 6379;
/** /**
* @var string 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;
/**
* @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send. * @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send.
* See http://redis.io/commands/auth * See http://redis.io/commands/auth
*/ */
...@@ -246,10 +257,10 @@ class Connection extends Component ...@@ -246,10 +257,10 @@ class Connection extends Component
if ($this->_socket !== null) { if ($this->_socket !== null) {
return; return;
} }
$connection = $this->hostname . ':' . $this->port . ', database=' . $this->database; $connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
\Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__); \Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__);
$this->_socket = @stream_socket_client( $this->_socket = @stream_socket_client(
'tcp://' . $this->hostname . ':' . $this->port, $this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port,
$errorNumber, $errorNumber,
$errorDescription, $errorDescription,
$this->connectionTimeout ? $this->connectionTimeout : ini_get("default_socket_timeout") $this->connectionTimeout ? $this->connectionTimeout : ini_get("default_socket_timeout")
...@@ -277,7 +288,7 @@ class Connection extends Component ...@@ -277,7 +288,7 @@ class Connection extends Component
public function close() public function close()
{ {
if ($this->_socket !== null) { if ($this->_socket !== null) {
$connection = $this->hostname . ':' . $this->port . ', database=' . $this->database; $connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
\Yii::trace('Closing DB connection: ' . $connection, __METHOD__); \Yii::trace('Closing DB connection: ' . $connection, __METHOD__);
$this->executeCommand('QUIT'); $this->executeCommand('QUIT');
stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR); stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment