53aa1810aefb2ce1dba67c8922993a9af107194d
[yaffs-website] / web / core / lib / Drupal / Core / Cache / Cache.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 use Drupal\Component\Assertion\Inspector;
6 use Drupal\Core\Database\Query\SelectInterface;
7
8 /**
9  * Helper methods for cache.
10  *
11  * @ingroup cache
12  */
13 class Cache {
14
15   /**
16    * Indicates that the item should never be removed unless explicitly deleted.
17    */
18   const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;
19
20   /**
21    * Merges arrays of cache contexts and removes duplicates.
22    *
23    * @param array $a
24    *   Cache contexts array to merge.
25    * @param array $b
26    *   Cache contexts array to merge.
27    *
28    * @return string[]
29    *   The merged array of cache contexts.
30    */
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;
36   }
37
38   /**
39    * Merges arrays of cache tags and removes duplicates.
40    *
41    * The cache tags array is returned in a format that is valid for
42    * \Drupal\Core\Cache\CacheBackendInterface::set().
43    *
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.
48    *
49    * @param array $a
50    *   Cache tags array to merge.
51    * @param array $b
52    *   Cache tags array to merge.
53    *
54    * @return string[]
55    *   The merged array of cache tags.
56    */
57   public static function mergeTags(array $a = [], array $b = []) {
58     assert(Inspector::assertAllStrings($a) && Inspector::assertAllStrings($b), 'Cache tags must be valid strings');
59
60     $cache_tags = array_unique(array_merge($a, $b));
61     sort($cache_tags);
62     return $cache_tags;
63   }
64
65   /**
66    * Merges max-age values (expressed in seconds), finds the lowest max-age.
67    *
68    * Ensures infinite max-age (Cache::PERMANENT) is taken into account.
69    *
70    * @param int $a
71    *   Max age value to merge.
72    * @param int $b
73    *   Max age value to merge.
74    *
75    * @return int
76    *   The minimum max-age value.
77    */
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) {
81       return $b;
82     }
83     if ($b === Cache::PERMANENT) {
84       return $a;
85     }
86
87     // If none or the values are Cache::PERMANENT, return the minimum value.
88     return min($a, $b);
89   }
90
91   /**
92    * Validates an array of cache tags.
93    *
94    * Can be called before using cache tags in operations, to ensure validity.
95    *
96    * @param string[] $tags
97    *   An array of cache tags.
98    *
99    * @deprecated
100    *   Use assert(Inspector::assertAllStrings($tags));
101    *
102    * @throws \LogicException
103    */
104   public static function validateTags(array $tags) {
105     if (empty($tags)) {
106       return;
107     }
108     foreach ($tags as $value) {
109       if (!is_string($value)) {
110         throw new \LogicException('Cache tags must be strings, ' . gettype($value) . ' given.');
111       }
112     }
113   }
114
115   /**
116    * Build an array of cache tags from a given prefix and an array of suffixes.
117    *
118    * Each suffix will be converted to a cache tag by appending it to the prefix,
119    * with a colon between them.
120    *
121    * @param string $prefix
122    *   A prefix string.
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.
127    *
128    * @return string[]
129    *   An array of cache tags.
130    */
131   public static function buildTags($prefix, array $suffixes, $glue = ':') {
132     $tags = [];
133     foreach ($suffixes as $suffix) {
134       $tags[] = $prefix . $glue . $suffix;
135     }
136     return $tags;
137   }
138
139   /**
140    * Marks cache items from all bins with any of the specified tags as invalid.
141    *
142    * @param string[] $tags
143    *   The list of tags to invalidate cache items for.
144    */
145   public static function invalidateTags(array $tags) {
146     \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
147   }
148
149   /**
150    * Gets all cache bin services.
151    *
152    * @return \Drupal\Core\Cache\CacheBackendInterface[]
153    *   An array of cache backend objects keyed by cache bins.
154    */
155   public static function getBins() {
156     $bins = [];
157     $container = \Drupal::getContainer();
158     foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
159       $bins[$bin] = $container->get($service_id);
160     }
161     return $bins;
162   }
163
164   /**
165    * Generates a hash from a query object, to be used as part of the cache key.
166    *
167    * This smart caching strategy saves Drupal from querying and rendering to
168    * HTML when the underlying query is unchanged.
169    *
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.
173    *
174    * @param \Drupal\Core\Database\Query\SelectInterface $query
175    *   A select query object.
176    *
177    * @return string
178    *   A hash of the query arguments.
179    */
180   public static function keyFromQuery(SelectInterface $query) {
181     $query->preExecute();
182     $keys = [(string) $query, $query->getArguments()];
183     return hash('sha256', serialize($keys));
184   }
185
186 }