annotate core/lib/Drupal/Core/Render/PlaceholderingRenderCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Render;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheFactoryInterface;
Chris@0 6 use Drupal\Core\Cache\Context\CacheContextsManager;
Chris@0 7 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Adds automatic placeholdering to the RenderCache.
Chris@0 11 *
Chris@0 12 * This automatic placeholdering is performed to ensure the containing elements
Chris@0 13 * and overarching response are as cacheable as possible. Elements whose subtree
Chris@0 14 * bubble either max-age=0 or high-cardinality cache contexts (such as 'user'
Chris@0 15 * and 'session') are considered poorly cacheable.
Chris@0 16 *
Chris@0 17 * @see sites/default/default.services.yml
Chris@0 18 *
Chris@0 19 * Automatic placeholdering is performed only on elements whose subtree was
Chris@0 20 * generated using a #lazy_builder callback and whose bubbled cacheability meets
Chris@0 21 * the auto-placeholdering conditions as configured in the renderer.config
Chris@0 22 * container parameter.
Chris@0 23 *
Chris@0 24 * This RenderCache implementation automatically replaces an element with a
Chris@0 25 * placeholder:
Chris@0 26 * - on render cache hit, i.e. ::get()
Chris@0 27 * - on render cache miss, i.e. ::set() (in subsequent requests, it will be a
Chris@0 28 * cache hit)
Chris@0 29 *
Chris@0 30 * In either case, the render cache is guaranteed to contain the to-be-rendered
Chris@0 31 * placeholder, so replacing (rendering) the placeholder will be very fast.
Chris@0 32 *
Chris@0 33 * Finally, in case the render cache item disappears between the time it is
Chris@0 34 * decided to automatically placeholder the element and the time where the
Chris@0 35 * placeholder is replaced (rendered), that is guaranteed to not be problematic.
Chris@0 36 * Because this only automatically placeholders elements that have a
Chris@0 37 * #lazy_builder callback set, which means that in the worst case, it will need
Chris@0 38 * to be re-rendered.
Chris@0 39 */
Chris@0 40 class PlaceholderingRenderCache extends RenderCache {
Chris@0 41
Chris@0 42 /**
Chris@0 43 * The placeholder generator.
Chris@0 44 *
Chris@0 45 * @var \Drupal\Core\Render\PlaceholderGeneratorInterface
Chris@0 46 */
Chris@0 47 protected $placeholderGenerator;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Stores rendered results for automatically placeholdered elements.
Chris@0 51 *
Chris@0 52 * This allows us to avoid talking to the cache twice per auto-placeholdered
Chris@0 53 * element, or in case of an uncacheable element, to render it twice.
Chris@0 54 *
Chris@0 55 * Scenario A. The double cache read would happen because:
Chris@0 56 * 1. when rendering, cache read, but auto-placeholdered
Chris@0 57 * 2. when rendering placeholders, again cache read
Chris@0 58 *
Chris@0 59 * Scenario B. The cache write plus read would happen because:
Chris@0 60 * 1. when rendering, cache write, but auto-placeholdered
Chris@0 61 * 2. when rendering placeholders, cache read
Chris@0 62 *
Chris@0 63 * Scenario C. The double rendering for an uncacheable element would happen because:
Chris@0 64 * 1. when rendering, not cacheable, but auto-placeholdered
Chris@0 65 * 2. when rendering placeholders, rendered again
Chris@0 66 *
Chris@0 67 * In all three scenarios, this static cache avoids the second step, thus
Chris@0 68 * avoiding expensive work.
Chris@0 69 *
Chris@0 70 * @var array
Chris@0 71 */
Chris@0 72 protected $placeholderResultsCache = [];
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Constructs a new PlaceholderingRenderCache object.
Chris@0 76 *
Chris@0 77 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
Chris@0 78 * The request stack.
Chris@0 79 * @param \Drupal\Core\Cache\CacheFactoryInterface $cache_factory
Chris@0 80 * The cache factory.
Chris@0 81 * @param \Drupal\Core\Cache\Context\CacheContextsManager $cache_contexts_manager
Chris@0 82 * The cache contexts manager.
Chris@0 83 * @param \Drupal\Core\Render\PlaceholderGeneratorInterface $placeholder_generator
Chris@0 84 * The placeholder generator.
Chris@0 85 */
Chris@0 86 public function __construct(RequestStack $request_stack, CacheFactoryInterface $cache_factory, CacheContextsManager $cache_contexts_manager, PlaceholderGeneratorInterface $placeholder_generator) {
Chris@0 87 parent::__construct($request_stack, $cache_factory, $cache_contexts_manager);
Chris@0 88 $this->placeholderGenerator = $placeholder_generator;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function get(array $elements) {
Chris@0 95 // @todo remove this check when https://www.drupal.org/node/2367555 lands.
Chris@0 96 if (!$this->requestStack->getCurrentRequest()->isMethodCacheable()) {
Chris@0 97 return FALSE;
Chris@0 98 }
Chris@0 99
Chris@0 100 // When rendering placeholders, special case auto-placeholdered elements:
Chris@0 101 // avoid retrieving them from cache again, or rendering them again.
Chris@0 102 if (isset($elements['#create_placeholder']) && $elements['#create_placeholder'] === FALSE) {
Chris@0 103 $cached_placeholder_result = $this->getFromPlaceholderResultsCache($elements);
Chris@0 104 if ($cached_placeholder_result !== FALSE) {
Chris@0 105 return $cached_placeholder_result;
Chris@0 106 }
Chris@0 107 }
Chris@0 108
Chris@0 109 $cached_element = parent::get($elements);
Chris@0 110
Chris@0 111 if ($cached_element === FALSE) {
Chris@0 112 return FALSE;
Chris@0 113 }
Chris@0 114 else {
Chris@0 115 if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($cached_element)) {
Chris@0 116 return $this->createPlaceholderAndRemember($cached_element, $elements);
Chris@0 117 }
Chris@0 118
Chris@0 119 return $cached_element;
Chris@0 120 }
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 public function set(array &$elements, array $pre_bubbling_elements) {
Chris@0 127 $result = parent::set($elements, $pre_bubbling_elements);
Chris@0 128
Chris@0 129 // @todo remove this check when https://www.drupal.org/node/2367555 lands.
Chris@0 130 if (!$this->requestStack->getCurrentRequest()->isMethodCacheable()) {
Chris@0 131 return FALSE;
Chris@0 132 }
Chris@0 133
Chris@0 134 if ($this->placeholderGenerator->canCreatePlaceholder($pre_bubbling_elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) {
Chris@0 135 // Overwrite $elements with a placeholder. The Renderer (which called this
Chris@0 136 // method) will update the context with the bubbleable metadata of the
Chris@0 137 // overwritten $elements.
Chris@0 138 $elements = $this->createPlaceholderAndRemember($this->getCacheableRenderArray($elements), $pre_bubbling_elements);
Chris@0 139 }
Chris@0 140
Chris@0 141 return $result;
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Create a placeholder for a renderable array and remember in a static cache.
Chris@0 146 *
Chris@0 147 * @param array $rendered_elements
Chris@0 148 * A fully rendered renderable array.
Chris@0 149 * @param array $pre_bubbling_elements
Chris@0 150 * A renderable array corresponding to the state (in particular, the
Chris@0 151 * cacheability metadata) of $rendered_elements prior to the beginning of
Chris@0 152 * its rendering process, and therefore before any bubbling of child
Chris@0 153 * information has taken place. Only the #cache property is used by this
Chris@0 154 * function, so the caller may omit all other properties and children from
Chris@0 155 * this array.
Chris@0 156 *
Chris@0 157 * @return array
Chris@0 158 * Renderable array with placeholder markup and the attached placeholder
Chris@0 159 * replacement metadata.
Chris@0 160 */
Chris@0 161 protected function createPlaceholderAndRemember(array $rendered_elements, array $pre_bubbling_elements) {
Chris@0 162 $placeholder_element = $this->placeholderGenerator->createPlaceholder($pre_bubbling_elements);
Chris@0 163 // Remember the result for this placeholder to avoid double work.
Chris@0 164 $placeholder = (string) $placeholder_element['#markup'];
Chris@0 165 $this->placeholderResultsCache[$placeholder] = $rendered_elements;
Chris@0 166 return $placeholder_element;
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Retrieves an auto-placeholdered renderable array from the static cache.
Chris@0 171 *
Chris@0 172 * @param array $elements
Chris@0 173 * A renderable array.
Chris@0 174 *
Chris@0 175 * @return array|false
Chris@0 176 * A renderable array, with the original element and all its children pre-
Chris@0 177 * rendered, or FALSE if no cached copy of the element is available.
Chris@0 178 */
Chris@0 179 protected function getFromPlaceholderResultsCache(array $elements) {
Chris@0 180 $placeholder_element = $this->placeholderGenerator->createPlaceholder($elements);
Chris@0 181 $placeholder = (string) $placeholder_element['#markup'];
Chris@0 182 if (isset($this->placeholderResultsCache[$placeholder])) {
Chris@0 183 return $this->placeholderResultsCache[$placeholder];
Chris@0 184 }
Chris@0 185 return FALSE;
Chris@0 186 }
Chris@0 187
Chris@0 188 }