From 05be10b035a753de965fd1dfcc71291fa4af2caf Mon Sep 17 00:00:00 2001 From: Joonas Loppi Date: Sat, 13 Apr 2013 13:18:18 +0300 Subject: [PATCH] Support for persistent connections: #persistent fragment in DSN --- src/redisent/Redis.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/redisent/Redis.php b/src/redisent/Redis.php index 76ae11f..86da884 100644 --- a/src/redisent/Redis.php +++ b/src/redisent/Redis.php @@ -29,6 +29,13 @@ class Redis { */ private $__sock; + /** + * Whether the connection is to be persistent or not + * @var boolean + * @access private + */ + private $__sockIsPersistent; + /** * The structure representing the data source of the Redis server * @var array @@ -58,10 +65,14 @@ class Redis { */ function __construct($dsn = 'redis://localhost:6379', $timeout = null) { $this->dsn = parse_url($dsn); + $this->__sockIsPersistent = !empty($this->dsn['fragment']) && $this->dsn['fragment'] == 'persistent'; $host = isset($this->dsn['host']) ? $this->dsn['host'] : 'localhost'; $port = isset($this->dsn['port']) ? $this->dsn['port'] : 6379; $timeout = $timeout ?: ini_get("default_socket_timeout"); - $this->__sock = @fsockopen($host, $port, $errno, $errstr, $timeout); + $this->__sock = $this->__sockIsPersistent ? + @pfsockopen($host, $port, $errno, $errstr, $timeout) : + @fsockopen($host, $port, $errno, $errstr, $timeout); + if ($this->__sock === FALSE) { throw new \Exception("{$errno} - {$errstr}"); } @@ -71,7 +82,9 @@ function __construct($dsn = 'redis://localhost:6379', $timeout = null) { } function __destruct() { - fclose($this->__sock); + if ($this->__sockIsPersistent == false) { + fclose($this->__sock); + } } /**