web/lib/Zend/Cache/Backend/TwoLevels.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Cache
    16  * @package    Zend_Cache
    17  * @subpackage Zend_Cache_Backend
    17  * @subpackage Zend_Cache_Backend
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: TwoLevels.php 24593 2012-01-05 20:35:02Z matthew $
    20  * @version    $Id$
    21  */
    21  */
    22 
    22 
    23 
    23 
    24 /**
    24 /**
    25  * @see Zend_Cache_Backend_ExtendedInterface
    25  * @see Zend_Cache_Backend_ExtendedInterface
    33 
    33 
    34 
    34 
    35 /**
    35 /**
    36  * @package    Zend_Cache
    36  * @package    Zend_Cache
    37  * @subpackage Zend_Cache_Backend
    37  * @subpackage Zend_Cache_Backend
    38  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    40  */
    40  */
    41 
    41 
    42 class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
    42 class Zend_Cache_Backend_TwoLevels extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
    43 {
    43 {
    70      * =====> (boolean) fast_backend_custom_naming :
    70      * =====> (boolean) fast_backend_custom_naming :
    71      * =====> (boolean) slow_backend_autoload :
    71      * =====> (boolean) slow_backend_autoload :
    72      * =====> (boolean) fast_backend_autoload :
    72      * =====> (boolean) fast_backend_autoload :
    73      * - See Zend_Cache::factory() method
    73      * - See Zend_Cache::factory() method
    74      *
    74      *
       
    75      * =====> (boolean) auto_fill_fast_cache
       
    76      * - If true, automatically fill the fast cache when a cache record was not found in fast cache, but did
       
    77      *   exist in slow cache. This can be usefull when a non-persistent cache like APC or Memcached got
       
    78      *   purged for whatever reason.
       
    79      * 
    75      * =====> (boolean) auto_refresh_fast_cache
    80      * =====> (boolean) auto_refresh_fast_cache
    76      * - If true, auto refresh the fast cache when a cache record is hit
    81      * - If true, auto refresh the fast cache when a cache record is hit
    77      *
    82      *
    78      * @var array available options
    83      * @var array available options
    79      */
    84      */
    85         'stats_update_factor' => 10,
    90         'stats_update_factor' => 10,
    86         'slow_backend_custom_naming' => false,
    91         'slow_backend_custom_naming' => false,
    87         'fast_backend_custom_naming' => false,
    92         'fast_backend_custom_naming' => false,
    88         'slow_backend_autoload' => false,
    93         'slow_backend_autoload' => false,
    89         'fast_backend_autoload' => false,
    94         'fast_backend_autoload' => false,
       
    95         'auto_fill_fast_cache' => true,
    90         'auto_refresh_fast_cache' => true
    96         'auto_refresh_fast_cache' => true
    91     );
    97     );
    92 
    98 
    93     /**
    99     /**
    94      * Slow Backend
   100      * Slow Backend
   221      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
   227      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
   222      * @return string|false cached datas
   228      * @return string|false cached datas
   223      */
   229      */
   224     public function load($id, $doNotTestCacheValidity = false)
   230     public function load($id, $doNotTestCacheValidity = false)
   225     {
   231     {
   226         $res = $this->_fastBackend->load($id, $doNotTestCacheValidity);
   232         $resultFast = $this->_fastBackend->load($id, $doNotTestCacheValidity);
   227         if ($res === false) {
   233         if ($resultFast === false) {
   228             $res = $this->_slowBackend->load($id, $doNotTestCacheValidity);
   234             $resultSlow = $this->_slowBackend->load($id, $doNotTestCacheValidity);
   229             if ($res === false) {
   235             if ($resultSlow === false) {
   230                 // there is no cache at all for this id
   236                 // there is no cache at all for this id
   231                 return false;
   237                 return false;
   232             }
   238             }
   233         }
   239         }
   234         $array = unserialize($res);
   240         $array = $resultFast !== false ? unserialize($resultFast) : unserialize($resultSlow);
       
   241         
       
   242         //In case no cache entry was found in the FastCache and auto-filling is enabled, copy data to FastCache
       
   243         if ($resultFast === false && $this->_options['auto_fill_fast_cache']) {
       
   244             $preparedData = $this->_prepareData($array['data'], $array['lifetime'], $array['priority']);
       
   245             $this->_fastBackend->save($preparedData, $id, array(), $array['lifetime']);
       
   246         }
   235         // maybe, we have to refresh the fast cache ?
   247         // maybe, we have to refresh the fast cache ?
   236         if ($this->_options['auto_refresh_fast_cache']) {
   248         elseif ($this->_options['auto_refresh_fast_cache']) {
   237             if ($array['priority'] == 10) {
   249             if ($array['priority'] == 10) {
   238                 // no need to refresh the fast cache with priority = 10
   250                 // no need to refresh the fast cache with priority = 10
   239                 return $array['data'];
   251                 return $array['data'];
   240             }
   252             }
   241             $newFastLifetime = $this->_getFastLifetime($array['lifetime'], $array['priority'], time() - $array['expire']);
   253             $newFastLifetime = $this->_getFastLifetime($array['lifetime'], $array['priority'], time() - $array['expire']);