Browse Source

Fixed the Memcache::clear() to not flush all the server, just the variables associated with the prefix. Refs #1911

Juan Basso 14 years ago
parent
commit
3dd86ebfd4

+ 23 - 1
lib/Cake/Cache/Engine/MemcacheEngine.php

@@ -189,10 +189,32 @@ class MemcacheEngine extends CacheEngine {
 /**
  * Delete all keys from the cache
  *
+ * @param boolean $check
  * @return boolean True if the cache was successfully cleared, false otherwise
  */
 	public function clear($check) {
-		return $this->_Memcache->flush();
+		if ($check) {
+			return true;
+		}
+		foreach ($this->_Memcache->getExtendedStats('slabs') as $slabs) {
+			foreach (array_keys($slabs) as $slabId) {
+				if (!is_numeric($slabId)) {
+					continue;
+				}
+
+				foreach ($this->_Memcache->getExtendedStats('cachedump', $slabId) as $stats) {
+					if (!is_array($stats)) {
+						continue;
+					}
+					foreach (array_keys($stats) as $key) {
+						if (strpos($key, $this->settings['prefix']) === 0) {
+							$this->_Memcache->delete($key);
+						}
+					}
+				}
+			}
+		}
+		return true;
 	}
 
 /**

+ 14 - 1
lib/Cake/Test/Case/Cache/Engine/MemcacheTest.php

@@ -344,11 +344,24 @@ class MemcacheEngineTest extends CakeTestCase {
  * @return void
  */
 	public function testClear() {
-		Cache::write('some_value', 'value', 'memcache');
+		Cache::config('memcache2', array(
+			'engine' => 'Memcache',
+			'prefix' => 'cake2_',
+			'duration' => 3600
+		));
 
+		Cache::write('some_value', 'cache1', 'memcache');
+		$result = Cache::clear(true, 'memcache');
+		$this->assertTrue($result);
+		$this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
+
+		Cache::write('some_value', 'cache2', 'memcache2');
 		$result = Cache::clear(false, 'memcache');
 		$this->assertTrue($result);
 		$this->assertFalse(Cache::read('some_value', 'memcache'));
+		$this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
+
+		Cache::clear(false, 'memcache2');
 	}
 /**
  * test that a 0 duration can succesfully write.