Cache
class Cache (View source)
Helper methods for cache.
Constants
| PERMANENT |
Indicates that the item should never be removed unless explicitly deleted. |
Methods
Merges arrays of cache contexts and removes duplicates.
Merges arrays of cache tags and removes duplicates.
Merges max-age values (expressed in seconds), finds the lowest max-age.
Build an array of cache tags from a given prefix and an array of suffixes.
Marks cache items from all bins with any of the specified tags as invalid.
Gets all cache bin services.
Generates a hash from a query object, to be used as part of the cache key.
Details
static string[]
mergeContexts(array ...$cache_contexts)
Merges arrays of cache contexts and removes duplicates.
static string[]
mergeTags(array ...$cache_tags)
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.
static int
mergeMaxAges(...$max_ages)
Merges max-age values (expressed in seconds), finds the lowest max-age.
Ensures infinite max-age (Cache::PERMANENT) is taken into account.
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.
static
invalidateTags(array $tags)
Marks cache items from all bins with any of the specified tags as invalid.
static CacheBackendInterface[]
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.
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.