Skip to content

Commit

Permalink
Fixed the Memcache::clear() to not flush all the server, just the var…
Browse files Browse the repository at this point in the history
…iables associated with the prefix. Refs #1911
  • Loading branch information
jrbasso committed Aug 20, 2011
1 parent acdfb48 commit 3dd86eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/Cake/Cache/Engine/MemcacheEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,32 @@ public function delete($key) {
/**
* 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;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion lib/Cake/Test/Case/Cache/Engine/MemcacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,24 @@ public function testConfigurationConflict() {
* @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.
Expand Down

0 comments on commit 3dd86eb

Please sign in to comment.