PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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