WincacheEngine.php 5.1 KB

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