comparison core/lib/Drupal/Core/Cache/Cache.php @ 0:4c8ae668cc8c

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