WincacheEngine.php 5.1 KB

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