WincacheEngine.php 5.2 KB

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