Просмотр исходного кода

Added AutoFillFastCache option

Allow the fast cache to be (re-)filled automatically using information in the slow cache, without having to enable the AutoRefreshFastCache option, which refreshes the cache entries on EVERY request, causing a lot of expensive writes and removes cache entry hit count statistics.

Also see:
http://framework.zend.com/issues/browse/ZF-8431
http://www.fabrizio-branca.de/magento-zend-frameworks-twolevels-cache-backend-mess.html
http://blog.stoeckl.de/post/11996239388/how-to-fix-two-levels-cache-in-zend-framework
http://framework.zend.com/issues/browse/ZF-8431
Menno Holtkamp 12 лет назад
Родитель
Сommit
a7abc9d2fb
1 измененных файлов с 18 добавлено и 6 удалено
  1. 18 6
      library/Zend/Cache/Backend/TwoLevels.php

+ 18 - 6
library/Zend/Cache/Backend/TwoLevels.php

@@ -72,6 +72,11 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
      * =====> (boolean) fast_backend_autoload :
      * - See Zend_Cache::factory() method
      *
+     * =====> (boolean) auto_fill_fast_cache
+     * - If true, automatically fill the fast cache when a cache record was not found in fast cache, but did
+     *   exist in slow cache. This can be usefull when a non-persistent cache like APC or Memcached got
+     *   purged for whatever reason.
+     * 
      * =====> (boolean) auto_refresh_fast_cache
      * - If true, auto refresh the fast cache when a cache record is hit
      *
@@ -87,6 +92,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
         'fast_backend_custom_naming' => false,
         'slow_backend_autoload' => false,
         'fast_backend_autoload' => false,
+        'auto_fill_fast_cache' => true,
         'auto_refresh_fast_cache' => true
     );
 
@@ -216,6 +222,7 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
      * Test if a cache is available for the given id and (if yes) return it (false else)
      *
      * Note : return value is always "string" (unserialization is done by the core not by the backend)
+     * Note : only when an entry is not encountered in the fast cache, it will be auto-refreshed
      *
      * @param  string  $id                     Cache id
      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
@@ -223,17 +230,22 @@ class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Ca
      */
     public function load($id, $doNotTestCacheValidity = false)
     {
-        $res = $this->_fastBackend->load($id, $doNotTestCacheValidity);
-        if ($res === false) {
-            $res = $this->_slowBackend->load($id, $doNotTestCacheValidity);
-            if ($res === false) {
+        $resultFast = $this->_fastBackend->load($id, $doNotTestCacheValidity);
+        if ($resultFast === false) {
+            $resultSlow = $this->_slowBackend->load($id, $doNotTestCacheValidity);
+            if ($resultSlow === false) {
                 // there is no cache at all for this id
                 return false;
             }
         }
-        $array = unserialize($res);
+        $array = $resultFast !== false ? unserialize($resultFast) : unserialize($resultSlow);
+        
+        //In case no cache entry was found in the FastCache and auto-filling is enabled, copy data to FastCache
+        if ($resultFast === false && $this->_options['auto_fill_fast_cache']) {
+            $this->_fastBackend->save($array['data'], $id, array(), $array['lifetime']);
+        }
         // maybe, we have to refresh the fast cache ?
-        if ($this->_options['auto_refresh_fast_cache']) {
+        elseif ($this->_options['auto_refresh_fast_cache']) {
             if ($array['priority'] == 10) {
                 // no need to refresh the fast cache with priority = 10
                 return $array['data'];