class PlaceholderingRenderCache extends RenderCache (View source)

Adds automatic placeholdering to the RenderCache.

This automatic placeholdering is performed to ensure the containing elements and overarching response are as cacheable as possible. Elements whose subtree bubble either max-age=0 or high-cardinality cache contexts (such as 'user' and 'session') are considered poorly cacheable.

Properties

protected RequestStack $requestStack

The request stack.

from  RenderCache
protected CacheFactoryInterface $cacheFactory

The cache factory.

from  RenderCache
protected CacheContextsManager $cacheContextsManager

The cache contexts manager.

from  RenderCache
protected PlaceholderGeneratorInterface $placeholderGenerator

The placeholder generator.

protected array $placeholderResultsCache

Stores rendered results for automatically placeholdered elements.

Methods

__construct(RequestStack $request_stack, CacheFactoryInterface $cache_factory, CacheContextsManager $cache_contexts_manager, PlaceholderGeneratorInterface $placeholder_generator)

Constructs a new PlaceholderingRenderCache object.

array|false
get(array $elements)

Gets the cached, pre-rendered element of a renderable element from cache.

bool|null
set(array $elements, array $pre_bubbling_elements)

Caches the rendered output of a renderable array.

int
maxAgeToExpire(int $max_age)

Maps a #cache[max-age] value to an "expire" value for the Cache API.

string
createCacheID(array $elements)

Creates the cache ID for a renderable element.

array
getCacheableRenderArray(array $elements)

Gets a cacheable render array for a render array and its rendered output.

array
createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements)

Create a placeholder for a renderable array and remember in a static cache.

array|false
getFromPlaceholderResultsCache(array $elements)

Retrieves an auto-placeholdered renderable array from the static cache.

Details

__construct(RequestStack $request_stack, CacheFactoryInterface $cache_factory, CacheContextsManager $cache_contexts_manager, PlaceholderGeneratorInterface $placeholder_generator)

Constructs a new PlaceholderingRenderCache object.

Parameters

RequestStack $request_stack

The request stack.

CacheFactoryInterface $cache_factory

The cache factory.

CacheContextsManager $cache_contexts_manager

The cache contexts manager.

PlaceholderGeneratorInterface $placeholder_generator

The placeholder generator.

array|false get(array $elements)

Gets the cached, pre-rendered element of a renderable element from cache.

Parameters

array $elements

A renderable array.

Return Value

array|false

A renderable array, with the original element and all its children pre- rendered, or FALSE if no cached copy of the element is available.

bool|null set(array $elements, array $pre_bubbling_elements)

Caches the rendered output of a renderable array.

May be called by an implementation of \Drupal\Core\Render\RendererInterface while rendering, if the #cache property is set.

Parameters

array $elements

A renderable array.

array $pre_bubbling_elements

A renderable array corresponding to the state (in particular, the cacheability metadata) of $elements prior to the beginning of its rendering process, and therefore before any bubbling of child information has taken place. Only the #cache property is used by this function, so the caller may omit all other properties and children from this array.

Return Value

bool|null

Returns FALSE if no cache item could be created, NULL otherwise.

protected int maxAgeToExpire(int $max_age)

Maps a #cache[max-age] value to an "expire" value for the Cache API.

Parameters

int $max_age

A #cache[max-age] value.

Return Value

int

A corresponding "expire" value.

See also

CacheBackendInterface::set

protected string createCacheID(array $elements)

Creates the cache ID for a renderable element.

Creates the cache ID string based on #cache['keys'] + #cache['contexts'].

Parameters

array $elements

A renderable array.

Return Value

string

The cache ID string, or FALSE if the element may not be cached.

array getCacheableRenderArray(array $elements)

Gets a cacheable render array for a render array and its rendered output.

Given a render array and its rendered output (HTML string), return an array data structure that allows the render array and its associated metadata to be cached reliably (and is serialization-safe).

If Drupal needs additional rendering metadata to be cached at some point, consumers of this method will continue to work. Those who only cache certain parts of a render array will cease to work.

Parameters

array $elements

A render array, on which \Drupal\Core\Render\RendererInterface::render() has already been invoked.

Return Value

array

An array representing the cacheable data for this render array.

protected array createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements)

Create a placeholder for a renderable array and remember in a static cache.

Parameters

array $rendered_elements

A fully rendered renderable array.

array $pre_bubbling_elements

A renderable array corresponding to the state (in particular, the cacheability metadata) of $rendered_elements prior to the beginning of its rendering process, and therefore before any bubbling of child information has taken place. Only the #cache property is used by this function, so the caller may omit all other properties and children from this array.

Return Value

array

Renderable array with placeholder markup and the attached placeholder replacement metadata.

protected array|false getFromPlaceholderResultsCache(array $elements)

Retrieves an auto-placeholdered renderable array from the static cache.

Parameters

array $elements

A renderable array.

Return Value

array|false

A renderable array, with the original element and all its children pre- rendered, or FALSE if no cached copy of the element is available.