PageRenderTime 47ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Cache/Backend/Xcache.php

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