XcacheEngine.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * @since 1.2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Cache\Engine;
  18. use Cake\Cache\CacheEngine;
  19. /**
  20. * Xcache storage engine for cache
  21. *
  22. * @link http://trac.lighttpd.net/xcache/ Xcache
  23. */
  24. class XcacheEngine extends CacheEngine {
  25. /**
  26. * The default config used unless overriden by runtime configuration
  27. *
  28. * - `duration` Specify how long items in this cache configuration last.
  29. * - `groups` List of groups or 'tags' associated to every key stored in this config.
  30. * handy for deleting a complete group from cache.
  31. * - `prefix` Prefix appended to all entries. Good for when you need to share a keyspace
  32. * with either another cache config or another application.
  33. * - `probability` Probability of hitting a cache gc cleanup. Setting to 0 will disable
  34. * cache::gc from ever being called automatically.
  35. * - `PHP_AUTH_USER` xcache.admin.user
  36. * - `PHP_AUTH_PW` xcache.admin.password
  37. *
  38. * @var array
  39. */
  40. protected $_defaultConfig = [
  41. 'duration' => 3600,
  42. 'groups' => [],
  43. 'prefix' => null,
  44. 'probability' => 100,
  45. 'PHP_AUTH_USER' => 'user',
  46. 'PHP_AUTH_PW' => 'password'
  47. ];
  48. /**
  49. * Initialize the Cache Engine
  50. *
  51. * Called automatically by the cache frontend
  52. *
  53. * @param array $config array of setting for the engine
  54. * @return boolean True if the engine has been successfully initialized, false if not
  55. */
  56. public function init(array $config = []) {
  57. if (php_sapi_name() !== 'cli') {
  58. if (!isset($config['prefix'])) {
  59. $config['prefix'] = Inflector::slug(APP_DIR) . '_';
  60. }
  61. parent::init($config);
  62. return function_exists('xcache_info');
  63. }
  64. return false;
  65. }
  66. /**
  67. * Write data for key into cache
  68. *
  69. * @param string $key Identifier for the data
  70. * @param mixed $value Data to be cached
  71. * @return boolean True if the data was successfully cached, false on failure
  72. */
  73. public function write($key, $value) {
  74. $key = $this->_key($key);
  75. $duration = $this->_config['duration'];
  76. $expires = time() + $duration;
  77. xcache_set($key . '_expires', $expires, $duration);
  78. return xcache_set($key, $value, $duration);
  79. }
  80. /**
  81. * Read a key from the cache
  82. *
  83. * @param string $key Identifier for the data
  84. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  85. */
  86. public function read($key) {
  87. $key = $this->_key($key);
  88. if (xcache_isset($key)) {
  89. $time = time();
  90. $cachetime = intval(xcache_get($key . '_expires'));
  91. if ($cachetime < $time || ($time + $this->_config['duration']) < $cachetime) {
  92. return false;
  93. }
  94. return xcache_get($key);
  95. }
  96. return false;
  97. }
  98. /**
  99. * Increments 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 increment
  104. * @return bool|int New incremented value, false otherwise
  105. */
  106. public function increment($key, $offset = 1) {
  107. $key = $this->_key($key);
  108. return xcache_inc($key, $offset);
  109. }
  110. /**
  111. * Decrements the value of an integer cached key.
  112. * If the cache key is not an integer it will be treated as 0
  113. *
  114. * @param string $key Identifier for the data
  115. * @param integer $offset How much to subtract
  116. * @return bool|int New decremented value, false otherwise
  117. */
  118. public function decrement($key, $offset = 1) {
  119. $key = $this->_key($key);
  120. return xcache_dec($key, $offset);
  121. }
  122. /**
  123. * Delete a key from the cache
  124. *
  125. * @param string $key Identifier for the data
  126. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  127. */
  128. public function delete($key) {
  129. $key = $this->_key($key);
  130. return xcache_unset($key);
  131. }
  132. /**
  133. * Delete all keys from the cache
  134. *
  135. * @param boolean $check
  136. * @return boolean True if the cache was successfully cleared, false otherwise
  137. */
  138. public function clear($check) {
  139. $this->_auth();
  140. $max = xcache_count(XC_TYPE_VAR);
  141. for ($i = 0; $i < $max; $i++) {
  142. xcache_clear_cache(XC_TYPE_VAR, $i);
  143. }
  144. $this->_auth(true);
  145. return true;
  146. }
  147. /**
  148. * Returns the `group value` for each of the configured groups
  149. * If the group initial value was not found, then it initializes
  150. * the group accordingly.
  151. *
  152. * @return array
  153. */
  154. public function groups() {
  155. $result = [];
  156. foreach ($this->_config['groups'] as $group) {
  157. $value = xcache_get($this->_config['prefix'] . $group);
  158. if (!$value) {
  159. $value = 1;
  160. xcache_set($this->_config['prefix'] . $group, $value, 0);
  161. }
  162. $result[] = $group . $value;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Increments the group value to simulate deletion of all keys under a group
  168. * old values will remain in storage until they expire.
  169. *
  170. * @param string $group name of the group to be cleared
  171. * @return boolean success
  172. */
  173. public function clearGroup($group) {
  174. return (bool)xcache_inc($this->_config['prefix'] . $group, 1);
  175. }
  176. /**
  177. * Populates and reverses $_SERVER authentication values
  178. * Makes necessary changes (and reverting them back) in $_SERVER
  179. *
  180. * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
  181. * (see xcache.admin configuration config)
  182. *
  183. * @param boolean $reverse Revert changes
  184. * @return void
  185. */
  186. protected function _auth($reverse = false) {
  187. static $backup = [];
  188. $keys = ['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password'];
  189. foreach ($keys as $key => $value) {
  190. if ($reverse) {
  191. if (isset($backup[$key])) {
  192. $_SERVER[$key] = $backup[$key];
  193. unset($backup[$key]);
  194. } else {
  195. unset($_SERVER[$key]);
  196. }
  197. } else {
  198. $value = env($key);
  199. if (!empty($value)) {
  200. $backup[$key] = $value;
  201. }
  202. if (!empty($this->_config[$value])) {
  203. $_SERVER[$key] = $this->_config[$value];
  204. } elseif (!empty($this->_config[$key])) {
  205. $_SERVER[$key] = $this->_config[$key];
  206. } else {
  207. $_SERVER[$key] = $value;
  208. }
  209. }
  210. }
  211. }
  212. }