Fixes issue with outdated disk caches
authorMatthias Schmidt <gravatronics@live.com>
Sat, 25 May 2013 18:06:10 +0000 (20:06 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 25 May 2013 18:06:10 +0000 (20:06 +0200)
Fixes #1289

wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php
wcfsetup/install/files/lib/util/DirectoryUtil.class.php

index 36a5bc1f2e8c2a1a1ef193cee43e76f5ab401190..7c08f503d67425bf789be26bd74dd8957ffb9c5f 100644 (file)
@@ -18,6 +18,12 @@ use wcf\util\FileUtil;
  * @category   Community Framework
  */
 class DiskCacheSource implements ICacheSource {
+       /**
+        * up-to-date directory util object for the cache folder
+        * @var wcf\util\DirectoryUtil
+        */
+       protected $directoryUtil = null;
+       
        /**
         * @see wcf\system\cache\source\ICacheSource::flush()
         */
@@ -34,7 +40,7 @@ class DiskCacheSource implements ICacheSource {
         * @see wcf\system\cache\source\ICacheSource::flushAll()
         */
        public function flushAll() {
-               DirectoryUtil::getInstance(WCF_DIR.'cache/')->removePattern(new Regex('.*\.php$'));
+               $this->getDirectoryUtil()->removePattern(new Regex('.*\.php$'));
        }
        
        /**
@@ -63,6 +69,10 @@ class DiskCacheSource implements ICacheSource {
                $file->write("<?php exit; /* cache: ".$cacheName." (generated at ".gmdate('r').") DO NOT EDIT THIS FILE */ ?>\n");
                $file->write(serialize($value));
                $file->close();
+               
+               // unset current DirectoryUtil object to make sure new cache file
+               // can be deleted in the same request
+               $this->directoryUtil = null;
        }
        
        /**
@@ -84,7 +94,7 @@ class DiskCacheSource implements ICacheSource {
                $directory = FileUtil::unifyDirSeperator(WCF_DIR.'cache/');
                $pattern = str_replace('*', '.*', str_replace('.', '\.', $pattern));
                
-               DirectoryUtil::getInstance($directory)->executeCallback(new Callback(function ($filename) {
+               $this->getDirectoryUtil()->executeCallback(new Callback(function ($filename) {
                        if (!@touch($filename, 1)) {
                                @unlink($filename);
                        }
@@ -151,4 +161,17 @@ class DiskCacheSource implements ICacheSource {
                
                return $value;
        }
+       
+       /**
+        * Returns an up-to-date directory util object for the cache folder.
+        * 
+        * @return      wcf\util\DirectoryUtil
+        */
+       protected function getDirectoryUtil() {
+               if ($this->directoryUtil === null) {
+                       $this->directoryUtil = new DirectoryUtil(WCF_DIR.'cache/');
+               }
+               
+               return $this->directoryUtil;
+       }
 }
index 9b353ad7b666b9b06a1ca8822be2404047bcdd0e..4c7e24df5879c4948b802514421f2efaf55ed932 100644 (file)
@@ -65,6 +65,26 @@ final class DirectoryUtil {
                false => array()        // non-recursive instances
        );
        
+       /**
+        * Creates a new instance of DirectoryUtil.
+        * 
+        * @param       string          $directory      directory path
+        * @param       boolean         $recursive      created a recursive directory iterator
+        * @see         wcf\util\DirectoryUtil::getInstance()
+        */
+       public function __construct($directory, $recursive = true) {
+               $this->directory = $directory;
+               $this->recursive = $recursive;
+               
+               // handle iterator type
+               if ($this->recursive) {
+                       $this->obj = new \RecursiveDirectoryIterator($directory);
+               }
+               else {
+                       $this->obj = new \DirectoryIterator($directory);
+               }
+       }
+       
        /**
         * Returns an instance of DirectoryUtil (or child).
         * 
@@ -89,26 +109,6 @@ final class DirectoryUtil {
                return static::$instances[$recursive][$directory];
        }
        
-       /**
-        * Creates a new instance of DirectoryUtil.
-        * 
-        * @param       string          $directory      directory path
-        * @param       boolean         $recursive      created a recursive directory iterator
-        * @see         wcf\util\DirectoryUtil::getInstance()
-        */
-       protected function __construct($directory, $recursive = true) {
-               $this->directory = $directory;
-               $this->recursive = $recursive;
-               
-               // handle iterator type
-               if ($this->recursive) {
-                       $this->obj = new \RecursiveDirectoryIterator($directory);
-               }
-               else {
-                       $this->obj = new \DirectoryIterator($directory);
-               }
-       }
-       
        /**
         * @see wcf\util\DirectoryUtil::getInstance()
         */