PageRenderTime 33ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Cache/Cache.php

http://github.com/drupal/drupal
PHP | 188 lines | 62 code | 15 blank | 111 comment | 5 complexity | 30ae2f9dba051e5bf33e96932cdac390 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. use Drupal\Component\Assertion\Inspector;
  4. use Drupal\Core\Database\Query\SelectInterface;
  5. /**
  6. * Helper methods for cache.
  7. *
  8. * @ingroup cache
  9. */
  10. class Cache {
  11. /**
  12. * Indicates that the item should never be removed unless explicitly deleted.
  13. */
  14. const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;
  15. /**
  16. * Merges arrays of cache contexts and removes duplicates.
  17. *
  18. * @param array $a
  19. * Cache contexts array to merge.
  20. * @param array $b
  21. * Cache contexts array to merge.
  22. *
  23. * @return string[]
  24. * The merged array of cache contexts.
  25. */
  26. public static function mergeContexts(array $a = [], array $b = []) {
  27. $cache_contexts = array_unique(array_merge($a, $b));
  28. assert(\Drupal::service('cache_contexts_manager')->assertValidTokens($cache_contexts));
  29. sort($cache_contexts);
  30. return $cache_contexts;
  31. }
  32. /**
  33. * Merges arrays of cache tags and removes duplicates.
  34. *
  35. * The cache tags array is returned in a format that is valid for
  36. * \Drupal\Core\Cache\CacheBackendInterface::set().
  37. *
  38. * When caching elements, it is necessary to collect all cache tags into a
  39. * single array, from both the element itself and all child elements. This
  40. * allows items to be invalidated based on all tags attached to the content
  41. * they're constituted from.
  42. *
  43. * @param array $a
  44. * Cache tags array to merge.
  45. * @param array $b
  46. * Cache tags array to merge.
  47. *
  48. * @return string[]
  49. * The merged array of cache tags.
  50. */
  51. public static function mergeTags(array $a = [], array $b = []) {
  52. assert(Inspector::assertAllStrings($a) && Inspector::assertAllStrings($b), 'Cache tags must be valid strings');
  53. $cache_tags = array_unique(array_merge($a, $b));
  54. sort($cache_tags);
  55. return $cache_tags;
  56. }
  57. /**
  58. * Merges max-age values (expressed in seconds), finds the lowest max-age.
  59. *
  60. * Ensures infinite max-age (Cache::PERMANENT) is taken into account.
  61. *
  62. * @param int $a
  63. * Max age value to merge.
  64. * @param int $b
  65. * Max age value to merge.
  66. *
  67. * @return int
  68. * The minimum max-age value.
  69. */
  70. public static function mergeMaxAges($a = Cache::PERMANENT, $b = Cache::PERMANENT) {
  71. // If one of the values is Cache::PERMANENT, return the other value.
  72. if ($a === Cache::PERMANENT) {
  73. return $b;
  74. }
  75. if ($b === Cache::PERMANENT) {
  76. return $a;
  77. }
  78. // If none or the values are Cache::PERMANENT, return the minimum value.
  79. return min($a, $b);
  80. }
  81. /**
  82. * Validates an array of cache tags.
  83. *
  84. * Can be called before using cache tags in operations, to ensure validity.
  85. *
  86. * @param string[] $tags
  87. * An array of cache tags.
  88. *
  89. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  90. * assert(\Drupal\Component\Assertion\Inspector::assertAllStrings($tags))
  91. * instead.
  92. *
  93. * @throws \LogicException
  94. */
  95. public static function validateTags(array $tags) {
  96. @trigger_error(__NAMESPACE__ . '\Cache::validateTags() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use assert(\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)) instead.', E_USER_DEPRECATED);
  97. if (empty($tags)) {
  98. return;
  99. }
  100. foreach ($tags as $value) {
  101. if (!is_string($value)) {
  102. throw new \LogicException('Cache tags must be strings, ' . gettype($value) . ' given.');
  103. }
  104. }
  105. }
  106. /**
  107. * Build an array of cache tags from a given prefix and an array of suffixes.
  108. *
  109. * Each suffix will be converted to a cache tag by appending it to the prefix,
  110. * with a colon between them.
  111. *
  112. * @param string $prefix
  113. * A prefix string.
  114. * @param array $suffixes
  115. * An array of suffixes. Will be cast to strings.
  116. * @param string $glue
  117. * A string to be used as glue for concatenation. Defaults to a colon.
  118. *
  119. * @return string[]
  120. * An array of cache tags.
  121. */
  122. public static function buildTags($prefix, array $suffixes, $glue = ':') {
  123. $tags = [];
  124. foreach ($suffixes as $suffix) {
  125. $tags[] = $prefix . $glue . $suffix;
  126. }
  127. return $tags;
  128. }
  129. /**
  130. * Marks cache items from all bins with any of the specified tags as invalid.
  131. *
  132. * @param string[] $tags
  133. * The list of tags to invalidate cache items for.
  134. */
  135. public static function invalidateTags(array $tags) {
  136. \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
  137. }
  138. /**
  139. * Gets all cache bin services.
  140. *
  141. * @return \Drupal\Core\Cache\CacheBackendInterface[]
  142. * An array of cache backend objects keyed by cache bins.
  143. */
  144. public static function getBins() {
  145. $bins = [];
  146. $container = \Drupal::getContainer();
  147. foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
  148. $bins[$bin] = $container->get($service_id);
  149. }
  150. return $bins;
  151. }
  152. /**
  153. * Generates a hash from a query object, to be used as part of the cache key.
  154. *
  155. * This smart caching strategy saves Drupal from querying and rendering to
  156. * HTML when the underlying query is unchanged.
  157. *
  158. * Expensive queries should use the query builder to create the query and then
  159. * call this function. Executing the query and formatting results should
  160. * happen in a #pre_render callback.
  161. *
  162. * @param \Drupal\Core\Database\Query\SelectInterface $query
  163. * A select query object.
  164. *
  165. * @return string
  166. * A hash of the query arguments.
  167. */
  168. public static function keyFromQuery(SelectInterface $query) {
  169. $query->preExecute();
  170. $keys = [(string) $query, $query->getArguments()];
  171. return hash('sha256', serialize($keys));
  172. }
  173. }