vendor/contao/core-bundle/src/Resources/contao/library/Contao/Cache.php line 13

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Contao.
  4. *
  5. * (c) Leo Feyer
  6. *
  7. * @license LGPL-3.0-or-later
  8. */
  9. namespace Contao;
  10. trigger_deprecation('contao/core-bundle', '4.13', 'Using the Cache library is deprecated and will no longer work in Contao 5.0. Use your own in-memory cache instead.');
  11. /**
  12. * A static class to store non-persistent data
  13. *
  14. * The class functions as a global cache container where you can store data
  15. * that is reused by the application. The cache content is not persisted, so
  16. * once the process is completed, the data is gone.
  17. *
  18. * Usage:
  19. *
  20. * public function getResult()
  21. * {
  22. * if (!Cache::has('result'))
  23. * {
  24. * Cache::set('result') = $this->complexMethod();
  25. * }
  26. * return Cache::get('result');
  27. * }
  28. *
  29. * @deprecated Deprecated since Contao 4.13, to be removed in Contao 5.0;
  30. * use your own in-memory cache instead
  31. */
  32. class Cache
  33. {
  34. /**
  35. * Object instance (Singleton)
  36. * @var Cache
  37. */
  38. protected static $objInstance;
  39. /**
  40. * The cache data
  41. * @var array
  42. */
  43. protected static $arrData = array();
  44. /**
  45. * Check whether a key is set
  46. *
  47. * @param string $strKey The cache key
  48. *
  49. * @return boolean True if the key is set
  50. */
  51. public static function has($strKey)
  52. {
  53. return isset(static::$arrData[$strKey]);
  54. }
  55. /**
  56. * Return a cache entry
  57. *
  58. * @param string $strKey The cache key
  59. *
  60. * @return mixed The cached data
  61. */
  62. public static function get($strKey)
  63. {
  64. return static::$arrData[$strKey];
  65. }
  66. /**
  67. * Add a cache entry
  68. *
  69. * @param string $strKey The cache key
  70. * @param mixed $varValue The data to be cached
  71. */
  72. public static function set($strKey, $varValue)
  73. {
  74. static::$arrData[$strKey] = $varValue;
  75. }
  76. /**
  77. * Remove a cache entry
  78. *
  79. * @param string $strKey The cache key
  80. */
  81. public static function remove($strKey)
  82. {
  83. unset(static::$arrData[$strKey]);
  84. }
  85. /**
  86. * Prevent direct instantiation (Singleton)
  87. *
  88. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  89. * The Cache class is now static.
  90. */
  91. protected function __construct()
  92. {
  93. }
  94. /**
  95. * Prevent cloning of the object (Singleton)
  96. *
  97. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  98. * The Cache class is now static.
  99. */
  100. final public function __clone()
  101. {
  102. }
  103. /**
  104. * Check whether a key is set
  105. *
  106. * @param string $strKey The cache key
  107. *
  108. * @return boolean True if the key is set
  109. *
  110. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  111. * Use Cache::has() instead.
  112. */
  113. public function __isset($strKey)
  114. {
  115. return static::has($strKey);
  116. }
  117. /**
  118. * Return a cache entry
  119. *
  120. * @param string $strKey The cache key
  121. *
  122. * @return mixed|null The cached data
  123. *
  124. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  125. * Use Cache::get() instead.
  126. */
  127. public function __get($strKey)
  128. {
  129. if (static::has($strKey))
  130. {
  131. return static::get($strKey);
  132. }
  133. return null;
  134. }
  135. /**
  136. * Add a cache entry
  137. *
  138. * @param string $strKey The cache key
  139. * @param mixed $varValue The data to be stored
  140. *
  141. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  142. * Use Cache::set() instead.
  143. */
  144. public function __set($strKey, $varValue)
  145. {
  146. static::set($strKey, $varValue);
  147. }
  148. /**
  149. * Remove a cache entry
  150. *
  151. * @param string $strKey The cache key
  152. *
  153. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  154. * Use Cache::remove() instead.
  155. */
  156. public function __unset($strKey)
  157. {
  158. static::remove($strKey);
  159. }
  160. /**
  161. * Instantiate the cache object (Factory)
  162. *
  163. * @return Cache The object instance
  164. *
  165. * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  166. * The Cache class is now static.
  167. */
  168. public static function getInstance()
  169. {
  170. trigger_deprecation('contao/core-bundle', '4.0', 'Using "Contao\Cache::getInstance()" has been deprecated and will no longer work in Contao 5.0. The "Contao\Cache" class is now static.');
  171. if (static::$objInstance === null)
  172. {
  173. static::$objInstance = new static();
  174. }
  175. return static::$objInstance;
  176. }
  177. }
  178. class_alias(Cache::class, 'Cache');