From c0de9cd5ae1d7380c7d217cab38b6f7229392738 Mon Sep 17 00:00:00 2001 From: savvot <savvot@gmail.com> Date: Fri, 6 Jun 2014 02:57:00 +0200 Subject: [PATCH] yii\redis\Connection - Added support for unix socket fixes #3714 close #6186 --- extensions/redis/CHANGELOG.md | 2 ++ extensions/redis/Connection.php | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/extensions/redis/CHANGELOG.md b/extensions/redis/CHANGELOG.md index ef0d5d3..24faebf 100644 --- a/extensions/redis/CHANGELOG.md +++ b/extensions/redis/CHANGELOG.md @@ -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) +- Enh #3714: Added support for connecting to redis server using a unix socket (savvot, robregonm) + 2.0.0 October 12, 2014 ---------------------- diff --git a/extensions/redis/Connection.php b/extensions/redis/Connection.php index 801c66a..69acaf9 100644 --- a/extensions/redis/Connection.php +++ b/extensions/redis/Connection.php @@ -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. * + * 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. * When the server needs authentication, you can set the [[password]] property to * authenticate with the server after connect. @@ -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'. + * If [[unixSocket]] is specified, hostname and port will be ignored. */ public $hostname = 'localhost'; /** * @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; /** + * @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. * See http://redis.io/commands/auth */ @@ -246,10 +257,10 @@ class Connection extends Component if ($this->_socket !== null) { 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__); $this->_socket = @stream_socket_client( - 'tcp://' . $this->hostname . ':' . $this->port, + $this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port, $errorNumber, $errorDescription, $this->connectionTimeout ? $this->connectionTimeout : ini_get("default_socket_timeout") @@ -277,7 +288,7 @@ class Connection extends Component public function close() { 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__); $this->executeCommand('QUIT'); stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR); -- libgit2 0.27.1