PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Cache/Backend/Xcache.php

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