File indexing completed on 2025-01-19 05:20:57

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Cache
0017  * @subpackage Zend_Cache_Backend
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /** @see Zend_Cache_Backend_Interface */
0025 // require_once 'Zend/Cache/Backend/Interface.php';
0026 
0027 /** @see Zend_Cache_Backend */
0028 // require_once 'Zend/Cache/Backend.php';
0029 
0030 
0031 /**
0032  * @package    Zend_Cache
0033  * @subpackage Zend_Cache_Backend
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 abstract class Zend_Cache_Backend_ZendServer extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface
0038 {
0039     /**
0040      * Available options
0041      *
0042      * =====> (string) namespace :
0043      * Namespace to be used for chaching operations
0044      *
0045      * @var array available options
0046      */
0047     protected $_options = array(
0048         'namespace' => 'zendframework'
0049     );
0050 
0051     /**
0052      * Store data
0053      *
0054      * @param mixed  $data        Object to store
0055      * @param string $id          Cache id
0056      * @param int    $timeToLive  Time to live in seconds
0057      * @throws Zend_Cache_Exception
0058      */
0059     abstract protected function _store($data, $id, $timeToLive);
0060 
0061     /**
0062      * Fetch data
0063      *
0064      * @param string $id          Cache id
0065      * @throws Zend_Cache_Exception
0066      */
0067     abstract protected function _fetch($id);
0068 
0069     /**
0070      * Unset data
0071      *
0072      * @param string $id          Cache id
0073      */
0074     abstract protected function _unset($id);
0075 
0076     /**
0077      * Clear cache
0078      */
0079     abstract protected function _clear();
0080 
0081     /**
0082      * Test if a cache is available for the given id and (if yes) return it (false else)
0083      *
0084      * @param  string  $id                     cache id
0085      * @param  boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
0086      * @return string cached datas (or false)
0087      */
0088     public function load($id, $doNotTestCacheValidity = false)
0089     {
0090         $tmp = $this->_fetch($id);
0091         if ($tmp !== null) {
0092             return $tmp;
0093         }
0094         return false;
0095     }
0096 
0097     /**
0098      * Test if a cache is available or not (for the given id)
0099      *
0100      * @param  string $id cache id
0101      * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
0102      * @throws Zend_Cache_Exception
0103      */
0104     public function test($id)
0105     {
0106         $tmp = $this->_fetch('internal-metadatas---' . $id);
0107         if ($tmp !== false) {
0108             if (!is_array($tmp) || !isset($tmp['mtime'])) {
0109                 Zend_Cache::throwException('Cache metadata for \'' . $id . '\' id is corrupted' );
0110             }
0111             return $tmp['mtime'];
0112         }
0113         return false;
0114     }
0115 
0116     /**
0117      * Compute & return the expire time
0118      *
0119      * @return int expire time (unix timestamp)
0120      */
0121     private function _expireTime($lifetime)
0122     {
0123         if ($lifetime === null) {
0124             return 9999999999;
0125         }
0126         return time() + $lifetime;
0127     }
0128 
0129     /**
0130      * Save some string datas into a cache record
0131      *
0132      * Note : $data is always "string" (serialization is done by the
0133      * core not by the backend)
0134      *
0135      * @param string $data datas to cache
0136      * @param string $id cache id
0137      * @param array $tags array of strings, the cache record will be tagged by each string entry
0138      * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
0139      * @return boolean true if no problem
0140      */
0141     public function save($data, $id, $tags = array(), $specificLifetime = false)
0142     {
0143         $lifetime = $this->getLifetime($specificLifetime);
0144         $metadatas = array(
0145             'mtime' => time(),
0146             'expire' => $this->_expireTime($lifetime),
0147         );
0148 
0149         if (count($tags) > 0) {
0150             $this->_log('Zend_Cache_Backend_ZendServer::save() : tags are unsupported by the ZendServer backends');
0151         }
0152 
0153         return  $this->_store($data, $id, $lifetime) &&
0154                 $this->_store($metadatas, 'internal-metadatas---' . $id, $lifetime);
0155     }
0156 
0157     /**
0158      * Remove a cache record
0159      *
0160      * @param  string $id cache id
0161      * @return boolean true if no problem
0162      */
0163     public function remove($id)
0164     {
0165         $result1 = $this->_unset($id);
0166         $result2 = $this->_unset('internal-metadatas---' . $id);
0167 
0168         return $result1 && $result2;
0169     }
0170 
0171     /**
0172      * Clean some cache records
0173      *
0174      * Available modes are :
0175      * 'all' (default)  => remove all cache entries ($tags is not used)
0176      * 'old'            => unsupported
0177      * 'matchingTag'    => unsupported
0178      * 'notMatchingTag' => unsupported
0179      * 'matchingAnyTag' => unsupported
0180      *
0181      * @param  string $mode clean mode
0182      * @param  array  $tags array of tags
0183      * @throws Zend_Cache_Exception
0184      * @return boolean true if no problem
0185      */
0186     public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
0187     {
0188         switch ($mode) {
0189             case Zend_Cache::CLEANING_MODE_ALL:
0190                 $this->_clear();
0191                 return true;
0192                 break;
0193             case Zend_Cache::CLEANING_MODE_OLD:
0194                 $this->_log("Zend_Cache_Backend_ZendServer::clean() : CLEANING_MODE_OLD is unsupported by the Zend Server backends.");
0195                 break;
0196             case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
0197             case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
0198             case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
0199                 $this->_clear();
0200                 $this->_log('Zend_Cache_Backend_ZendServer::clean() : tags are unsupported by the Zend Server backends.');
0201                 break;
0202             default:
0203                 Zend_Cache::throwException('Invalid mode for clean() method');
0204                 break;
0205         }
0206     }
0207 }