WincacheEngine.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://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. class WincacheEngine extends CacheEngine
  23. {
  24. /**
  25. * Contains the compiled group names
  26. * (prefixed with the global configuration prefix)
  27. *
  28. * @var array
  29. */
  30. protected $_compiledGroupNames = [];
  31. /**
  32. * Initialize the Cache Engine
  33. *
  34. * Called automatically by the cache frontend
  35. *
  36. * @param array $config array of setting for the engine
  37. * @return bool True if the engine has been successfully initialized, false if not
  38. */
  39. public function init(array $config = [])
  40. {
  41. if (!extension_loaded('wincache')) {
  42. return false;
  43. }
  44. parent::init($config);
  45. return true;
  46. }
  47. /**
  48. * Write data for key into cache
  49. *
  50. * @param string $key Identifier for the data
  51. * @param mixed $value Data to be cached
  52. * @return bool True if the data was successfully cached, false on failure
  53. */
  54. public function write($key, $value)
  55. {
  56. $key = $this->_key($key);
  57. $duration = $this->_config['duration'];
  58. return wincache_ucache_set($key, $value, $duration);
  59. }
  60. /**
  61. * Read a key from the cache
  62. *
  63. * @param string $key Identifier for the data
  64. * @return mixed The cached data, or false if the data doesn't exist,
  65. * has expired, or if there was an error fetching it
  66. */
  67. public function read($key)
  68. {
  69. $key = $this->_key($key);
  70. return wincache_ucache_get($key);
  71. }
  72. /**
  73. * Increments the value of an integer cached key
  74. *
  75. * @param string $key Identifier for the data
  76. * @param int $offset How much to increment
  77. * @return bool|int New incremented value, false otherwise
  78. */
  79. public function increment($key, $offset = 1)
  80. {
  81. $key = $this->_key($key);
  82. return wincache_ucache_inc($key, $offset);
  83. }
  84. /**
  85. * Decrements the value of an integer cached key
  86. *
  87. * @param string $key Identifier for the data
  88. * @param int $offset How much to subtract
  89. * @return bool|int New decremented value, false otherwise
  90. */
  91. public function decrement($key, $offset = 1)
  92. {
  93. $key = $this->_key($key);
  94. return wincache_ucache_dec($key, $offset);
  95. }
  96. /**
  97. * Delete a key from the cache
  98. *
  99. * @param string $key Identifier for the data
  100. * @return bool True if the value was successfully deleted, false if it didn't exist or couldn't be removed
  101. */
  102. public function delete($key)
  103. {
  104. $key = $this->_key($key);
  105. return wincache_ucache_delete($key);
  106. }
  107. /**
  108. * Delete all keys from the cache. This will clear every
  109. * item in the cache matching the cache config prefix.
  110. *
  111. * @param bool $check If true, nothing will be cleared, as entries will
  112. * naturally expire in wincache..
  113. * @return bool True Returns true.
  114. */
  115. public function clear($check)
  116. {
  117. if ($check) {
  118. return true;
  119. }
  120. $info = wincache_ucache_info();
  121. $cacheKeys = $info['ucache_entries'];
  122. unset($info);
  123. foreach ($cacheKeys as $key) {
  124. if (strpos($key['key_name'], $this->_config['prefix']) === 0) {
  125. wincache_ucache_delete($key['key_name']);
  126. }
  127. }
  128. return true;
  129. }
  130. /**
  131. * Returns the `group value` for each of the configured groups
  132. * If the group initial value was not found, then it initializes
  133. * the group accordingly.
  134. *
  135. * @return array
  136. */
  137. public function groups()
  138. {
  139. if (empty($this->_compiledGroupNames)) {
  140. foreach ($this->_config['groups'] as $group) {
  141. $this->_compiledGroupNames[] = $this->_config['prefix'] . $group;
  142. }
  143. }
  144. $groups = wincache_ucache_get($this->_compiledGroupNames);
  145. if (count($groups) !== count($this->_config['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 = [];
  155. $groups = array_values($groups);
  156. foreach ($this->_config['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. * @param string $group The group to clear.
  166. * @return bool success
  167. */
  168. public function clearGroup($group)
  169. {
  170. $success = false;
  171. wincache_ucache_inc($this->_config['prefix'] . $group, 1, $success);
  172. return $success;
  173. }
  174. }