Fixed wildcard clearing for memcached
authorAlexander Ebert <ebert@woltlab.com>
Sun, 23 Jun 2013 18:31:40 +0000 (20:31 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 23 Jun 2013 18:31:40 +0000 (20:31 +0200)
wcfsetup/install/files/lib/system/cache/source/MemcachedCacheSource.class.php

index 540c9b04dd955bf1dbfbfe674806b7ae6bd09996..3ae545ed6aa81a3a24b2314a7ed6f9a0a1e4d3e4 100644 (file)
@@ -93,9 +93,12 @@ class MemcachedCacheSource implements ICacheSource {
         */
        public function flush($cacheName, $useWildcard) {
                $cacheName = $this->prefix . $cacheName;
-               $this->memcached->delete($cacheName);
                
-               $this->updateMaster(null, $cacheName);
+               $resources = ($useWildcard) ? $this->getResources('~^' .  $cacheName. '(-[a-f0-9]+)?$~') : array($cacheName);
+               foreach ($resources as $resource) {
+                       $this->memcached->delete($resource);
+                       $this->updateMaster(null, $resource);
+               }
        }
        
        /**
@@ -238,4 +241,29 @@ class MemcachedCacheSource implements ICacheSource {
                // default TTL: 3 days
                return (60 * 60 * 24 * 3);
        }
+       
+       /**
+        * Gets a list of resources matching given pattern.
+        * 
+        * @param       string          $pattern
+        * @return      array<string>
+        */
+       protected function getResources($pattern) {
+               $resources = array();
+               $master = $this->memcached->get($this->prefix . 'master');
+               
+               if ($master !== false) {
+                       $master = @unserialize($master);
+                       
+                       if ($master !== false) {
+                               foreach ($master as $index => $key) {
+                                       if (preg_match($pattern, $key)) {
+                                               $resources[] = $key;
+                                       }
+                               }
+                       }
+               }
+               
+               return $resources;
+       }
 }