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

__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.

convertTokensToKeys(array $context_tokens)

Converts cache context tokens to cache keys.

string[]
optimizeTokens(array $context_tokens)

Optimizes cache context tokens (the minimal representative subset).

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.

bool
assertValidTokens(mixed $context_tokens)

Asserts the context tokens are valid

Details

__construct(ContainerInterface $container, array $contexts)

Constructs a CacheContextsManager object.

Parameters

ContainerInterface $container

The current service container.

array $contexts

An array of the available cache context IDs.

string[] getAll()

Provides an array of available cache contexts.

Return Value

string[]

An array of available cache context IDs.

array getLabels(bool $include_calculated_cache_contexts = FALSE)

Provides an array of available cache context labels.

To be used in cache configuration forms.

Parameters

bool $include_calculated_cache_contexts

Whether to also return calculated cache contexts. Default to FALSE.

Return Value

array

An array of available cache contexts and corresponding labels.

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'.

Parameters

array $context_tokens

An array of cache context tokens.

Return Value

ContextCacheKeys

The ContextCacheKeys object containing the converted cache keys and cacheability metadata.

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']

Parameters

array $context_tokens

A set of cache context tokens.

Return Value

string[]

A representative subset of the given set of cache context tokens..

protected CacheContextInterface getService(string $context_id)

Retrieves a cache context service from the container.

Parameters

string $context_id

The context ID, which together with the service ID prefix allows the corresponding cache context service to be retrieved.

Return Value

CacheContextInterface

The requested cache context service.

static array parseTokens(array $context_tokens)

Parses cache context tokens into context IDs and optional parameters.

Parameters

array $context_tokens

An array of cache context tokens.

Return Value

array

An array with the parsed results, with each result being an array containing:

  • The cache context ID.
  • The associated parameter (for a calculated cache context), or NULL if there is no parameter.

validateTokens(array $context_tokens = [])

Validates an array of cache context tokens.

Can be called before using cache contexts in operations, to check validity.

Parameters

array $context_tokens

An array of cache context tokens.

Exceptions

LogicException

See also

CacheContextsManager::parseTokens

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.

Parameters

mixed $context_tokens

Variable to be examined - should be array of context_tokens.

Return Value

bool

TRUE if context_tokens is an array of valid tokens.