commit 0562b954d7afff2a1072e978b46f84283baeff67
Author: Andy Postnikov <apostnikov@gmail.com>
Date:   Sat Nov 14 14:14:08 2015 +0300

    Add is_masqerading cache context and try use it

diff --git a/masquerade.services.yml b/masquerade.services.yml
index 4c395de..4c176a3 100644
--- a/masquerade.services.yml
+++ b/masquerade.services.yml
@@ -14,3 +14,8 @@ services:
     arguments: ['@masquerade']
     tags:
       - { name: access_check, applies_to: _masquerade_unmasquerade_access }
+  cache_context.is_masquerading:
+    class: Drupal\masquerade\Cache\MasqueradeCacheContext
+    arguments: ['@request_stack']
+    tags:
+      - { name: cache.context }
diff --git a/src/Access/UnmasqueradeAccessCheck.php b/src/Access/UnmasqueradeAccessCheck.php
index 5444b7d..83ae118 100644
--- a/src/Access/UnmasqueradeAccessCheck.php
+++ b/src/Access/UnmasqueradeAccessCheck.php
@@ -40,7 +40,7 @@ class UnmasqueradeAccessCheck implements AccessInterface {
    */
   public function access() {
     return AccessResult::allowedIf($this->masquerade->isMasquerading())
-      ->setCacheMaxAge(0);
+      ->addCacheContexts(['is_masquerading']);
   }
 
 }
diff --git a/src/Cache/MasqueradeCacheContext.php b/src/Cache/MasqueradeCacheContext.php
new file mode 100644
index 0000000..6d1aae6
--- /dev/null
+++ b/src/Cache/MasqueradeCacheContext.php
@@ -0,0 +1,46 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\masquerade\Cache\MasqueradeCacheContext.
+ */
+
+namespace Drupal\masquerade\Cache;
+
+use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Cache\Context\CacheContextInterface;
+use Drupal\Core\Cache\Context\RequestStackCacheContextBase;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
+
+/**
+ * Defines the MasqueradeCacheContext service, for "masquerade" caching.
+ *
+ * Cache context ID: 'is_masquerading'.
+ */
+class MasqueradeCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getLabel() {
+    return new TranslatableMarkup('User is masquerading');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getContext() {
+    return $this->requestStack
+      ->getCurrentRequest()
+      ->getSession()
+      ->has('masquerading');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheableMetadata() {
+    return new CacheableMetadata();
+  }
+
+}
diff --git a/src/Plugin/Block/MasqueradeBlock.php b/src/Plugin/Block/MasqueradeBlock.php
index 97716f2..1d12820 100644
--- a/src/Plugin/Block/MasqueradeBlock.php
+++ b/src/Plugin/Block/MasqueradeBlock.php
@@ -9,6 +9,7 @@ namespace Drupal\masquerade\Plugin\Block;
 
 use Drupal\Core\Access\AccessResult;
 use Drupal\Core\Block\BlockBase;
+use Drupal\Core\Cache\Cache;
 use Drupal\Core\Form\FormBuilderInterface;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Session\AccountInterface;
@@ -85,9 +86,8 @@ class MasqueradeBlock extends BlockBase implements ContainerFactoryPluginInterfa
   /**
    * {@inheritdoc}
    */
-  public function getCacheMaxAge() {
-    // @todo Fix caching https://www.drupal.org/node/2448699.
-    return 0;
+  public function getCacheContexts() {
+    return Cache::mergeContexts(parent::getCacheContexts(), ['is_masquerading']);
   }
 
   /**
