3 namespace Drupal\Core\Cache;
5 use Drupal\Core\Database\Query\SelectInterface;
8 * Helper methods for cache.
15 * Indicates that the item should never be removed unless explicitly deleted.
17 const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;
20 * Merges arrays of cache contexts and removes duplicates.
23 * Cache contexts array to merge.
25 * Cache contexts array to merge.
28 * The merged array of cache contexts.
30 public static function mergeContexts(array $a = [], array $b = []) {
31 $cache_contexts = array_unique(array_merge($a, $b));
32 assert('\Drupal::service(\'cache_contexts_manager\')->assertValidTokens($cache_contexts)');
33 sort($cache_contexts);
34 return $cache_contexts;
38 * Merges arrays of cache tags and removes duplicates.
40 * The cache tags array is returned in a format that is valid for
41 * \Drupal\Core\Cache\CacheBackendInterface::set().
43 * When caching elements, it is necessary to collect all cache tags into a
44 * single array, from both the element itself and all child elements. This
45 * allows items to be invalidated based on all tags attached to the content
46 * they're constituted from.
49 * Cache tags array to merge.
51 * Cache tags array to merge.
54 * The merged array of cache tags.
56 public static function mergeTags(array $a = [], array $b = []) {
57 assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($a) && \Drupal\Component\Assertion\Inspector::assertAllStrings($b)', 'Cache tags must be valid strings');
59 $cache_tags = array_unique(array_merge($a, $b));
65 * Merges max-age values (expressed in seconds), finds the lowest max-age.
67 * Ensures infinite max-age (Cache::PERMANENT) is taken into account.
70 * Max age value to merge.
72 * Max age value to merge.
75 * The minimum max-age value.
77 public static function mergeMaxAges($a = Cache::PERMANENT, $b = Cache::PERMANENT) {
78 // If one of the values is Cache::PERMANENT, return the other value.
79 if ($a === Cache::PERMANENT) {
82 if ($b === Cache::PERMANENT) {
86 // If none or the values are Cache::PERMANENT, return the minimum value.
91 * Validates an array of cache tags.
93 * Can be called before using cache tags in operations, to ensure validity.
95 * @param string[] $tags
96 * An array of cache tags.
99 * Use assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)');
101 * @throws \LogicException
103 public static function validateTags(array $tags) {
107 foreach ($tags as $value) {
108 if (!is_string($value)) {
109 throw new \LogicException('Cache tags must be strings, ' . gettype($value) . ' given.');
115 * Build an array of cache tags from a given prefix and an array of suffixes.
117 * Each suffix will be converted to a cache tag by appending it to the prefix,
118 * with a colon between them.
120 * @param string $prefix
122 * @param array $suffixes
123 * An array of suffixes. Will be cast to strings.
124 * @param string $glue
125 * A string to be used as glue for concatenation. Defaults to a colon.
128 * An array of cache tags.
130 public static function buildTags($prefix, array $suffixes, $glue = ':') {
132 foreach ($suffixes as $suffix) {
133 $tags[] = $prefix . $glue . $suffix;
139 * Marks cache items from all bins with any of the specified tags as invalid.
141 * @param string[] $tags
142 * The list of tags to invalidate cache items for.
144 public static function invalidateTags(array $tags) {
145 \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
149 * Gets all cache bin services.
151 * @return \Drupal\Core\Cache\CacheBackendInterface[]
152 * An array of cache backend objects keyed by cache bins.
154 public static function getBins() {
156 $container = \Drupal::getContainer();
157 foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
158 $bins[$bin] = $container->get($service_id);
164 * Generates a hash from a query object, to be used as part of the cache key.
166 * This smart caching strategy saves Drupal from querying and rendering to
167 * HTML when the underlying query is unchanged.
169 * Expensive queries should use the query builder to create the query and then
170 * call this function. Executing the query and formatting results should
171 * happen in a #pre_render callback.
173 * @param \Drupal\Core\Database\Query\SelectInterface $query
174 * A select query object.
177 * A hash of the query arguments.
179 public static function keyFromQuery(SelectInterface $query) {
180 $query->preExecute();
181 $keys = [(string) $query, $query->getArguments()];
182 return hash('sha256', serialize($keys));