XcacheEngine.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.cache
  17. * @since CakePHP(tm) v 1.2.0.4947
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Xcache storage engine for cache
  22. *
  23. * @link http://trac.lighttpd.net/xcache/ Xcache
  24. * @package cake
  25. * @subpackage cake.cake.libs.cache
  26. */
  27. class XcacheEngine extends CacheEngine {
  28. /**
  29. * Settings
  30. *
  31. * - PHP_AUTH_USER = xcache.admin.user, default cake
  32. * - PHP_AUTH_PW = xcache.admin.password, default cake
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. public $settings = array();
  38. /**
  39. * Initialize the Cache Engine
  40. *
  41. * Called automatically by the cache frontend
  42. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  43. *
  44. * @param array $setting array of setting for the engine
  45. * @return boolean True if the engine has been successfully initialized, false if not
  46. */
  47. public function init($settings) {
  48. parent::init(array_merge(array(
  49. 'engine' => 'Xcache',
  50. 'prefix' => Inflector::slug(APP_DIR) . '_',
  51. 'PHP_AUTH_USER' => 'user',
  52. 'PHP_AUTH_PW' => 'password'
  53. ), $settings)
  54. );
  55. return function_exists('xcache_info');
  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 succesfully 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. * @param integer $duration How long to cache the data, in seconds
  94. * @return New incremented value, false otherwise
  95. */
  96. public function increment($key, $offset = 1) {
  97. return xcache_inc($key, $offset);
  98. }
  99. /**
  100. * Decrements the value of an integer cached key.
  101. * If the cache key is not an integer it will be treated as 0
  102. *
  103. * @param string $key Identifier for the data
  104. * @param integer $offset How much to substract
  105. * @param integer $duration How long to cache the data, in seconds
  106. * @return New decremented value, false otherwise
  107. */
  108. public function decrement($key, $offset = 1) {
  109. return xcache_dec($key, $offset);
  110. }
  111. /**
  112. * Delete a key from the cache
  113. *
  114. * @param string $key Identifier for the data
  115. * @return boolean True if the value was succesfully deleted, false if it didn't exist or couldn't be removed
  116. */
  117. public function delete($key) {
  118. return xcache_unset($key);
  119. }
  120. /**
  121. * Delete all keys from the cache
  122. *
  123. * @return boolean True if the cache was succesfully cleared, false otherwise
  124. */
  125. public function clear($check) {
  126. $this->__auth();
  127. $max = xcache_count(XC_TYPE_VAR);
  128. for ($i = 0; $i < $max; $i++) {
  129. xcache_clear_cache(XC_TYPE_VAR, $i);
  130. }
  131. $this->__auth(true);
  132. return true;
  133. }
  134. /**
  135. * Populates and reverses $_SERVER authentication values
  136. * Makes necessary changes (and reverting them back) in $_SERVER
  137. *
  138. * This has to be done because xcache_clear_cache() needs to pass Basic Http Auth
  139. * (see xcache.admin configuration settings)
  140. *
  141. * @param boolean Revert changes
  142. * @access private
  143. */
  144. function __auth($reverse = false) {
  145. static $backup = array();
  146. $keys = array('PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password');
  147. foreach ($keys as $key => $setting) {
  148. if ($reverse) {
  149. if (isset($backup[$key])) {
  150. $_SERVER[$key] = $backup[$key];
  151. unset($backup[$key]);
  152. } else {
  153. unset($_SERVER[$key]);
  154. }
  155. } else {
  156. $value = env($key);
  157. if (!empty($value)) {
  158. $backup[$key] = $value;
  159. }
  160. if (!empty($this->settings[$setting])) {
  161. $_SERVER[$key] = $this->settings[$setting];
  162. } else if (!empty($this->settings[$key])) {
  163. $_SERVER[$key] = $this->settings[$key];
  164. } else {
  165. $_SERVER[$key] = $value;
  166. }
  167. }
  168. }
  169. }
  170. }