Procházet zdrojové kódy

Patched extension support for static caching to include removal/clean ops based on inner cache

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20687 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic před 16 roky
rodič
revize
79b7186855

+ 32 - 7
library/Zend/Cache/Backend/Static.php

@@ -101,7 +101,13 @@ class Zend_Cache_Backend_Static
         if ($name == 'tag_cache') {
             return $this->getInnerCache();
         } else {
-            return parent::getOption($name);
+            if (in_array($name, $this->_options)) {
+                return $this->_options[$name];
+            }
+            if ($name == 'lifetime') {
+                return parent::getLifetime();
+            }
+            return null;
         }
     }
 
@@ -159,8 +165,20 @@ class Zend_Cache_Backend_Static
         if (empty($fileName)) {
             $fileName = $this->_options['index_filename'];
         }
+        if (is_null($this->_tagged) && $tagged = $this->getInnerCache()->load(self::INNER_CACHE_NAME)) {
+            $this->_tagged = $tagged;
+        } elseif (!$this->_tagged) {
+            return false;
+        }
         $pathName = $this->_options['public_dir'] . dirname($id);
-        $file     = $pathName . '/' . $fileName . $this->_options['file_extension'];
+        
+        // Switch extension if needed
+        if (isset($this->_tagged[$id])) {
+            $extension = $this->_tagged[$id]['extension'];
+        } else {
+            $extension = $this->_options['file_extension'];
+        }
+        $file     = $pathName . '/' . $fileName . $extension;
         if (file_exists($file)) {
             return true;
         }
@@ -184,6 +202,11 @@ class Zend_Cache_Backend_Static
         if ($this->_options['disable_caching']) {
             return true;
         }
+        $extension = null;
+        if (is_array($data)) {
+            $extension = '.' . ltrim($data[1], '.');
+            $data = $data[0];
+        }
         clearstatcache();
         if (is_null($id) || strlen($id) == 0) {
             $id = $this->_detectId();
@@ -203,8 +226,9 @@ class Zend_Cache_Backend_Static
             $dataUnserialized = unserialize($data);
             $data = $dataUnserialized['data'];
         }
-
-        $file = rtrim($pathName, '/') . '/' . $fileName . $this->_options['file_extension'];
+        $ext = $this->_options['file_extension'];
+        if ($extension) $ext = $extension;
+        $file = rtrim($pathName, '/') . '/' . $fileName . $ext;
         if ($this->_options['file_locking']) {
             $result = file_put_contents($file, $data, LOCK_EX);
         } else {
@@ -221,7 +245,8 @@ class Zend_Cache_Backend_Static
             if (!isset($this->_tagged[$id])) {
                 $this->_tagged[$id] = array();
             }
-            $this->_tagged[$id] = array_unique(array_merge($this->_tagged[$id], $tags));
+            $this->_tagged[$id]['tags'] = array_unique(array_merge($this->_tagged[$id], $tags));
+            $this->_tagged[$id]['extension'] = $ext;
             $this->getInnerCache()->save($this->_tagged, self::INNER_CACHE_NAME);
         }
         return (bool) $result;
