XcacheEngine.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. parent::init(array_merge(array(
  46. 'engine' => 'Xcache',
  47. 'prefix' => Inflector::slug(APP_DIR) . '_',
  48. 'PHP_AUTH_USER' => 'user',
  49. 'PHP_AUTH_PW' => 'password'
  50. ), $settings)
  51. );
  52. return function_exists('xcache_info');
  53. }
  54. /**
  55. * Write data for key into cache
  56. *
  57. * @param string $key Identifier for the data
  58. * @param mixed $value Data to be cached
  59. * @param integer $duration How long to cache the data, in seconds
  60. * @return boolean True if the data was successfully cached, false on failure
  61. */
  62. public function write($key, $value, $duration) {
  63. $expires = time() + $duration;
  64. xcache_set($key . '_expires', $expires, $duration);
  65. return xcache_set($key, $value, $duration);
  66. }
  67. /**
  68. * Read a key from the cache
  69. *
  70. * @param string $key Identifier for the data
  71. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  72. */
  73. public function read($key) {
  74. if (xcache_isset($key)) {
  75. $time = time();
  76. $cachetime = intval(xcache_get($key . '_expires'));
  77. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  78. return false;
  79. }
  80. return xcache_get($key);
  81. }
  82. return false;
  83. }
  84. /**
  85. * Increments the value of an integer cached key
  86. * If the cache key is not an integer it will be treated as 0
  87. *
  88. * @param string $key Identifier for the data
  89. * @param integer $offset How much to increment
  90. * @return New incremented value, false otherwise
  91. */
  92. public function increment($key, $offset = 1) {
  93. return xcache_inc($key, $offset);
  94. }
  95. /**
  96. * Decrements the value of an integer cached key.
  97. * If the cache key is not an integer it will be treated as 0
  98. *
  99. * @param string $key Identifier for the data
  100. * @param integer $offset How much to subtract
  101. * @return New decremented value, false otherwise
  102. */
  103. public function decrement($key, $offset = 1) {
  104. return xcache_dec($key, $offset);
  105. }
  106. /**
  107. * Delete a key from the cache
  108. *
  109. * @param string $key Identifier for the data
  110. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  111. */
  112. public function delete($key) {
  113. return xcache_unset($key);
  114. }
  115. /**
  116. * Delete all keys from the cache
  117. *
  118. * @param boolean $check
  119. * @return boolean True if the cache was successfully cleared, false otherwise
  120. */
  121. public function clear($check) {
  122. $this->_auth();
  123. $max = xcache_count(XC_TYPE_VAR);
  124. for ($i = 0; $i < $max; $i++) {
  125. xcache_clear_cache(XC_TYPE_VAR, $i);
  126. }
  127. $this->_auth(true);
  128. return true;
  129. }
  130. /**
  131. * Returns the `group value` for each of the configured groups
  132. * If the group initial value was not found, then it initializes
  133. * the group accordingly.
  134. *
  135. * @return array
  136. **/
  137. public function groups() {
  138. $result = array();
  139. foreach ($this->settings['groups'] as $group) {
  140. $value = xcache_get($this->settings['prefix'] . $group);
  141. if (!$value) {
  142. $value = 1;
  143. xcache_set($this->settings['prefix'] . $group, $value, 0);
  144. }
  145. $result[] = $group . $value;
  146. }
  147. return $result;
  148. }
  149. /**
  150. * Increments the group value to simulate deletion of all keys under a group
  151. * old values will remain in storage until they expire.
  152. *
  153. * @return boolean success
  154. **/
  155. public function clearGroup($group) {
  156. return (bool)xcache_inc($this->settings['prefix'] . $group, 1);
  157. }
  158. /**
  159. * Populates and reverses $_SERVER authentication values
  160. * Makes necessary changes (and reverting them back) in $_SERVER
  161. *
  162. * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
  163. * (see xcache.admin configuration settings)
  164. *
  165. * @param boolean $reverse Revert changes
  166. * @return void
  167. */
  168. protected function _auth($reverse = false) {
  169. static $backup = array();
  170. $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
  171. foreach ($keys as $key => $setting) {
  172. if ($reverse) {
  173. if (isset($backup[$key])) {
  174. $_SERVER[$key] = $backup[$key];
  175. unset($backup[$key]);
  176. } else {
  177. unset($_SERVER[$key]);
  178. }
  179. } else {
  180. $value = env($key);
  181. if (!empty($value)) {
  182. $backup[$key] = $value;
  183. }
  184. if (!empty($this->settings[$setting])) {
  185. $_SERVER[$key] = $this->settings[$setting];
  186. } elseif (!empty($this->settings[$key])) {
  187. $_SERVER[$key] = $this->settings[$key];
  188. } else {
  189. $_SERVER[$key] = $value;
  190. }
  191. }
  192. }
  193. }
  194. }