Skip to content

Instantly share code, notes, and snippets.

@LionsAd
Created February 18, 2015 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LionsAd/fef2568413569c13e952 to your computer and use it in GitHub Desktop.
Save LionsAd/fef2568413569c13e952 to your computer and use it in GitHub Desktop.
SmartCache++
diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php
index 69e99a0..8bdf86a 100644
--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -184,6 +184,11 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
$this->bubbleStack();
return $elements['#markup'];
}
+
+ // Store the cache ID for later comparison.
+ if ($this->requestStack->getCurrentRequest()->isMethodSafe() && $cid = $this->createCacheID($elements)) {
+ $elements['#cache_old_id'] = $cid;
+ }
}
// If the default values for this element have not been loaded yet, populate
@@ -493,6 +498,9 @@ protected function cacheGet(array $elements) {
if (!empty($cid) && $cache = $this->cacheFactory->get($bin)->get($cid)) {
$cached_element = $cache->data;
+ if (isset($cached_element['#cache_try_again'])) {
+ return $this->cacheGet($cached_element);
+ }
// Return the cached element.
return $cached_element;
}
@@ -530,7 +538,17 @@ protected function cacheSet(array &$elements) {
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
$expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : Cache::PERMANENT;
- $this->cacheFactory->get($bin)->set($cid, $data, $expire, $data['#cache']['tags']);
+ $cache = $this->cacheFactory->get($bin);
+
+ // This assumes bubbling worked correctly and createCacheID() working on contexts instead of cache keys.
+ if (isset($elements['#cache_old_id']) && $cid != $elements['#cache_old_id']) {
+ $redir_data = [
+ '#cache_try_again' => TRUE,
+ '#cache' => $elements['#cache'],
+ ];
+ $cache->set($elements['#cache_old_id'], $redir_data, $expire, $data['#cache']['tags']);
+ }
+ $cache->set($cid, $data, $expire, $data['#cache']['tags']);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment