1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<?php
namespace Drupal\Core\Cache;
use Drupal\Component\Assertion\Inspector;
/**
* Helper methods for cache.
*
* @ingroup cache
*/
class Cache {
/**
* Indicates that the item should never be removed unless explicitly deleted.
*/
const PERMANENT = CacheBackendInterface::CACHE_PERMANENT;
/**
* Merges lists of cache contexts and removes duplicates.
*
* @param list<string> ...$cache_contexts
* Cache contexts to merge.
*
* @return list<string>
* The merged list of cache contexts.
*/
public static function mergeContexts(array ...$cache_contexts) {
$cache_contexts = array_values(array_unique(array_merge(...$cache_contexts)));
assert(\Drupal::service('cache_contexts_manager')->assertValidTokens($cache_contexts), sprintf('Failed to assert that "%s" are valid cache contexts.', implode(', ', $cache_contexts)));
return $cache_contexts;
}
/**
* Merges lists of cache tags and removes duplicates.
*
* The cache tags list is returned in a format that is valid for
* \Drupal\Core\Cache\CacheBackendInterface::set().
*
* When caching elements, it is necessary to collect all cache tags into a
* single list, from both the element itself and all child elements. This
* allows items to be invalidated based on all tags attached to the content
* they're constituted from.
*
* @param list<string> ...$cache_tags
* Cache tags to merge.
*
* @return list<string>
* The merged list of cache tags.
*/
public static function mergeTags(array ...$cache_tags) {
$cache_tags = array_values(array_unique(array_merge(...$cache_tags)));
assert(Inspector::assertAllStrings($cache_tags), 'Cache tags must be valid strings');
return $cache_tags;
}
/**
* Merges max-age values (expressed in seconds), finds the lowest max-age.
*
* Ensures infinite max-age (Cache::PERMANENT) is taken into account.
*
* @param int ...$max_ages
* Max age values to merge.
*
* @return int
* The minimum max-age value.
*/
public static function mergeMaxAges(...$max_ages) {
// Remove Cache::PERMANENT values to return the correct minimum value.
$max_ages = array_filter($max_ages, function ($max_age) {
return $max_age !== Cache::PERMANENT;
});
// If there are no max ages left return Cache::PERMANENT, otherwise return
// the minimum value.
return empty($max_ages) ? Cache::PERMANENT : min($max_ages);
}
/**
* Build a list of cache tags from a given prefix and an array of suffixes.
*
* Each suffix will be converted to a cache tag by appending it to the prefix,
* with a colon between them.
*
* @param string $prefix
* A prefix string.
* @param array $suffixes
* An array of suffixes. Will be cast to strings.
* @param string $glue
* A string to be used as glue for concatenation. Defaults to a colon.
*
* @return list<string>
* A list of cache tags.
*/
public static function buildTags($prefix, array $suffixes, $glue = ':') {
$tags = [];
foreach ($suffixes as $suffix) {
$tags[] = $prefix . $glue . $suffix;
}
return $tags;
}
/**
* Marks cache items from all bins with any of the specified tags as invalid.
*
* @param string[] $tags
* The list of tags to invalidate cache items for.
*/
public static function invalidateTags(array $tags) {
\Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
}
/**
* Gets all cache bin services.
*
* @return \Drupal\Core\Cache\CacheBackendInterface[]
* An array of cache backend objects keyed by cache bins.
*/
public static function getBins() {
$bins = [];
$container = \Drupal::getContainer();
foreach ($container->getParameter('cache_bins') as $service_id => $bin) {
$bins[$bin] = $container->get($service_id);
}
return $bins;
}
/**
* Gets all memory cache bin services.
*
* @return \Drupal\Core\Cache\CacheBackendInterface[]
* An array of cache backend objects keyed by memory cache bins.
*/
public static function getMemoryBins(): array {
$bins = [];
$container = \Drupal::getContainer();
foreach ($container->getParameter('memory_cache_bins') as $service_id => $bin) {
$bins[$bin] = $container->get($service_id);
}
return $bins;
}
}
|