XcacheEngine.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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-2011, 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-2011, 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. * @access public
  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) {
  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. /**
  56. * Write data for key into cache
  57. *
  58. * @param string $key Identifier for the data
  59. * @param mixed $value Data to be cached
  60. * @param integer $duration How long to cache the data, in seconds
  61. * @return boolean True if the data was successfully cached, false on failure
  62. */
  63. public function write($key, $value, $duration) {
  64. $expires = time() + $duration;
  65. xcache_set($key . '_expires', $expires, $duration);
  66. return xcache_set($key, $value, $duration);
  67. }
  68. /**
  69. * Read a key from the cache
  70. *
  71. * @param string $key Identifier for the data
  72. * @return mixed The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
  73. */
  74. public function read($key) {
  75. if (xcache_isset($key)) {
  76. $time = time();
  77. $cachetime = intval(xcache_get($key . '_expires'));
  78. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  79. return false;
  80. }
  81. return xcache_get($key);
  82. }
  83. return false;
  84. }
  85. /**
  86. * Increments the value of an integer cached key
  87. * If the cache key is not an integer it will be treated as 0
  88. *
  89. * @param string $key Identifier for the data
  90. * @param integer $offset How much to increment
  91. * @return New incremented value, false otherwise
  92. */
  93. public function increment($key, $offset = 1) {
  94. return xcache_inc($key, $offset);
  95. }
  96. /**
  97. * Decrements the value of an integer cached key.
  98. * If the cache key is not an integer it will be treated as 0
  99. *
  100. * @param string $key Identifier for the data
  101. * @param integer $offset How much to subtract
  102. * @return New decremented value, false otherwise
  103. */
  104. public function decrement($key, $offset = 1) {
  105. return xcache_dec($key, $offset);
  106. }
  107. /**
  108. * Delete a key from the cache
  109. *
  110. * @param string $key Identifier for the data
  111. * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  112. */
  113. public function delete($key) {
  114. return xcache_unset($key);
  115. }
  116. /**
  117. * Delete all keys from the cache
  118. *
  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. * Populates and reverses $_SERVER authentication values
  132. * Makes necessary changes (and reverting them back) in $_SERVER
  133. *
  134. * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
  135. * (see xcache.admin configuration settings)
  136. *
  137. * @param boolean $reverse Revert changes
  138. * @access private
  139. */
  140. function __auth($reverse = false) {
  141. static $backup = array();
  142. $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
  143. foreach ($keys as $key => $setting) {
  144. if ($reverse) {
  145. if (isset($backup[$key])) {
  146. $_SERVER[$key] = $backup[$key];
  147. unset($backup[$key]);
  148. } else {
  149. unset($_SERVER[$key]);
  150. }
  151. } else {
  152. $value = env($key);
  153. if (!empty($value)) {
  154. $backup[$key] = $value;
  155. }
  156. if (!empty($this->settings[$setting])) {
  157. $_SERVER[$key] = $this->settings[$setting];
  158. } else if (!empty($this->settings[$key])) {
  159. $_SERVER[$key] = $this->settings[$key];
  160. } else {
  161. $_SERVER[$key] = $value;
  162. }
  163. }
  164. }
  165. }
  166. }