Browse Source

Throw an exception on invalid cache configuration, #14985

bancer 5 years ago
parent
commit
419717e7b0

+ 18 - 11
src/Cache/Engine/MemcachedEngine.php

@@ -145,7 +145,24 @@ class MemcachedEngine extends CacheEngine
         }
         $this->_setOptions();
 
-        if (count($this->_Memcached->getServerList())) {
+        $serverList = $this->_Memcached->getServerList();
+        if (count($serverList)) {
+            if ($this->_config['persistent'] !== false) {
+                $actualServers = [];
+                foreach ($serverList as $server) {
+                    $actualServers[] = $server['host'] . ':' .$server['port'];
+                }
+                unset($server);
+                sort($actualServers);
+                $configuredServers = $this->_config['servers'];
+                sort($configuredServers);
+                if ($actualServers !== $configuredServers) {
+                    $message = "Invalid cache configuration. Multiple persistent cache configurations are detected" .
+                        " with different 'servers' values. 'servers' values for persistent cache configurations" .
+                        " must be the same when using the same persistence id.";
+                    throw new InvalidArgumentException($message);
+                }
+            }
             return true;
         }
 
@@ -523,14 +540,4 @@ class MemcachedEngine extends CacheEngine
     {
         return (bool)$this->_Memcached->increment($this->_config['prefix'] . $group);
     }
-
-    /**
-     * Get the list of the servers in the pool.
-     *
-     * @return array
-     */
-    public function getServerList()
-    {
-        return $this->_Memcached->getServerList();
-    }
 }

+ 3 - 18
tests/TestCase/Cache/Engine/MemcachedEngineTest.php

@@ -361,8 +361,10 @@ class MemcachedEngineTest extends TestCase
     }
 
     /**
-     * testPhpSerializerSetting method
+     * testConfigDifferentPorts method
      *
+     * @expectedException InvalidArgumentException
+     * @expectedExceptionMessage Invalid cache configuration. Multiple persistent cache configurations are detected with different 'servers' values. 'servers' values for persistent cache configurations must be the same when using the same persistence id.
      * @return void
      */
     public function testConfigDifferentPorts()
@@ -374,14 +376,6 @@ class MemcachedEngineTest extends TestCase
             'persistent' => true,
         ];
         $Memcached1->init($config1);
-        $expectedServers = [
-            [
-                'host' => '127.0.0.1',
-                'port' => 11211,
-                'type' => 'TCP',
-            ],
-        ];
-        $this->assertEquals($expectedServers, $Memcached1->getServerList());
 
         $Memcached2 = new MemcachedEngine();
         $config2 = [
@@ -389,16 +383,7 @@ class MemcachedEngineTest extends TestCase
             'servers' => ['127.0.0.1:11212'],
             'persistent' => true,
         ];
-
         $Memcached2->init($config2);
-        $expectedServers = [
-            [
-                'host' => '127.0.0.1',
-                'port' => 11212,
-                'type' => 'TCP',
-            ],
-        ];
-        $this->assertEquals($expectedServers, $Memcached2->getServerList());
     }
 
     /**