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.
17 * @subpackage Zend_Cache_Backend
18 * @copyright Copyright (c) 2005-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
25 * @see Zend_Cache_Backend_Interface
27 require_once 'Zend/Cache/Backend/Interface.php';
30 * @see Zend_Cache_Backend
32 require_once 'Zend/Cache/Backend.php';
37 * @subpackage Zend_Cache_Backend
38 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_Cache_Backend_Xcache
extends Zend_Cache_Backend
implements Zend_Cache_Backend_Interface
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';
53 * =====> (string) user :
54 * xcache.admin.user (necessary for the clean() method)
56 * =====> (string) password :
57 * xcache.admin.pass (clear, not MD5) (necessary for the clean() method)
59 * @var array available options
61 protected $_options = array(
69 * @param array $options associative array of options
70 * @throws Zend_Cache_Exception
73 public function __construct(array $options = array())
75 if (!extension_loaded('xcache')) {
76 Zend_Cache
::throwException('The xcache extension must be loaded for using this backend !');
78 parent
::__construct($options);
82 * Test if a cache is available for the given id and (if yes) return it (false else)
84 * WARNING $doNotTestCacheValidity=true is unsupported by the Xcache backend
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)
90 public function load($id, $doNotTestCacheValidity = false)
92 if ($doNotTestCacheValidity) {
93 $this->_log("Zend_Cache_Backend_Xcache::load() : \$doNotTestCacheValidity=true is unsupported by the Xcache backend");
95 $tmp = xcache_get($id);
103 * Test if a cache is available or not (for the given id)
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
108 public function test($id)
110 if (xcache_isset($id)) {
111 $tmp = xcache_get($id);
112 if (is_array($tmp)) {
120 * Save some string datas into a cache record
122 * Note : $data is always "string" (serialization is done by the
123 * core not by the backend)
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
131 public function save($data, $id, $tags = array(), $specificLifetime = false)
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
);
142 * Remove a cache record
144 * @param string $id cache id
145 * @return boolean true if no problem
147 public function remove($id)
149 return xcache_unset($id);
153 * Clean some cache records
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
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
167 public function clean($mode = Zend_Cache
::CLEANING_MODE_ALL
, $tags = array())
170 case Zend_Cache
::CLEANING_MODE_ALL
:
171 // Necessary because xcache_clear_cache() need basic authentification
173 if (isset($_SERVER['PHP_AUTH_USER'])) {
174 $backup['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_USER'];
176 if (isset($_SERVER['PHP_AUTH_PW'])) {
177 $backup['PHP_AUTH_PW'] = $_SERVER['PHP_AUTH_PW'];
179 if ($this->_options
['user']) {
180 $_SERVER['PHP_AUTH_USER'] = $this->_options
['user'];
182 if ($this->_options
['password']) {
183 $_SERVER['PHP_AUTH_PW'] = $this->_options
['password'];
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'];
192 case Zend_Cache
::CLEANING_MODE_OLD
:
193 $this->_log("Zend_Cache_Backend_Xcache::clean() : CLEANING_MODE_OLD is unsupported by the Xcache backend");
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
);
201 Zend_Cache
::throwException('Invalid mode for clean() method');
207 * Return true if the automatic cleaning is available for the backend
211 public function isAutomaticCleaningAvailable()