/libraries/src/Cache/Storage/WincacheStorage.php

https://github.com/joomla/joomla-cms · PHP · 208 lines · 89 code · 21 blank · 98 comment · 10 complexity · e1521a2b63b05447815ee0b966cb0e3b MD5 · raw file

  1. <?php
  2. /**
  3. * Joomla! Content Management System
  4. *
  5. * @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. namespace Joomla\CMS\Cache\Storage;
  9. \defined('JPATH_PLATFORM') or die;
  10. use Joomla\CMS\Cache\CacheStorage;
  11. /**
  12. * WinCache cache storage handler
  13. *
  14. * @link https://www.php.net/manual/en/book.wincache.php
  15. * @since 1.7.0
  16. * @deprecated 5.0 WinCache is abandoned and not supported from PHP 8 onwards
  17. */
  18. class WincacheStorage extends CacheStorage
  19. {
  20. /**
  21. * Check if the cache contains data stored by ID and group
  22. *
  23. * @param string $id The cache data ID
  24. * @param string $group The cache data group
  25. *
  26. * @return boolean
  27. *
  28. * @since 3.7.0
  29. * @deprecated 5.0
  30. */
  31. public function contains($id, $group)
  32. {
  33. return wincache_ucache_exists($this->_getCacheId($id, $group));
  34. }
  35. /**
  36. * Get cached data by ID and group
  37. *
  38. * @param string $id The cache data ID
  39. * @param string $group The cache data group
  40. * @param boolean $checkTime True to verify cache time expiration threshold
  41. *
  42. * @return mixed Boolean false on failure or a cached data object
  43. *
  44. * @since 1.7.0
  45. * @deprecated 5.0
  46. */
  47. public function get($id, $group, $checkTime = true)
  48. {
  49. return wincache_ucache_get($this->_getCacheId($id, $group));
  50. }
  51. /**
  52. * Get all cached data
  53. *
  54. * @return mixed Boolean false on failure or a cached data object
  55. *
  56. * @since 1.7.0
  57. * @deprecated 5.0
  58. */
  59. public function getAll()
  60. {
  61. $allinfo = wincache_ucache_info();
  62. $keys = $allinfo['ucache_entries'];
  63. $secret = $this->_hash;
  64. $data = array();
  65. foreach ($keys as $key)
  66. {
  67. $name = $key['key_name'];
  68. $namearr = explode('-', $name);
  69. if ($namearr !== false && $namearr[0] == $secret && $namearr[1] === 'cache')
  70. {
  71. $group = $namearr[2];
  72. if (!isset($data[$group]))
  73. {
  74. $item = new CacheStorageHelper($group);
  75. }
  76. else
  77. {
  78. $item = $data[$group];
  79. }
  80. if (isset($key['value_size']))
  81. {
  82. $item->updateSize($key['value_size']);
  83. }
  84. else
  85. {
  86. // Dummy, WINCACHE version is too low.
  87. $item->updateSize(1);
  88. }
  89. $data[$group] = $item;
  90. }
  91. }
  92. return $data;
  93. }
  94. /**
  95. * Store the data to cache by ID and group
  96. *
  97. * @param string $id The cache data ID
  98. * @param string $group The cache data group
  99. * @param string $data The data to store in cache
  100. *
  101. * @return boolean
  102. *
  103. * @since 1.7.0
  104. * @deprecated 5.0
  105. */
  106. public function store($id, $group, $data)
  107. {
  108. return wincache_ucache_set($this->_getCacheId($id, $group), $data, $this->_lifetime);
  109. }
  110. /**
  111. * Remove a cached data entry by ID and group
  112. *
  113. * @param string $id The cache data ID
  114. * @param string $group The cache data group
  115. *
  116. * @return boolean
  117. *
  118. * @since 1.7.0
  119. * @deprecated 5.0
  120. */
  121. public function remove($id, $group)
  122. {
  123. return wincache_ucache_delete($this->_getCacheId($id, $group));
  124. }
  125. /**
  126. * Clean cache for a group given a mode.
  127. *
  128. * group mode : cleans all cache in the group
  129. * notgroup mode : cleans all cache not in the group
  130. *
  131. * @param string $group The cache data group
  132. * @param string $mode The mode for cleaning cache [group|notgroup]
  133. *
  134. * @return boolean
  135. *
  136. * @since 1.7.0
  137. * @deprecated 5.0
  138. */
  139. public function clean($group, $mode = null)
  140. {
  141. $allinfo = wincache_ucache_info();
  142. $keys = $allinfo['ucache_entries'];
  143. $secret = $this->_hash;
  144. foreach ($keys as $key)
  145. {
  146. if (strpos($key['key_name'], $secret . '-cache-' . $group . '-') === 0 xor $mode !== 'group')
  147. {
  148. wincache_ucache_delete($key['key_name']);
  149. }
  150. }
  151. return true;
  152. }
  153. /**
  154. * Garbage collect expired cache data
  155. *
  156. * @return boolean
  157. *
  158. * @since 1.7.0
  159. * @deprecated 5.0
  160. */
  161. public function gc()
  162. {
  163. $allinfo = wincache_ucache_info();
  164. $keys = $allinfo['ucache_entries'];
  165. $secret = $this->_hash;
  166. foreach ($keys as $key)
  167. {
  168. if (strpos($key['key_name'], $secret . '-cache-'))
  169. {
  170. wincache_ucache_get($key['key_name']);
  171. }
  172. }
  173. return true;
  174. }
  175. /**
  176. * Test to see if the storage handler is available.
  177. *
  178. * @return boolean
  179. *
  180. * @since 3.0.0
  181. * @deprecated 5.0
  182. */
  183. public static function isSupported()
  184. {
  185. return \extension_loaded('wincache') && \function_exists('wincache_ucache_get') && !strcmp(ini_get('wincache.ucenabled'), '1');
  186. }
  187. }