ContextCacheKeys example

$keys[] = 'en';
              break;

            case 'theme':
              $keys[] = 'stark';
              break;

            default:
              $keys[] = $context_id;
          }
        }
        return new ContextCacheKeys($keys);
      });
    $this->placeholderGenerator = new PlaceholderGenerator($this->rendererConfig);
    $this->renderCache = new PlaceholderingRenderCache($this->requestStack, $this->cacheFactory, $this->cacheContextsManager, $this->placeholderGenerator);
    $this->renderer = new Renderer($this->controllerResolver, $this->themeManager, $this->elementInfo, $this->placeholderGenerator, $this->renderCache, $this->requestStack, $this->rendererConfig);

    $container = new ContainerBuilder();
    $container->set('cache_contexts_manager', $this->cacheContextsManager);
    $container->set('render_cache', $this->renderCache);
    $container->set('renderer', $this->renderer);
    \Drupal::setContainer($container);
  }

  
$cacheable_metadata = $cacheable_metadata->merge($context->getCacheableMetadata($parameter));
    }

    sort($optimized_tokens);
    $keys = [];
    foreach (array_combine($optimized_tokensstatic::parseTokens($optimized_tokens)) as $context_token => $context) {
      [$context_id$parameter] = $context;
      $keys[] = '[' . $context_token . ']=' . $this->getService($context_id)->getContext($parameter);
    }

    // Create the returned object and merge in the cacheability metadata.     $context_cache_keys = new ContextCacheKeys($keys);
    return $context_cache_keys->merge($cacheable_metadata);
  }

  /** * Optimizes cache context tokens (the minimal representative subset). * * A minimal representative subset means that any cache context token in the * given set of cache context tokens that is a property of another cache * context cache context token in the set, is removed. * * Hence a minimal representative subset is the most compact representation * possible of a set of cache context tokens, that still captures the entire * universe of variations. * * If a cache context is being optimized away, it is able to set cacheable * metadata for itself which will be bubbled up. * * For example, when caching per user ('user'), also caching per role * ('user.roles') is meaningless because "per role" is implied by "per user". * * In the following examples, remember that the period indicates hierarchy and * the colon can be used to get a specific value of a calculated cache * context: * - ['a', 'a.b'] -> ['a'] * - ['a', 'a.b.c'] -> ['a'] * - ['a.b', 'a.b.c'] -> ['a.b'] * - ['a', 'a.b', 'a.b.c'] -> ['a'] * - ['x', 'x:foo'] -> ['x'] * - ['a', 'a.b.c:bar'] -> ['a'] * * @param string[] $context_tokens * A set of cache context tokens. * * @return string[] * A representative subset of the given set of cache context tokens.. */
Home | Imprint | This part of the site doesn't use cookies.