add prefix option to OC_Cache::clear

This commit is contained in:
Robin Appelman 2012-07-22 02:31:43 +02:00
parent 2b74778958
commit 51566e87c7
6 changed files with 37 additions and 12 deletions

View File

@ -99,12 +99,13 @@ class OC_Cache {
} }
/** /**
* clear the user cache * clear the user cache of all entries starting with a prefix
* @param string prefix (optional)
* @return bool * @return bool
*/ */
static public function clear() { static public function clear($prefix='') {
$user_cache = self::getUserCache(); $user_cache = self::getUserCache();
return $user_cache->clear(); return $user_cache->clear($prefix);
} }
/** /**

5
lib/cache/apc.php vendored
View File

@ -43,14 +43,15 @@ class OC_Cache_APC {
return apc_delete($this->getNamespace().$key); return apc_delete($this->getNamespace().$key);
} }
public function clear(){ public function clear($prefix=''){
$ns = $this->getNamespace(); $ns = $this->getNamespace().$prefix;
$cache = apc_cache_info('user'); $cache = apc_cache_info('user');
foreach($cache['cache_list'] as $entry) { foreach($cache['cache_list'] as $entry) {
if (strpos($entry['info'], $ns) === 0) { if (strpos($entry['info'], $ns) === 0) {
apc_delete($entry['info']); apc_delete($entry['info']);
} }
} }
return true;
} }
} }
if(!function_exists('apc_exists')) { if(!function_exists('apc_exists')) {

View File

@ -46,8 +46,8 @@ class OC_Cache_Broker {
return $this->slow_cache->remove($key); return $this->slow_cache->remove($key);
} }
public function clear(){ public function clear($prefix=''){
$this->fast_cache->clear(); $this->fast_cache->clear($prefix);
$this->slow_cache->clear(); $this->slow_cache->clear($prefix);
} }
} }

5
lib/cache/file.php vendored
View File

@ -62,15 +62,16 @@ class OC_Cache_File{
return $storage->unlink($key); return $storage->unlink($key);
} }
public function clear(){ public function clear($prefix=''){
$storage = $this->getStorage(); $storage = $this->getStorage();
if($storage and $storage->is_dir('/')){ if($storage and $storage->is_dir('/')){
$dh=$storage->opendir('/'); $dh=$storage->opendir('/');
while($file=readdir($dh)){ while($file=readdir($dh)){
if($file!='.' and $file!='..'){ if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)){
$storage->unlink('/'.$file); $storage->unlink('/'.$file);
} }
} }
} }
return true;
} }
} }

View File

@ -43,7 +43,8 @@ class OC_Cache_XCache {
return xcache_unset($this->getNamespace().$key); return xcache_unset($this->getNamespace().$key);
} }
public function clear(){ public function clear($prefix=''){
return xcache_unset_by_prefix($this->getNamespace()); xcache_unset_by_prefix($this->getNamespace().$prefix);
return true;
} }
} }

View File

@ -42,6 +42,27 @@ abstract class Test_Cache extends UnitTestCase {
$this->assertNull($this->instance->get('not_set'),'Unset value not equal to null'); $this->assertNull($this->instance->get('not_set'),'Unset value not equal to null');
$this->assertTrue($this->instance->remove('value1')); $this->assertTrue($this->instance->remove('value1'));
$this->assertFalse($this->instance->hasKey('value1'));
}
function testClear(){
$value='ipsum lorum';
$this->instance->set('1_value1',$value);
$this->instance->set('1_value2',$value);
$this->instance->set('2_value1',$value);
$this->instance->set('3_value1',$value);
$this->assertTrue($this->instance->clear('1_'));
$this->assertFalse($this->instance->hasKey('1_value1'));
$this->assertFalse($this->instance->hasKey('1_value2'));
$this->assertTrue($this->instance->hasKey('2_value1'));
$this->assertTrue($this->instance->hasKey('3_value1'));
$this->assertTrue($this->instance->clear());
$this->assertFalse($this->instance->hasKey('1_value1'));
$this->assertFalse($this->instance->hasKey('1_value2'));
$this->assertFalse($this->instance->hasKey('2_value1'));
$this->assertFalse($this->instance->hasKey('3_value1'));
} }
function testTTL(){ function testTTL(){