[BUGFIX] Make persistent connections available for Redis cache backend 12/51312/3
authorKasper Ligaard <kasperligaard+typo3.org@gmail.com>
Thu, 15 Dec 2016 13:59:26 +0000 (14:59 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Sat, 14 Jan 2017 22:25:45 +0000 (23:25 +0100)
The Redis cache backend allows configuration of all connection
setting, except setting persistent connections. This omission
is fixed by this patch. Being unable to use persistent connections
under high load can hurt performance in cloud setups. Persistent
connections has been part of phpredis since 2010 and thus is
broadly available, but is impossible to use with TYPO3.

Resolves: #79005
Releases: master, 7.6
Change-Id: Ie5476e894072bb9979691f28bc7a1e10e86ef454
Reviewed-on: https://review.typo3.org/51312
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Classes/Cache/Backend/RedisBackend.php

index 571f54f..8a99f73 100644 (file)
@@ -74,6 +74,13 @@ class RedisBackend extends AbstractBackend implements TaggableBackendInterface
     protected $connected = false;
 
     /**
+     * Persistent connection
+     *
+     * @var bool
+     */
+    protected $persistentConnection = false;
+
+    /**
      * Hostname / IP of the Redis server, defaults to 127.0.0.1.
      *
      * @var string
@@ -140,7 +147,11 @@ class RedisBackend extends AbstractBackend implements TaggableBackendInterface
     {
         $this->redis = new \Redis();
         try {
-            $this->connected = $this->redis->connect($this->hostname, $this->port);
+            if ($this->persistentConnection) {
+                $this->connected = $this->redis->pconnect($this->hostname, $this->port);
+            } else {
+                $this->connected = $this->redis->connect($this->hostname, $this->port);
+            }
         } catch (\Exception $e) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Could not connect to redis server.', 'core', \TYPO3\CMS\Core\Utility\GeneralUtility::SYSLOG_SEVERITY_ERROR);
         }
@@ -161,6 +172,18 @@ class RedisBackend extends AbstractBackend implements TaggableBackendInterface
     }
 
     /**
+     * Setter for persistent connection
+     *
+     * @param bool $persistentConnection
+     * @return void
+     * @api
+     */
+    public function setPersistentConnection($persistentConnection)
+    {
+        $this->persistentConnection = $persistentConnection;
+    }
+
+    /**
      * Setter for server hostname
      *
      * @param string $hostname Hostname