PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Cache/Engine/XcacheEngine.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 176 lines | 74 code | 10 blank | 92 comment | 13 complexity | e6ab122bef87cc94abb13333056dc8fc MD5 | raw file
  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. */
  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. * 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. * @return void
  139. */
  140. protected 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. }