XcacheEngine.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Xcache storage engine for cache.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Cache.Engine
  17. * @since CakePHP(tm) v 1.2.0.4947
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Xcache storage engine for cache
  22. *
  23. * @link http://trac.lighttpd.net/xcache/ Xcache
  24. * @package Cake.Cache.Engine
  25. */
  26. class XcacheEngine extends CacheEngine {
  27. /**
  28. * Settings
  29. *
  30. * - PHP_AUTH_USER = xcache.admin.user, default cake
  31. * - PHP_AUTH_PW = xcache.admin.password, default cake
  32. *
  33. * @var array
  34. */
  35. public $settings = array();
  36. /**
  37. * Initialize the Cache Engine
  38. *
  39. * Called automatically by the cache frontend
  40. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  41. *
  42. * @param array $settings array of setting for the engine
  43. * @return boolean True if the engine has been successfully initialized, false if not
  44. */
  45. public function init($settings = array()) {
  46. if (php_sapi_name() !== 'cli') {
  47. parent::init(array_merge(array(
  48. 'engine' => 'Xcache',
  49. 'prefix' => Inflector::slug(APP_DIR) . '_',
  50. 'PHP_AUTH_USER' => 'user',
  51. 'PHP_AUTH_PW' => 'password'
  52. ), $settings)
  53. );
  54. return function_exists('xcache_info');
  55. }
  56. return false;
  57. }
  58. /**
  59. * Write data for key into cache
  60. *
  61. * @param string $key Identifier for the data
  62. * @param mixed $value Data to be cached
  63. * @param integer $duration How long to cache the data, in seconds
  64. * @return boolean True if the data was successfully cached, false on failure
  65. */
  66. public function write($key, $value, $duration) {
  67. $expires = time() + $duration;
  68. xcache_set($key . '_expires', $expires, $duration);
  69. return xcache_set($key, $value, $duration);
  70. }
  71. /**
  72. * Read a key from the cache
  73. *
  74. * @param string $key Identifier for the data
  75. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  76. */
  77. public function read($key) {
  78. if (xcache_isset($key)) {
  79. $time = time();
  80. $cachetime = intval(xcache_get($key . '_expires'));
  81. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  82. return false;
  83. }
  84. return xcache_get($key);
  85. }
  86. return false;
  87. }
  88. /**
  89. * Increments the value of an integer cached key
  90. * If the cache key is not an integer it will be treated as 0
  91. *
  92. * @param string $key Identifier for the data
  93. * @param integer $offset How much to increment
  94. * @return New incremented value, false otherwise
  95. */
  96. public function increment($key, $offset = 1) {
  97. return xcache_inc($key, $offset);
  98. }
  99. /**
  100. * Decrements the value of an integer cached key.
  101. * If the cache key is not an integer it will be treated as 0
  102. *
  103. * @param string $key Identifier for the data
  104. * @param integer $offset How much to subtract
  105. * @return New decremented value, false otherwise
  106. */
  107. public function decrement($key, $offset = 1) {
  108. return xcache_dec($key, $offset);
  109. }
  110. /**
  111. * Delete a key from the cache
  112. *
  113. * @param string $key Identifier for the data
  114. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  115. */
  116. public function delete($key) {
  117. return xcache_unset($key);
  118. }
  119. /**
  120. * Delete all keys from the cache
  121. *
  122. * @param boolean $check
  123. * @return boolean True if the cache was successfully cleared, false otherwise
  124. */
  125. public function clear($check) {
  126. $this->_auth();
  127. $max = xcache_count(XC_TYPE_VAR);
  128. for ($i = 0; $i < $max; $i++) {
  129. xcache_clear_cache(XC_TYPE_VAR, $i);
  130. }
  131. $this->_auth(true);
  132. return true;
  133. }
  134. /**
  135. * Returns the `group value` for each of the configured groups
  136. * If the group initial value was not found, then it initializes
  137. * the group accordingly.
  138. *
  139. * @return array
  140. */
  141. public function groups() {
  142. $result = array();
  143. foreach ($this->settings['groups'] as $group) {
  144. $value = xcache_get($this->settings['prefix'] . $group);
  145. if (!$value) {
  146. $value = 1;
  147. xcache_set($this->settings['prefix'] . $group, $value, 0);
  148. }
  149. $result[] = $group . $value;
  150. }
  151. return $result;
  152. }
  153. /**
  154. * Increments the group value to simulate deletion of all keys under a group
  155. * old values will remain in storage until they expire.
  156. *
  157. * @return boolean success
  158. */
  159. public function clearGroup($group) {
  160. return (bool)xcache_inc($this->settings['prefix'] . $group, 1);
  161. }
  162. /**
  163. * Populates and reverses $_SERVER authentication values
  164. * Makes necessary changes (and reverting them back) in $_SERVER
  165. *
  166. * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
  167. * (see xcache.admin configuration settings)
  168. *
  169. * @param boolean $reverse Revert changes
  170. * @return void
  171. */
  172. protected function _auth($reverse = false) {
  173. static $backup = array();
  174. $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
  175. foreach ($keys as $key => $setting) {
  176. if ($reverse) {
  177. if (isset($backup[$key])) {
  178. $_SERVER[$key] = $backup[$key];
  179. unset($backup[$key]);
  180. } else {
  181. unset($_SERVER[$key]);
  182. }
  183. } else {
  184. $value = env($key);
  185. if (!empty($value)) {
  186. $backup[$key] = $value;
  187. }
  188. if (!empty($this->settings[$setting])) {
  189. $_SERVER[$key] = $this->settings[$setting];
  190. } elseif (!empty($this->settings[$key])) {
  191. $_SERVER[$key] = $this->settings[$key];
  192. } else {
  193. $_SERVER[$key] = $value;
  194. }
  195. }
  196. }
  197. }
  198. }