vendor/pimcore/pimcore/lib/Cache.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore;
  15. use Pimcore\Cache\Core\CoreCacheHandler;
  16. use Pimcore\Event\CoreCacheEvents;
  17. use Symfony\Component\EventDispatcher\GenericEvent;
  18. /**
  19. * This acts as facade for the actual cache implementation and exists primarily for BC reasons.
  20. */
  21. class Cache
  22. {
  23. /**
  24. * @var CoreCacheHandler
  25. */
  26. protected static $handler;
  27. /**
  28. * Get the cache handler implementation
  29. *
  30. * @internal
  31. *
  32. * @return CoreCacheHandler
  33. */
  34. public static function getHandler()
  35. {
  36. if (null === static::$handler) {
  37. static::$handler = \Pimcore::getContainer()->get(CoreCacheHandler::class);
  38. }
  39. return static::$handler;
  40. }
  41. /**
  42. * Initialize the cache. This acts mainly as integration point with legacy caches.
  43. *
  44. * @internal
  45. */
  46. public static function init()
  47. {
  48. if (\Pimcore::hasKernel()) {
  49. \Pimcore::getContainer()
  50. ->get('event_dispatcher')
  51. ->dispatch(new GenericEvent(), CoreCacheEvents::INIT);
  52. if (isset($_REQUEST['pimcore_nocache']) && \Pimcore::inDebugMode()) {
  53. self::getHandler()->setPool(\Pimcore::getContainer()->get('pimcore.cache.adapter.null_tag_aware'));
  54. }
  55. }
  56. }
  57. /**
  58. * Returns the content of the requested cache entry
  59. *
  60. * @param string $key
  61. *
  62. * @return mixed
  63. */
  64. public static function load($key)
  65. {
  66. return static::getHandler()->load($key);
  67. }
  68. /**
  69. * Save an item to the cache (deferred to shutdown if force is false and forceImmediateWrite is not set)
  70. *
  71. * @param mixed $data
  72. * @param string $key
  73. * @param array $tags
  74. * @param int|\DateInterval|null $lifetime
  75. * @param int $priority
  76. * @param bool $force
  77. *
  78. * @return bool
  79. */
  80. public static function save($data, $key, $tags = [], $lifetime = null, $priority = 0, $force = false)
  81. {
  82. return static::getHandler()->save($key, $data, $tags, $lifetime, $priority, $force);
  83. }
  84. /**
  85. * Remove an item from the cache
  86. *
  87. * @param string $key
  88. *
  89. * @return bool
  90. */
  91. public static function remove($key)
  92. {
  93. return static::getHandler()->remove($key);
  94. }
  95. /**
  96. * Empty the cache
  97. *
  98. * @return bool
  99. */
  100. public static function clearAll()
  101. {
  102. return static::getHandler()->clearAll();
  103. }
  104. /**
  105. * Removes entries from the cache matching the given tag
  106. *
  107. * @param string $tag
  108. *
  109. * @return bool
  110. */
  111. public static function clearTag($tag)
  112. {
  113. return static::getHandler()->clearTag($tag);
  114. }
  115. /**
  116. * Removes entries from the cache matching the given tags
  117. *
  118. * @param array $tags
  119. *
  120. * @return bool
  121. */
  122. public static function clearTags(array $tags = []): bool
  123. {
  124. return static::getHandler()->clearTags($tags);
  125. }
  126. /**
  127. * Adds a tag to the shutdown queue
  128. *
  129. * @param string $tag
  130. */
  131. public static function addClearTagOnShutdown($tag)
  132. {
  133. static::getHandler()->addTagClearedOnShutdown($tag);
  134. }
  135. /**
  136. * Add tag to the list ignored on save. Items with this tag won't be saved to cache.
  137. *
  138. * @param string $tag
  139. */
  140. public static function addIgnoredTagOnSave($tag)
  141. {
  142. static::getHandler()->addTagIgnoredOnSave($tag);
  143. }
  144. /**
  145. * Remove tag from the list ignored on save
  146. *
  147. * @param string $tag
  148. */
  149. public static function removeIgnoredTagOnSave($tag)
  150. {
  151. static::getHandler()->removeTagIgnoredOnSave($tag);
  152. }
  153. /**
  154. * Add tag to the list ignored on clear. Tags in this list won't be cleared via clearTags()
  155. *
  156. * @param string $tag
  157. */
  158. public static function addIgnoredTagOnClear($tag)
  159. {
  160. static::getHandler()->addTagIgnoredOnClear($tag);
  161. }
  162. /**
  163. * Remove tag from the list ignored on clear
  164. *
  165. * @param string $tag
  166. */
  167. public static function removeIgnoredTagOnClear($tag)
  168. {
  169. static::getHandler()->removeTagIgnoredOnClear($tag);
  170. }
  171. /**
  172. * Write and clean up cache
  173. *
  174. * @internal
  175. *
  176. * @param bool $forceWrite
  177. */
  178. public static function shutdown($forceWrite = false)
  179. {
  180. static::getHandler()->shutdown($forceWrite);
  181. }
  182. /**
  183. * Disables the complete pimcore cache
  184. */
  185. public static function disable()
  186. {
  187. static::getHandler()->disable();
  188. }
  189. /**
  190. * Enables the pimcore cache
  191. */
  192. public static function enable()
  193. {
  194. static::getHandler()->enable();
  195. }
  196. /**
  197. * @return bool
  198. */
  199. public static function isEnabled()
  200. {
  201. return static::getHandler()->isEnabled();
  202. }
  203. /**
  204. * @param bool $forceImmediateWrite
  205. */
  206. public static function setForceImmediateWrite($forceImmediateWrite)
  207. {
  208. static::getHandler()->setForceImmediateWrite($forceImmediateWrite);
  209. }
  210. /**
  211. * @return bool
  212. */
  213. public static function getForceImmediateWrite()
  214. {
  215. return static::getHandler()->getForceImmediateWrite();
  216. }
  217. }