3 namespace Drupal\Core\Cache;
5 use Drupal\Component\Assertion\Inspector;
6 use Drupal\Core\Database\Query\SelectInterface;
9 * Helper methods for cache.
16 * Indicates that the item should never be removed unless explicitly deleted.
18 const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;
21 * Merges arrays of cache contexts and removes duplicates.
24 * Cache contexts array to merge.
26 * Cache contexts array to merge.
29 * The merged array of cache contexts.
31 public static function mergeContexts(array $a = [], array $b = []) {
32 $cache_contexts = array_unique(array_merge($a, $b));
33 assert(\Drupal::service('cache_contexts_manager')->assertValidTokens($cache_contexts));
34 sort($cache_contexts);
35 return $cache_contexts;
39 * Merges arrays of cache tags and removes duplicates.
41 * The cache tags array is returned in a format that is valid for
42 * \Drupal\Core\Cache\CacheBackendInterface::set().
44 * When caching elements, it is necessary to collect all cache tags into a
45 * single array, from both the element itself and all child elements. This
46 * allows items to be invalidated based on all tags attached to the content
47 * they're constituted from.
50 * Cache tags array to merge.
52 * Cache tags array to merge.
55 * The merged array of cache tags.
57 public static function mergeTags(array $a = [], array $b = []) {
58 assert(Inspector::assertAllStrings($a) && Inspector::assertAllStrings($b), 'Cache tags must be valid strings');
60 $cache_tags = array_unique(array_merge($a, $b));
66 * Merges max-age values (expressed in seconds), finds the lowest max-age.
68 * Ensures infinite max-age (Cache::PERMANENT) is taken into account.
71 * Max age value to merge.
73 * Max age value to merge.
76 * The minimum max-age value.
78 public static function mergeMaxAges($a = Cache::PERMANENT, $b = Cache::PERMANENT) {
79 // If one of the values is Cache::PERMANENT, return the other value.
80 if ($a === Cache::PERMANENT) {
83 if ($b === Cache::PERMANENT) {
87 // If none or the values are Cache::PERMANENT, return the minimum value.
92 * Validates an array of cache tags.
94 * Can be called before using cache tags in operations, to ensure validity.
96 * @param string[] $tags
97 * An array of cache tags.
100 * Use assert(Inspector::assertAllStrings($tags));
102 * @throws \LogicException
104 public static function validateTags(array $tags) {
108 foreach ($tags as $value) {
109 if (!is_string($value)) {
110 throw new \LogicException('Cache tags must be strings, ' . gettype($value) . ' given.');
116 * Build an array of cache tags from a given prefix and an array of suffixes.
118 * Each suffix will be converted to a cache tag by appending it to the prefix,
119 * with a colon between them.
121 * @param string $prefix
123 * @param array $suffixes
124 * An array of suffixes. Will be cast to strings.
125 * @param string $glue
126 * A string to be used as glue for concatenation. Defaults to a colon.
129 * An array of cache tags.
131 public static function buildTags($prefix, array $suffixes, $glue = ':') {
133 foreach ($suffixes as $suffix) {
134 $tags[] = $prefix . $glue . $suffix;
140 * Marks cache items from all bins with any of the specified tags as invalid.
142 * @param string[] $tags
143 * The list of tags to invalidate cache items for.
145 public static function invalidateTags(array $tags) {
146 \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
150 * Gets all cache bin services.
152 * @return \Drupal\Core\Cache\CacheBackendInterface[]
153 * An array of cache backend objects keyed by cache bins.
155 public static function getBins() {
157 $container = \Drupal::getContainer();
158 foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
159 $bins[$bin] = $container->get($service_id);
165 * Generates a hash from a query object, to be used as part of the cache key.
167 * This smart caching strategy saves Drupal from querying and rendering to
168 * HTML when the underlying query is unchanged.
170 * Expensive queries should use the query builder to create the query and then
171 * call this function. Executing the query and formatting results should
172 * happen in a #pre_render callback.
174 * @param \Drupal\Core\Database\Query\SelectInterface $query
175 * A select query object.
178 * A hash of the query arguments.
180 public static function keyFromQuery(SelectInterface $query) {
181 $query->preExecute();
182 $keys = [(string) $query, $query->getArguments()];
183 return hash('sha256', serialize($keys));