 core/core.services.yml                             |  2 +-
 core/includes/common.inc                           | 75 +-----------------
 .../Core/Render/MainContent/HtmlRenderer.php       | 10 +--
 core/lib/Drupal/Core/Render/Renderer.php           | 89 +++++++++++++++++++++-
 core/lib/Drupal/Core/Render/RendererInterface.php  | 19 +++++
 .../src/Plugin/views/cache/CachePluginBase.php     | 71 ++++++++---------
 core/modules/views/src/Plugin/views/cache/Time.php |  8 +-
 7 files changed, 153 insertions(+), 121 deletions(-)

diff --git a/core/core.services.yml b/core/core.services.yml
index 328acd8..b1e131a 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1106,4 +1106,4 @@ services:
       - { name: mime_type_guesser }
   renderer:
     class: Drupal\Core\Render\Renderer
-    arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info']
+    arguments: ['@controller_resolver', '@theme.manager', '@plugin.manager.element_info', '@request_stack']
diff --git a/core/includes/common.inc b/core/includes/common.inc
index d05ee0c..115c729 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -2409,79 +2409,6 @@ function show(&$element) {
 }
 
 /**
- * Gets the cached, prerendered element of a renderable element from the cache.
- *
- * @param array $elements
- *   A renderable array.
- *
- * @return array
- *   A renderable array, with the original element and all its children pre-
- *   rendered, or FALSE if no cached copy of the element is available.
- *
- * @see drupal_render()
- * @see drupal_render_cache_set()
- */
-function drupal_render_cache_get(array $elements) {
-  if (!\Drupal::request()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) {
-    return FALSE;
-  }
-  $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
-
-  if (!empty($cid) && $cache = \Drupal::cache($bin)->get($cid)) {
-    $cached_element = $cache->data;
-    // Return the cached element.
-    return $cached_element;
-  }
-  return FALSE;
-}
-
-/**
- * Caches the rendered output of a renderable element.
- *
- * This is called by drupal_render() if the #cache property is set on an
- * element.
- *
- * @param $markup
- *   The rendered output string of $elements.
- * @param array $elements
- *   A renderable array.
- *
- * @see drupal_render_cache_get()
- */
-function drupal_render_cache_set(&$markup, array $elements) {
-  // Create the cache ID for the element.
-  if (!\Drupal::request()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) {
-    return FALSE;
-  }
-
-  // Cache implementations are allowed to modify the markup, to support
-  // replacing markup with edge-side include commands. The supporting cache
-  // backend will store the markup in some other key (like
-  // $data['#real-value']) and return an include command instead. When the
-  // ESI command is executed by the content accelerator, the real value can
-  // be retrieved and used.
-  $data['#markup'] = $markup;
-
-  // Persist attached data associated with this element.
-  $data['#attached'] = $elements['#attached'];
-
-  // Persist #post_render_cache callbacks associated with this element.
-  if (isset($elements['#post_render_cache'])) {
-    $data['#post_render_cache'] = $elements['#post_render_cache'];
-  }
-
-  // Persist cache tags associated with this element. Also associate the
-  // "rendered" cache tag. This allows us to invalidate the entire render cache,
-  // regardless of the cache bin.
-  $data['#cache']['tags'] = $elements['#cache']['tags'];
-  $data['#cache']['tags'][] = 'rendered';
-
-  $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
-  $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : Cache::PERMANENT;
-  \Drupal::cache($bin)->set($cid, $data, $expire, $data['#cache']['tags']);
-}
-
-/**
  * Generates a render cache placeholder.
  *
  * This is used by drupal_pre_render_render_cache_placeholder() to generate
@@ -2501,7 +2428,7 @@ function drupal_render_cache_set(&$markup, array $elements) {
  *
  * @throws \Exception
  *
- * @see drupal_render_cache_get()
+ * @see \Drupal\Core\Render\Renderer::getFromCache()
  */
 function drupal_render_cache_generate_placeholder($callback, array &$context) {
   if (is_string($callback) && strpos($callback, '::') === FALSE) {
diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
index 323fd87..081902f 100644
--- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
@@ -185,16 +185,12 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
       // title. We set its $is_root_call parameter to FALSE, to ensure
       // #post_render_cache callbacks are not yet applied. This is essentially
       // "pre-rendering" the main content, the "full rendering" will happen in
-      // ::renderContentIntoResponse().
+      // ::renderResponse().
       // @todo Remove this once https://www.drupal.org/node/2359901 lands.
       if (!empty($main_content)) {
         $this->renderer->render($main_content, FALSE);
-        $main_content = [
-          '#markup' => $main_content['#markup'],
-          '#attached' => $main_content['#attached'],
-          '#cache' => ['tags' => $main_content['#cache']['tags']],
-          '#post_render_cache' => $main_content['#post_render_cache'],
-          '#title' => isset($main_content['#title']) ? $main_content['#title'] : NULL,
+        $main_content = $this->renderer->getCacheableRenderArray($main_content) + [
+          '#title' => isset($main_content['#title']) ? $main_content['#title'] : NULL
         ];
       }
 
diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php
index 2746041..eebd558 100644
--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -12,6 +12,7 @@
 use Drupal\Component\Utility\SafeMarkup;
 use Drupal\Core\Cache\Cache;
 use Drupal\Component\Utility\NestedArray;
+use Symfony\Component\HttpFoundation\RequestStack;
 
 /**
  * Turns a render array into a HTML string.
@@ -40,6 +41,13 @@ class Renderer implements RendererInterface {
   protected $elementInfo;
 
   /**
+   * The request stack.
+   *
+   * @var \Symfony\Component\HttpFoundation\RequestStack
+   */
+  protected $requestStack;
+
+  /**
    * The stack containing bubbleable rendering metadata.
    *
    * @var \SplStack|null
@@ -55,11 +63,14 @@ class Renderer implements RendererInterface {
    *   The theme manager.
    * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
    *   The element info.
+   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
+   *   The request stack.
    */
-  public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info) {
+  public function __construct(ControllerResolverInterface $controller_resolver, ThemeManagerInterface $theme, ElementInfoManagerInterface $element_info, RequestStack $request_stack) {
     $this->controllerResolver = $controller_resolver;
     $this->theme = $theme;
     $this->elementInfo = $element_info;
+    $this->requestStack = $request_stack;
   }
 
   /**
@@ -133,7 +144,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
     // Try to fetch the prerendered element from cache, run any
     // #post_render_cache callbacks and return the final markup.
     if (isset($elements['#cache'])) {
-      $cached_element = drupal_render_cache_get($elements);
+      $cached_element = $this->cacheGet($elements);
       if ($cached_element !== FALSE) {
         $elements = $cached_element;
         // Only when we're not in a root (non-recursive) drupal_render() call,
@@ -312,7 +323,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
 
     // Cache the processed element if #cache is set.
     if (isset($elements['#cache'])) {
-      drupal_render_cache_set($elements['#markup'], $elements);
+      $this->cacheSet($elements);
     }
 
     // Only when we're in a root (non-recursive) drupal_render() call,
@@ -437,4 +448,76 @@ protected function processPostRenderCache(array &$elements) {
     }
   }
 
+  /**
+   * Gets the cached, prerendered element of a renderable element from the cache.
+   *
+   * @param array $elements
+   *   A renderable array.
+   *
+   * @return array
+   *   A renderable array, with the original element and all its children pre-
+   *   rendered, or FALSE if no cached copy of the element is available.
+   *
+   * @see ::render()
+   * @see ::saveToCache()
+   */
+  protected function cacheGet(array $elements) {
+    if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) {
+      return FALSE;
+    }
+    $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
+
+    if (!empty($cid) && $cache = \Drupal::cache($bin)->get($cid)) {
+      $cached_element = $cache->data;
+      // Return the cached element.
+      return $cached_element;
+    }
+    return FALSE;
+  }
+
+  /**
+   * Caches the rendered output of a renderable element.
+   *
+   * This is called by ::render() if the #cache property is set on an element.
+   *
+   * @param array $elements
+   *   A renderable array.
+   *
+   * @return bool|null
+   *  Returns FALSE if no cache item could be created, NULL otherwise.
+   *
+   * @see ::getFromCache()
+   */
+  protected function cacheSet(array &$elements) {
+    // Create the cache ID for the element.
+    if (!$this->requestStack->getCurrentRequest()->isMethodSafe() || !$cid = drupal_render_cid_create($elements)) {
+      return FALSE;
+    }
+
+    $data = $this->getCacheableRenderArray($elements);
+
+    // Cache tags are cached, but we also want to assocaite the "rendered" cache
+    // tag. This allows us to invalidate the entire render cache, regardless of
+    // the cache bin.
+    $data['#cache']['tags'][] = 'rendered';
+
+    $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
+    $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : Cache::PERMANENT;
+    \Drupal::cache($bin)->set($cid, $data, $expire, $data['#cache']['tags']);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheableRenderArray(array $elements) {
+    return [
+      '#markup' => $elements['#markup'],
+      '#attached' => $elements['#attached'],
+      '#post_render_cache' => $elements['#post_render_cache'],
+      '#cache' => [
+        'tags' => $elements['#cache']['tags'],
+      ],
+    ];
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Render/RendererInterface.php b/core/lib/Drupal/Core/Render/RendererInterface.php
index 97f09b6..43f4f44 100644
--- a/core/lib/Drupal/Core/Render/RendererInterface.php
+++ b/core/lib/Drupal/Core/Render/RendererInterface.php
@@ -269,4 +269,23 @@ public function renderPlain(&$elements);
    */
   public function render(&$elements, $is_root_call = FALSE);
 
+  /**
+   * 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.
+   *
+   * @param array $elements
+   *   A renderable array, on which ::render() has already been invoked.
+   *
+   * @return array
+   *   An array representing the cacheable data for this render array.
+   */
+  public function getCacheableRenderArray(array $elements);
+
 }
diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
index 1369b8a..2767482 100644
--- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
+++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
@@ -7,10 +7,11 @@
 
 namespace Drupal\views\Plugin\views\cache;
 
-use Drupal\Component\Utility\NestedArray;
 use Drupal\Core\Cache\Cache;
+use Drupal\Core\Render\RendererInterface;
 use Drupal\views\Plugin\views\PluginBase;
 use Drupal\Core\Database\Query\Select;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * @defgroup views_cache_plugins Views cache plugins
@@ -73,6 +74,38 @@
   protected $outputKey;
 
   /**
+   * The renderer service.
+   *
+   * @var \Drupal\Core\Render\RendererInterface
+   */
+  protected $renderer;
+
+  /**
+   * Constructs a CachePluginBase object.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\Core\Render\RendererInterface $renderer
+   *   The renderer service.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $renderer) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+
+    $this->renderer = $renderer;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static($configuration, $plugin_id, $plugin_definition, $container->get('renderer'));
+  }
+
+  /**
    * Returns the outputKey property.
    *
    * @return string
@@ -143,8 +176,8 @@ public function cacheSet($type) {
         \Drupal::cache($this->resultsBin)->set($this->generateResultsKey(), $data, $this->cacheSetExpire($type), $this->getCacheTags());
         break;
       case 'output':
-        $this->storage['output'] = drupal_render($this->view->display_handler->output);
-        $this->gatherRenderMetadata($this->view->display_handler->output);
+        $this->renderer->render($this->view->display_handler->output);
+        $this->storage = $this->renderer->getCacheableRenderArray($this->view->display_handler->output);
         \Drupal::cache($this->outputBin)->set($this->generateOutputKey(), $this->storage, $this->cacheSetExpire($type), $this->getCacheTags());
         break;
     }
@@ -178,16 +211,7 @@ public function cacheGet($type) {
         if ($cache = \Drupal::cache($this->outputBin)->get($this->generateOutputKey())) {
           if (!$cutoff || $cache->created > $cutoff) {
             $this->storage = $cache->data;
-
-            $this->restoreRenderMetadata();
-            $this->view->display_handler->output = array(
-              '#attached' => &$this->view->element['#attached'],
-              '#cache' => [
-                'tags' => &$this->view->element['#cache']['tags'],
-              ],
-              '#post_render_cache' => &$this->view->element['#post_render_cache'],
-              '#markup' => $cache->data['output'],
-            );
+            $this->view->display_handler->output = $this->storage;
 
             return TRUE;
           }
@@ -235,27 +259,6 @@ public function postRender(&$output) { }
   public function cacheStart() { }
 
   /**
-   * Gather bubbleable render metadata from the render array.
-   *
-   * @param array $render_array
-   *   The view render array to collect data from.
-   */
-  protected function gatherRenderMetadata(array $render_array = []) {
-    $this->storage['attachments'] = $render_array['#attached'];
-    $this->storage['postRenderCache'] = $render_array['#post_render_cache'];
-    $this->storage['cacheTags'] = $render_array['#cache']['tags'];
-  }
-
-  /**
-   * Restore bubbleable render metadata.
-   */
-  public function restoreRenderMetadata() {
-    $this->view->element['#attached'] = drupal_merge_attached($this->view->element['#attached'], $this->storage['attachments']);
-    $this->view->element['#cache']['tags'] = Cache::mergeTags(isset($this->view->element['#cache']['tags']) ? $this->view->element['#cache']['tags'] : [], $this->storage['cacheTags']);
-    $this->view->element['#post_render_cache'] = NestedArray::mergeDeep(isset($this->view->element['#post_render_cache']) ? $this->view->element['#post_render_cache'] : [], $this->storage['postRenderCache']);
-  }
-
-  /**
    * Calculates and sets a cache ID used for the result cache.
    *
    * @return string
diff --git a/core/modules/views/src/Plugin/views/cache/Time.php b/core/modules/views/src/Plugin/views/cache/Time.php
index b318cfa..3c43755 100644
--- a/core/modules/views/src/Plugin/views/cache/Time.php
+++ b/core/modules/views/src/Plugin/views/cache/Time.php
@@ -9,6 +9,7 @@
 
 use Drupal\Core\Datetime\DateFormatter;
 use Drupal\Core\Cache\Cache;
+use Drupal\Core\Render\RendererInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Drupal\Core\Form\FormStateInterface;
 
@@ -46,12 +47,14 @@ class Time extends CachePluginBase {
    *   The plugin_id for the plugin instance.
    * @param mixed $plugin_definition
    *   The plugin implementation definition.
+   * @param \Drupal\Core\Render\RendererInterface $renderer
+   *   The renderer service.
    * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
    *   The date formatter service.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatter $date_formatter) {
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $renderer, DateFormatter $date_formatter) {
     $this->dateFormatter = $date_formatter;
-    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    parent::__construct($configuration, $plugin_id, $plugin_definition, $renderer);
   }
 
   /**
@@ -62,6 +65,7 @@ public static function create(ContainerInterface $container, array $configuratio
       $configuration,
       $plugin_id,
       $plugin_definition,
+      $container->get('renderer'),
       $container->get('date.formatter')
     );
   }
