Base for a static organization website

XcacheEngine.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Xcache storage engine for cache.
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Cache.Engine
  15. * @since CakePHP(tm) v 1.2.0.4947
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. /**
  19. * Xcache storage engine for cache
  20. *
  21. * @link http://trac.lighttpd.net/xcache/ Xcache
  22. * @package Cake.Cache.Engine
  23. */
  24. class XcacheEngine extends CacheEngine {
  25. /**
  26. * Settings
  27. *
  28. * - PHP_AUTH_USER = xcache.admin.user, default cake
  29. * - PHP_AUTH_PW = xcache.admin.password, default cake
  30. *
  31. * @var array
  32. */
  33. public $settings = array();
  34. /**
  35. * Initialize the Cache Engine
  36. *
  37. * Called automatically by the cache frontend
  38. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  39. *
  40. * @param array $settings array of setting for the engine
  41. * @return bool True if the engine has been successfully initialized, false if not
  42. */
  43. public function init($settings = array()) {
  44. if (PHP_SAPI !== 'cli') {
  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. return false;
  55. }
  56. /**
  57. * Write data for key into cache
  58. *
  59. * @param string $key Identifier for the data
  60. * @param mixed $value Data to be cached
  61. * @param int $duration How long to cache the data, in seconds
  62. * @return bool True if the data was successfully cached, false on failure
  63. */
  64. public function write($key, $value, $duration) {
  65. $expires = time() + $duration;
  66. xcache_set($key . '_expires', $expires, $duration);
  67. return xcache_set($key, $value, $duration);
  68. }
  69. /**
  70. * Read a key from the cache
  71. *
  72. * @param string $key Identifier for the data
  73. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  74. */
  75. public function read($key) {
  76. if (xcache_isset($key)) {
  77. $time = time();
  78. $cachetime = (int)xcache_get($key . '_expires');
  79. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  80. return false;
  81. }
  82. return xcache_get($key);
  83. }
  84. return false;
  85. }
  86. /**
  87. * Increments the value of an integer cached key
  88. * If the cache key is not an integer it will be treated as 0
  89. *
  90. * @param string $key Identifier for the data
  91. * @param int $offset How much to increment
  92. * @return New incremented value, false otherwise
  93. */
  94. public function increment($key, $offset = 1) {
  95. return xcache_inc($key, $offset);
  96. }
  97. /**
  98. * Decrements the value of an integer cached key.
  99. * If the cache key is not an integer it will be treated as 0
  100. *
  101. * @param string $key Identifier for the data
  102. * @param int $offset How much to subtract
  103. * @return New decremented value, false otherwise
  104. */
  105. public function decrement($key, $offset = 1) {
  106. return xcache_dec($key, $offset);
  107. }
  108. /**
  109. * Delete a key from the cache
  110. *
  111. * @param string $key Identifier for the data
  112. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  113. */
  114. public function delete($key) {
  115. return xcache_unset($key);
  116. }
  117. /**
  118. * Delete all keys from the cache
  119. *
  120. * @param bool $check If true no deletes will occur and instead CakePHP will rely
  121. * on key TTL values.
  122. * @return bool 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. * @param string $group The group to clear.
  157. * @return bool 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 bool $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. }