@@ -341,7 +366,7 @@ class Zend_Cache_Backend_Static
                 foreach ($tags as $tag) {
                     $urls = array_keys($this->_tagged);
                     foreach ($urls as $url) {
-                        if (in_array($tag, $this->_tagged[$url])) {
+                        if (in_array($tag, $this->_tagged[$url]['tags'])) {
                             $this->remove($url);
                             unset($this->_tagged[$url]);
                         }
@@ -382,7 +407,7 @@ class Zend_Cache_Backend_Static
                 }
                 $urls = array_keys($this->_tagged);
                 foreach ($urls as $url) {
-                    $difference = array_diff($tags, $this->_tagged[$url]);
+                    $difference = array_diff($tags, $this->_tagged[$url]['tags']);
                     if (count($tags) == count($difference)) {
                         $this->remove($url);
                         unset($this->_tagged[$url]);

+ 9 - 3
library/Zend/Cache/Frontend/Capture.php

@@ -45,6 +45,8 @@ class Zend_Cache_Frontend_Capture extends Zend_Cache_Core
      * @var array
      */
     protected $_tags = array();
+    
+    protected $_extension = null;
 
     /**
      * Start the cache
@@ -52,9 +54,10 @@ class Zend_Cache_Frontend_Capture extends Zend_Cache_Core
      * @param  string  $id Cache id
      * @return mixed True if the cache is hit (false else) with $echoData=true (default) ; string else (datas)
      */
-    public function start($id, array $tags)
+    public function start($id, array $tags, $extension = null)
     {
         $this->_tags = $tags;
+        $this->_extension = $extension;
         ob_start(array($this, '_flush'));
         ob_implicit_flush(false);
         $this->_idStack[] = $id;
@@ -74,8 +77,11 @@ class Zend_Cache_Frontend_Capture extends Zend_Cache_Core
         if (is_null($id)) {
             Zend_Cache::throwException('use of _flush() without a start()');
         }
-        file_put_contents('/var/www/data.dump', $data);
-        $this->save($data, $id, $this->_tags);
+        if ($this->_extension) {
+            $this->save(array($data, $this->_extension), $id, $this->_tags);
+        } else {
+            $this->save($data, $id, $this->_tags);
+        }
         return $data;
     }
 }

+ 21 - 2
library/Zend/Controller/Action/Helper/Cache.php

@@ -64,6 +64,13 @@ class Zend_Controller_Action_Helper_Cache
      * @var array
      */
     protected $_tags = array();
+    
+    /**
+     * Indexed map of Extensions by Controller and Action
+     *
+     * @var array
+     */
+    protected $_extensions = array();
 
     /**
      * Track output buffering condition
@@ -78,7 +85,7 @@ class Zend_Controller_Action_Helper_Cache
      * @param array $tags
      * @return void
      */
-    public function direct(array $actions, array $tags = array())
+    public function direct(array $actions, array $tags = array(), $extension = null)
     {
         $controller = $this->getRequest()->getControllerName();
         $actions = array_unique($actions);
@@ -100,6 +107,14 @@ class Zend_Controller_Action_Helper_Cache
                 }
             }
         }
+        if ($extension) {
+            if (!isset($this->_extensions[$controller])) {
+                $this->_extensions[$controller] = array();
+            }
+            foreach ($actions as $action) {
+                $this->_extensions[$controller][$action] = $extension;
+            }
+        }
     }
 
     /**
@@ -166,8 +181,12 @@ class Zend_Controller_Action_Helper_Cache
             && !empty($this->_tags[$controller][$action])) {
                 $tags = array_unique($this->_tags[$controller][$action]);
             }
+            $extension = null;
+            if ($this->_extensions[$controller][$action]) {
+                $extension = $this->_extensions[$controller][$action];
+            }
             $this->getCache(Zend_Cache_Manager::PAGECACHE)
-                ->start($this->_encodeCacheId($reqUri), $tags);
+                ->start($this->_encodeCacheId($reqUri), $tags, $extension);
         }
     }
     

+ 7 - 0
tests/Zend/Cache/StaticBackendTest.php

@@ -118,6 +118,13 @@ class Zend_Cache_StaticBackendTest extends Zend_Cache_CommonBackendTest {
         $this->assertTrue($res);
     }
 
+    public function testSaveWithSpecificExtension()
+    {
+        $res = $this->_instance->save(array('data to cache', 'xml'), bin2hex('/foo'), array('tag1'));
+        $this->assertTrue($this->_instance->test(bin2hex('/foo')));
+        unlink($this->_instance->getOption('public_dir') . '/foo.xml');
+    }
+
     public function testTestWithAnExistingCacheId()
     {
         $res = $this->_instance->test(bin2hex('/bar'));