CacheContextsManager
class CacheContextsManager (View source)
Converts cache context tokens into cache keys.
Uses cache context services (services tagged with 'cache.context', and whose service ID has the 'cache_context.' prefix) to dynamically generate cache keys based on the request context, thus allowing developers to express the state by which should varied (the current URL, language, and so on).
Note that this maps exactly to HTTP's Vary header semantics:
Properties
| protected ContainerInterface | $container | The service container. |
|
| protected string[] | $contexts | Available cache context IDs and corresponding labels. |
Methods
Constructs a CacheContextsManager object.
Provides an array of available cache contexts.
Provides an array of available cache context labels.
Converts cache context tokens to cache keys.
Optimizes cache context tokens (the minimal representative subset).
Retrieves a cache context service from the container.
Parses cache context tokens into context IDs and optional parameters.
Validates an array of cache context tokens.
Asserts the context tokens are valid.
Details
__construct(ContainerInterface $container, array $contexts)
Constructs a CacheContextsManager object.
string[]
getAll()
Provides an array of available cache contexts.
array
getLabels(bool $include_calculated_cache_contexts = FALSE)
Provides an array of available cache context labels.
To be used in cache configuration forms.
ContextCacheKeys
convertTokensToKeys(array $context_tokens)
Converts cache context tokens to cache keys.
A cache context token is either:
- a cache context ID (if the service ID is 'cache_context.foo', then 'foo' is a cache context ID); for example, 'foo'.
- a calculated cache context ID, followed by a colon, followed by the parameter for the calculated cache context; for example, 'bar:some_parameter'.
string[]
optimizeTokens(array $context_tokens)
Optimizes cache context tokens (the minimal representative subset).
A minimal representative subset means that any cache context token in the given set of cache context tokens that is a property of another cache context cache context token in the set, is removed.
Hence a minimal representative subset is the most compact representation possible of a set of cache context tokens, that still captures the entire universe of variations.
If a cache context is being optimized away, it is able to set cacheable metadata for itself which will be bubbled up.
For example, when caching per user ('user'), also caching per role ('user.roles') is meaningless because "per role" is implied by "per user".
In the following examples, remember that the period indicates hierarchy and the colon can be used to get a specific value of a calculated cache context:
- ['a', 'a.b'] -> ['a']
- ['a', 'a.b.c'] -> ['a']
- ['a.b', 'a.b.c'] -> ['a.b']
- ['a', 'a.b', 'a.b.c'] -> ['a']
- ['x', 'x:foo'] -> ['x']
- ['a', 'a.b.c:bar'] -> ['a']
protected CacheContextInterface
getService(string $context_id)
Retrieves a cache context service from the container.
static array
parseTokens(array $context_tokens)
Parses cache context tokens into context IDs and optional parameters.
validateTokens(array $context_tokens = [])
Validates an array of cache context tokens.
Can be called before using cache contexts in operations, to check validity.
bool
assertValidTokens(mixed $context_tokens)
Asserts the context tokens are valid.
Similar to ::validateTokens, this method returns boolean TRUE when the context tokens are valid, and FALSE when they are not instead of returning NULL when they are valid and throwing a \LogicException when they are not. This function should be used with the assert() statement.