class Cache (View source)

Helper methods for cache.

Constants

PERMANENT

Indicates that the item should never be removed unless explicitly deleted.

Methods

static string[]
mergeContexts(array $a = [], array $b = [])

Merges arrays of cache contexts and removes duplicates.

static string[]
mergeTags(array $a = [], array $b = [])

Merges arrays of cache tags and removes duplicates.

static int
mergeMaxAges(int $a = Cache::PERMANENT, int $b = Cache::PERMANENT)

Merges max-age values (expressed in seconds), finds the lowest max-age.

static 
validateTags(array $tags) deprecated

Validates an array of cache tags.

static string[]
buildTags(string $prefix, array $suffixes, string $glue = ':')

Build an array of cache tags from a given prefix and an array of suffixes.

static 
invalidateTags(array $tags)

Marks cache items from all bins with any of the specified tags as invalid.

getBins()

Gets all cache bin services.

static string
keyFromQuery(SelectInterface $query)

Generates a hash from a query object, to be used as part of the cache key.

Details

static string[] mergeContexts(array $a = [], array $b = [])

Merges arrays of cache contexts and removes duplicates.

Parameters

array $a

Cache contexts array to merge.

array $b

Cache contexts array to merge.

Return Value

string[]

The merged array of cache contexts.

static string[] mergeTags(array $a = [], array $b = [])

Merges arrays of cache tags and removes duplicates.

The cache tags array 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 array, 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.

Parameters

array $a

Cache tags array to merge.

array $b

Cache tags array to merge.

Return Value

string[]

The merged array of cache tags.

static int mergeMaxAges(int $a = Cache::PERMANENT, int $b = Cache::PERMANENT)

Merges max-age values (expressed in seconds), finds the lowest max-age.

Ensures infinite max-age (Cache::PERMANENT) is taken into account.

Parameters

int $a

Max age value to merge.

int $b

Max age value to merge.

Return Value

int

The minimum max-age value.

static validateTags(array $tags) deprecated

deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use assert(\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)) instead.

Validates an array of cache tags.

Can be called before using cache tags in operations, to ensure validity.

Parameters

array $tags

An array of cache tags.

Exceptions

LogicException

static string[] buildTags(string $prefix, array $suffixes, string $glue = ':')

Build an array 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.

Parameters

string $prefix

A prefix string.

array $suffixes

An array of suffixes. Will be cast to strings.

string $glue

A string to be used as glue for concatenation. Defaults to a colon.

Return Value

string[]

An array of cache tags.

static invalidateTags(array $tags)

Marks cache items from all bins with any of the specified tags as invalid.

Parameters

array $tags

The list of tags to invalidate cache items for.

static CacheBackendInterface[] getBins()

Gets all cache bin services.

Return Value

CacheBackendInterface[]

An array of cache backend objects keyed by cache bins.

static string keyFromQuery(SelectInterface $query)

Generates a hash from a query object, to be used as part of the cache key.

This smart caching strategy saves Drupal from querying and rendering to HTML when the underlying query is unchanged.

Expensive queries should use the query builder to create the query and then call this function. Executing the query and formatting results should happen in a #pre_render callback.

Parameters

SelectInterface $query

A select query object.

Return Value

string

A hash of the query arguments.