/framework/vendor/zend/Zend/Cache/Backend/Xcache.php
PHP | 216 lines | 94 code | 15 blank | 107 comment | 12 complexity | e96b78d17618f60b4529ae316fe5501a MD5 | raw file
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: Xcache.php 20096 2010-01-06 02:05:09Z bkarwin $ 21 */ 22 23 24/** 25 * @see Zend_Cache_Backend_Interface 26 */ 27require_once 'Zend/Cache/Backend/Interface.php'; 28 29/** 30 * @see Zend_Cache_Backend 31 */ 32require_once 'Zend/Cache/Backend.php'; 33 34 35/** 36 * @package Zend_Cache 37 * @subpackage Zend_Cache_Backend 38 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 39 * @license http://framework.zend.com/license/new-bsd New BSD License 40 */ 41class Zend_Cache_Backend_Xcache extends Zend_Cache_Backend implements Zend_Cache_Backend_Interface 42{ 43 44 /** 45 * Log message 46 */ 47 const TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::clean() : tags are unsupported by the Xcache backend'; 48 const TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND = 'Zend_Cache_Backend_Xcache::save() : tags are unsupported by the Xcache backend'; 49 50 /** 51 * Available options 52 * 53 * =====> (string) user : 54 * xcache.admin.user (necessary for the clean() method) 55 * 56 * =====> (string) password : 57 * xcache.admin.pass (clear, not MD5) (necessary for the clean() method) 58 * 59 * @var array available options 60 */ 61 protected $_options = array( 62 'user' => null, 63 'password' => null 64 ); 65 66 /** 67 * Constructor 68 * 69 * @param array $options associative array of options 70 * @throws Zend_Cache_Exception 71 * @return void 72 */ 73 public function __construct(array $options = array()) 74 { 75 if (!extension_loaded('xcache')) { 76 Zend_Cache::throwException('The xcache extension must be loaded for using this backend !'); 77 } 78 parent::__construct($options); 79 } 80 81 /** 82 * Test if a cache is available for the given id and (if yes) return it (false else) 83 * 84 * WARNING $doNotTestCacheValidity=true is unsupported by the Xcache backend 85 * 86 * @param string $id cache id 87 * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested 88 * @return string cached datas (or false) 89 */ 90 public function load($id, $doNotTestCacheValidity = false) 91 { 92 if ($doNotTestCacheValidity) { 93 $this->_log("Zend_Cache_Backend_Xcache::load() : \$doNotTestCacheValidity=true is unsupported by the Xcache backend"); 94 } 95 $tmp = xcache_get($id); 96 if (is_array($tmp)) { 97 return $tmp[0]; 98 } 99 return false; 100 } 101 102 /** 103 * Test if a cache is available or not (for the given id) 104 * 105 * @param string $id cache id 106 * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record 107 */ 108 public function test($id) 109 { 110 if (xcache_isset($id)) { 111 $tmp = xcache_get($id); 112 if (is_array($tmp)) { 113 return $tmp[1]; 114 } 115 } 116 return false; 117 } 118 119 /** 120 * Save some string datas into a cache record 121 * 122 * Note : $data is always "string" (serialization is done by the 123 * core not by the backend) 124 * 125 * @param string $data datas to cache 126 * @param string $id cache id 127 * @param array $tags array of strings, the cache record will be tagged by each string entry 128 * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime) 129 * @return boolean true if no problem 130 */ 131 public function save($data, $id, $tags = array(), $specificLifetime = false) 132 { 133 $lifetime = $this->getLifetime($specificLifetime); 134 $result = xcache_set($id, array($data, time()), $lifetime); 135 if (count($tags) > 0) { 136 $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_XCACHE_BACKEND); 137 } 138 return $result; 139 } 140 141 /** 142 * Remove a cache record 143 * 144 * @param string $id cache id 145 * @return boolean true if no problem 146 */ 147 public function remove($id) 148 { 149 return xcache_unset($id); 150 } 151 152 /** 153 * Clean some cache records 154 * 155 * Available modes are : 156 * 'all' (default) => remove all cache entries ($tags is not used) 157 * 'old' => unsupported 158 * 'matchingTag' => unsupported 159 * 'notMatchingTag' => unsupported 160 * 'matchingAnyTag' => unsupported 161 * 162 * @param string $mode clean mode 163 * @param array $tags array of tags 164 * @throws Zend_Cache_Exception 165 * @return boolean true if no problem 166 */ 167 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) 168 { 169 switch ($mode) { 170 case Zend_Cache::CLEANING_MODE_ALL: 171 // Necessary because xcache_clear_cache() need basic authentification 172 $backup = array(); 173 if (isset($_SERVER['PHP_AUTH_USER'])) { 174 $backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER']; 175 } 176 if (isset($_SERVER['PHP_AUTH_PW'])) { 177 $backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW']; 178 } 179 if ($this->_options['user']) { 180 $_SERVER['PHP_AUTH_USER'] = $this->_options['user']; 181 } 182 if ($this->_options['password']) { 183 $_SERVER['PHP_AUTH_PW'] = $this->_options['password']; 184 } 185 xcache_clear_cache(XC_TYPE_VAR, 0); 186 if (isset($backup['PHP_AUTH_USER'])) { 187 $_SERVER['PHP_AUTH_USER'] = $backup['PHP_AUTH_USER']; 188 $_SERVER['PHP_AUTH_PW'] = $backup['PHP_AUTH_PW']; 189 } 190 return true; 191 break; 192 case Zend_Cache::CLEANING_MODE_OLD: 193 $this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend"); 194 break; 195 case Zend_Cache::CLEANING_MODE_MATCHING_TAG: 196 case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG: 197 case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG: 198 $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_XCACHE_BACKEND); 199 break; 200 default: 201 Zend_Cache::throwException('Invalid mode for clean() method'); 202 break; 203 } 204 } 205 206 /** 207 * Return true if the automatic cleaning is available for the backend 208 * 209 * @return boolean 210 */ 211 public function isAutomaticCleaningAvailable() 212 { 213 return false; 214 } 215 216}