XcacheEngine.php 5.6 KB

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