Base for a static organization website

WincacheEngine.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Wincache storage engine for cache.
  4. *
  5. * Supports wincache 1.1.0 and higher.
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Cache.Engine
  17. * @since CakePHP(tm) v 1.2.0.4933
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Wincache storage engine for cache
  22. *
  23. * @package Cake.Cache.Engine
  24. */
  25. class WincacheEngine extends CacheEngine {
  26. /**
  27. * Contains the compiled group names
  28. * (prefixed with the global configuration prefix)
  29. *
  30. * @var array
  31. */
  32. protected $_compiledGroupNames = array();
  33. /**
  34. * Initialize the Cache Engine
  35. *
  36. * Called automatically by the cache frontend
  37. * To reinitialize the settings call Cache::engine('EngineName', [optional] settings = array());
  38. *
  39. * @param array $settings array of setting for the engine
  40. * @return bool True if the engine has been successfully initialized, false if not
  41. * @see CacheEngine::__defaults
  42. */
  43. public function init($settings = array()) {
  44. if (!isset($settings['prefix'])) {
  45. $settings['prefix'] = Inflector::slug(APP_DIR) . '_';
  46. }
  47. $settings += array('engine' => 'Wincache');
  48. parent::init($settings);
  49. return function_exists('wincache_ucache_info');
  50. }
  51. /**
  52. * Write data for key into cache
  53. *
  54. * @param string $key Identifier for the data
  55. * @param mixed $value Data to be cached
  56. * @param int $duration How long to cache the data, in seconds
  57. * @return bool True if the data was successfully cached, false on failure
  58. */
  59. public function write($key, $value, $duration) {
  60. $expires = time() + $duration;
  61. $data = array(
  62. $key . '_expires' => $expires,
  63. $key => $value
  64. );
  65. $result = wincache_ucache_set($data, null, $duration);
  66. return empty($result);
  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
  73. * there was an error fetching it
  74. */
  75. public function read($key) {
  76. $time = time();
  77. $cachetime = (int)wincache_ucache_get($key . '_expires');
  78. if ($cachetime < $time || ($time + $this->settings['duration']) < $cachetime) {
  79. return false;
  80. }
  81. return wincache_ucache_get($key);
  82. }
  83. /**
  84. * Increments the value of an integer cached key
  85. *
  86. * @param string $key Identifier for the data
  87. * @param int $offset How much to increment
  88. * @return New incremented value, false otherwise
  89. */
  90. public function increment($key, $offset = 1) {
  91. return wincache_ucache_inc($key, $offset);
  92. }
  93. /**
  94. * Decrements the value of an integer cached key
  95. *
  96. * @param string $key Identifier for the data
  97. * @param int $offset How much to subtract
  98. * @return New decremented value, false otherwise
  99. */
  100. public function decrement($key, $offset = 1) {
  101. return wincache_ucache_dec($key, $offset);
  102. }
  103. /**
  104. * Delete a key from the cache
  105. *
  106. * @param string $key Identifier for the data
  107. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  108. */
  109. public function delete($key) {
  110. return wincache_ucache_delete($key);
  111. }
  112. /**
  113. * Delete all keys from the cache. This will clear every
  114. * item in the cache matching the cache config prefix.
  115. *
  116. * @param bool $check If true, nothing will be cleared, as entries will
  117. * naturally expire in wincache..
  118. * @return bool True Returns true.
  119. */
  120. public function clear($check) {
  121. if ($check) {
  122. return true;
  123. }
  124. $info = wincache_ucache_info();
  125. $cacheKeys = $info['ucache_entries'];
  126. unset($info);
  127. foreach ($cacheKeys as $key) {
  128. if (strpos($key['key_name'], $this->settings['prefix']) === 0) {
  129. wincache_ucache_delete($key['key_name']);
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * Returns the `group value` for each of the configured groups
  136. * If the group initial value was not found, then it initializes
  137. * the group accordingly.
  138. *
  139. * @return array
  140. */
  141. public function groups() {
  142. if (empty($this->_compiledGroupNames)) {
  143. foreach ($this->settings['groups'] as $group) {
  144. $this->_compiledGroupNames[] = $this->settings['prefix'] . $group;
  145. }
  146. }
  147. $groups = wincache_ucache_get($this->_compiledGroupNames);
  148. if (count($groups) !== count($this->settings['groups'])) {
  149. foreach ($this->_compiledGroupNames as $group) {
  150. if (!isset($groups[$group])) {
  151. wincache_ucache_set($group, 1);
  152. $groups[$group] = 1;
  153. }
  154. }
  155. ksort($groups);
  156. }
  157. $result = array();
  158. $groups = array_values($groups);
  159. foreach ($this->settings['groups'] as $i => $group) {
  160. $result[] = $group . $groups[$i];
  161. }
  162. return $result;
  163. }
  164. /**
  165. * Increments the group value to simulate deletion of all keys under a group
  166. * old values will remain in storage until they expire.
  167. *
  168. * @param string $group The group to clear.
  169. * @return bool success
  170. */
  171. public function clearGroup($group) {
  172. $success = null;
  173. wincache_ucache_inc($this->settings['prefix'] . $group, 1, $success);
  174. return $success;
  175. }
  176. }