web/lib/Zend/Cache/Backend/ZendServer.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Cache
       
    17  * @subpackage Zend_Cache_Backend
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: ZendServer.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /** @see Zend_Cache_Backend_Interface */
       
    25 require_once 'Zend/Cache/Backend/Interface.php';
       
    26 
       
    27 /** @see Zend_Cache_Backend */
       
    28 require_once 'Zend/Cache/Backend.php';
       
    29 
       
    30 
       
    31 /**
       
    32  * @package    Zend_Cache
       
    33  * @subpackage Zend_Cache_Backend
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 abstract class Zend_Cache_Backend_ZendServer extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
       
    38 {
       
    39     /**
       
    40      * Available options
       
    41      *
       
    42      * =====> (string) namespace :
       
    43      * Namespace to be used for chaching operations
       
    44      *
       
    45      * @var array available options
       
    46      */
       
    47     protected $_options = array(
       
    48         'namespace' => 'zendframework'
       
    49     );
       
    50 
       
    51     /**
       
    52      * Store data
       
    53      *
       
    54      * @param mixed  $data        Object to store
       
    55      * @param string $id          Cache id
       
    56      * @param int    $timeToLive  Time to live in seconds
       
    57      * @throws Zend_Cache_Exception
       
    58      */
       
    59     abstract protected function _store($data, $id, $timeToLive);
       
    60 
       
    61     /**
       
    62      * Fetch data
       
    63      *
       
    64      * @param string $id          Cache id
       
    65      * @throws Zend_Cache_Exception
       
    66      */
       
    67     abstract protected function _fetch($id);
       
    68 
       
    69     /**
       
    70      * Unset data
       
    71      *
       
    72      * @param string $id          Cache id
       
    73      */
       
    74     abstract protected function _unset($id);
       
    75 
       
    76     /**
       
    77      * Clear cache
       
    78      */
       
    79     abstract protected function _clear();
       
    80 
       
    81     /**
       
    82      * Test if a cache is available for the given id and (if yes) return it (false else)
       
    83      *
       
    84      * @param  string  $id                     cache id
       
    85      * @param  boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
       
    86      * @return string cached datas (or false)
       
    87      */
       
    88     public function load($id, $doNotTestCacheValidity = false)
       
    89     {
       
    90         $tmp = $this->_fetch($id);
       
    91         if ($tmp !== null) {
       
    92             return $tmp;
       
    93         }
       
    94         return false;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Test if a cache is available or not (for the given id)
       
    99      *
       
   100      * @param  string $id cache id
       
   101      * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
       
   102      * @throws Zend_Cache_Exception
       
   103      */
       
   104     public function test($id)
       
   105     {
       
   106         $tmp = $this->_fetch('internal-metadatas---' . $id);
       
   107         if ($tmp !== false) {
       
   108             if (!is_array($tmp) || !isset($tmp['mtime'])) {
       
   109                 Zend_Cache::throwException('Cache metadata for \'' . $id . '\' id is corrupted' );
       
   110             }
       
   111             return $tmp['mtime'];
       
   112         }
       
   113         return false;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Compute & return the expire time
       
   118      *
       
   119      * @return int expire time (unix timestamp)
       
   120      */
       
   121     private function _expireTime($lifetime)
       
   122     {
       
   123         if ($lifetime === null) {
       
   124             return 9999999999;
       
   125         }
       
   126         return time() + $lifetime;
       
   127     }
       
   128 
       
   129     /**
       
   130      * Save some string datas into a cache record
       
   131      *
       
   132      * Note : $data is always "string" (serialization is done by the
       
   133      * core not by the backend)
       
   134      *
       
   135      * @param string $data datas to cache
       
   136      * @param string $id cache id
       
   137      * @param array $tags array of strings, the cache record will be tagged by each string entry
       
   138      * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
       
   139      * @return boolean true if no problem
       
   140      */
       
   141     public function save($data, $id, $tags = array(), $specificLifetime = false)
       
   142     {
       
   143         $lifetime = $this->getLifetime($specificLifetime);
       
   144         $metadatas = array(
       
   145             'mtime' => time(),
       
   146             'expire' => $this->_expireTime($lifetime),
       
   147         );
       
   148 
       
   149         if (count($tags) > 0) {
       
   150             $this->_log('Zend_Cache_Backend_ZendServer::save() : tags are unsupported by the ZendServer backends');
       
   151         }
       
   152 
       
   153         return  $this->_store($data, $id, $lifetime) &&
       
   154                 $this->_store($metadatas, 'internal-metadatas---' . $id, $lifetime);
       
   155     }
       
   156 
       
   157     /**
       
   158      * Remove a cache record
       
   159      *
       
   160      * @param  string $id cache id
       
   161      * @return boolean true if no problem
       
   162      */
       
   163     public function remove($id)
       
   164     {
       
   165         $result1 = $this->_unset($id);
       
   166         $result2 = $this->_unset('internal-metadatas---' . $id);
       
   167 
       
   168         return $result1 && $result2;
       
   169     }
       
   170 
       
   171     /**
       
   172      * Clean some cache records
       
   173      *
       
   174      * Available modes are :
       
   175      * 'all' (default)  => remove all cache entries ($tags is not used)
       
   176      * 'old'            => unsupported
       
   177      * 'matchingTag'    => unsupported
       
   178      * 'notMatchingTag' => unsupported
       
   179      * 'matchingAnyTag' => unsupported
       
   180      *
       
   181      * @param  string $mode clean mode
       
   182      * @param  array  $tags array of tags
       
   183      * @throws Zend_Cache_Exception
       
   184      * @return boolean true if no problem
       
   185      */
       
   186     public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
       
   187     {
       
   188         switch ($mode) {
       
   189             case Zend_Cache::CLEANING_MODE_ALL:
       
   190                 $this->_clear();
       
   191                 return true;
       
   192                 break;
       
   193             case Zend_Cache::CLEANING_MODE_OLD:
       
   194                 $this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends.");
       
   195                 break;
       
   196             case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
       
   197             case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
       
   198             case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
       
   199                 $this->_clear();
       
   200                 $this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.');
       
   201                 break;
       
   202             default:
       
   203                 Zend_Cache::throwException('Invalid mode for clean() method');
       
   204                 break;
       
   205         }
       
   206     }
       
   207 }