Fixed [XF2] Redis Cache: Workaround for default value of "persistent_id"


Well-known member
Using PHP 7.1.9 + Phpredis 3.1.3 / 3.1.4 RC1, I'm trying to make XenForo 2.0 Beta 2 share a persistent Redis connection with other PHP code. XenForo uses an empty string as a default value for the "persistent_id". Unfortunately, Phpredis does not treat an empty string the same as omitting the parameter completely. As a consequence, a Redis connection that was established without passing this optional parameter is not re-used when calling the pconnect method with "persistent_id" set to the empty string.

I've filed a bug against Phpredis such that this issue may eventually be fixed:

However, since the workaround is very simple, maybe you could add it in XenForo 2 Beta 3?

--- a/src/XF/CacheFactory.php
+++ b/src/XF/CacheFactory.php
@@ -202,10 +202,14 @@ class CacheFactory
         $r = new \Redis();
-        if ($config['persistent'])
+        if ($config['persistent'] && $config['persistent_id'])
             $r->pconnect($config['host'], $config['port'], $config['timeout'], $config['persistent_id']);
+        else if ($config['persistent'])
+        {
+            $r->pconnect($config['host'], $config['port'], $config['timeout']);
+        }
             $r->connect($config['host'], $config['port'], $config['timeout']);

Chris D

XenForo developer
Staff member
Seems reasonable. Consider it implemented like that for the next release (I’ll apply your patch later).