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

This commit is contained in:
Juan Basso 2011-08-19 22:12:37 -04:00
parent acdfb483a7
commit 3dd86ebfd4
2 changed files with 37 additions and 2 deletions

View file

@ -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;
}
/**

View file

@ -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.