]> source.dussan.org Git - nextcloud-server.git/commitdiff
add prefix option to OC_Cache::clear
authorRobin Appelman <icewind@owncloud.com>
Sun, 22 Jul 2012 00:31:43 +0000 (02:31 +0200)
committerRobin Appelman <icewind@owncloud.com>
Sun, 22 Jul 2012 00:31:48 +0000 (02:31 +0200)
lib/cache.php
lib/cache/apc.php
lib/cache/broker.php
lib/cache/file.php
lib/cache/xcache.php
tests/lib/cache.php

index 6f4b3e6e3f638d9616ef31216b78ee36cc8fc397..36f409ab71a5f2cb4bba1cf1f16a9130e731b372 100644 (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
         */
-       static public function clear() {
+       static public function clear($prefix='') {
                $user_cache = self::getUserCache();
-               return $user_cache->clear();
+               return $user_cache->clear($prefix);
        }
 
        /**
index 6cf47d0c1586cfedaeb7937bcacc14f3e4f870e1..c192fe2f196817fbefeba9c6742215f221ac4fe3 100644 (file)
@@ -43,14 +43,15 @@ class OC_Cache_APC {
                return apc_delete($this->getNamespace().$key);
        }
 
-       public function clear(){
-               $ns = $this->getNamespace();
+       public function clear($prefix=''){
+               $ns = $this->getNamespace().$prefix;
                $cache = apc_cache_info('user');
                foreach($cache['cache_list'] as $entry) {
                        if (strpos($entry['info'], $ns) === 0) {
                                apc_delete($entry['info']);
                        }
                }
+               return true;
        }
 }
 if(!function_exists('apc_exists')) {
index 931d0dd407e64546d3ef41bf27ecb75b0f28594f..c2aceabaf53d8656e6864be14e8e1f537e59d8dd 100644 (file)
@@ -46,8 +46,8 @@ class OC_Cache_Broker {
                return $this->slow_cache->remove($key);
        }
 
-       public function clear(){
-               $this->fast_cache->clear();
-               $this->slow_cache->clear();
+       public function clear($prefix=''){
+               $this->fast_cache->clear($prefix);
+               $this->slow_cache->clear($prefix);
        }
 }
index 0b7d3e30508d4c9934bf7bf4a72bebf82ee2c288..562c3d1716780f162f8be62492c6aaca8bf92acc 100644 (file)
@@ -62,15 +62,16 @@ class OC_Cache_File{
                return $storage->unlink($key);
        }
 
-       public function clear(){
+       public function clear($prefix=''){
                $storage = $this->getStorage();
                if($storage and $storage->is_dir('/')){
                        $dh=$storage->opendir('/');
                        while($file=readdir($dh)){
-                               if($file!='.' and $file!='..'){
+                               if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)){
                                        $storage->unlink('/'.$file);
                                }
                        }
                }
+               return true;
        }
 }
index bd55cee8f6bbcb186d6a8781ffe528e36ebf6512..951f9b47545f6ef1a2790074d00ef4ee05d1f411 100644 (file)
@@ -43,7 +43,8 @@ class OC_Cache_XCache {
                return xcache_unset($this->getNamespace().$key);
        }
 
-       public function clear(){
-               return xcache_unset_by_prefix($this->getNamespace());
+       public function clear($prefix=''){
+               xcache_unset_by_prefix($this->getNamespace().$prefix);
+               return true;
        }
 }
index bb5cfc6ee19897a703212d545fecff0b4ebd2582..511999be956835a3dd0af684138982715bcab4f5 100644 (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->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(){