From ed9da6cb7fcad54742a4f76b5e1f9525aca2898d Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Wed, 29 May 2019 14:58:52 +0200 Subject: [PATCH] Memcached: Don't add the same server to the pool many times when using persistent connections --- program/lib/Roundcube/cache/memcached.php | 26 +++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/program/lib/Roundcube/cache/memcached.php b/program/lib/Roundcube/cache/memcached.php index cd9475a04..717e89767 100644 --- a/program/lib/Roundcube/cache/memcached.php +++ b/program/lib/Roundcube/cache/memcached.php @@ -90,8 +90,10 @@ class rcube_cache_memcached extends rcube_cache $pconnect = $rcube->config->get('memcache_pconnect', true); $timeout = $rcube->config->get('memcache_timeout', 1); $retry_interval = $rcube->config->get('memcache_retry_interval', 15); + $hosts = $rcube->config->get('memcache_hosts'); + $persistent_id = $pconnect ? ('rc' . md5(serialize($hosts))) : null; - self::$memcache = new Memcached($pconnect ? 'roundcube' : null); + self::$memcache = new Memcached($persistent_id); self::$memcache->setOptions(array( Memcached::OPT_CONNECT_TIMEOUT => $timeout * 1000, @@ -100,17 +102,19 @@ class rcube_cache_memcached extends rcube_cache Memcached::OPT_COMPRESSION => true, )); - foreach ((array) $rcube->config->get('memcache_hosts') as $host) { - if (substr($host, 0, 7) != 'unix://') { - list($host, $port) = explode(':', $host); - if (!$port) $port = 11211; + if (!$pconnect || !count(self::$memcache->getServerList())) { + foreach ((array) $hosts as $host) { + if (substr($host, 0, 7) != 'unix://') { + list($host, $port) = explode(':', $host); + if (!$port) $port = 11211; + } + else { + $host = substr($host, 8); + $port = 0; + } + + self::$memcache->addServer($host, $port); } - else { - $host = substr($host, 8); - $port = 0; - } - - self::$memcache->addServer($host, $port); } // test connection