generateCacheKey example



    /** * Returns a cache key for the given Request. */
    private function getCacheKey(Request $request): string
    {
        if (isset($this->keyCache[$request])) {
            return $this->keyCache[$request];
        }

        return $this->keyCache[$request] = $this->generateCacheKey($request);
    }

    /** * Persists the Request HTTP headers. */
    private function persistRequest(Request $request): array
    {
        return $request->headers->all();
    }

    /** * Persists the Response HTTP headers. */

  public function negotiate(string $component_id, array $all_definitions): ?string {
    $cache_key = $this->generateCacheKey($component_id);
    $cached_data = $this->cache[$cache_key] ?? NULL;
    if (isset($cached_data)) {
      return $cached_data;
    }
    $negotiated = $this->doNegotiate($component_id$all_definitions);
    $this->cache[$cache_key] = $negotiated;
    return $negotiated;
  }

  /** * Negotiates the active component for the current request. * * @param string $component_id * The requested component id. Ex: 'my-button', 'my-button--primary', * 'sdc_example:my-button--primary', ... * @param array[] $all_definitions * All the plugin definitions for components keyed by plugin ID. * * @return string|null * The negotiated plugin ID or null if no negotiation was successful. */
            // Make sure to deal with an array in the rest of the code             if (!\is_array($constraints)) {
                $constraints = [$constraints];
            }

            $metadata = new GenericMetadata();
            $metadata->addConstraints($constraints);

            $this->validateGenericNode(
                $value,
                $previousObject,
                \is_object($value) ? $this->generateCacheKey($value) : null,
                $metadata,
                $this->defaultPropertyPath,
                $groups,
                null,
                TraversalStrategy::IMPLICIT,
                $this->context
            );

            $this->context->setNode($previousValue$previousObject$previousMetadata$previousPath);
            $this->context->setGroup($previousGroup);

            
if ($this->ttl === 0) {
            return true;
        }

        $headers = [];

        foreach ($response->headers() as $header) {
            $headers[$header->getName()] = $header->getValueLine();
        }

        return $this->cache->save(
            $this->generateCacheKey($request),
            serialize(['headers' => $headers, 'output' => $response->getBody()]),
            $this->ttl
        );
    }

    /** * Gets the cached response for the request. * * @param CLIRequest|IncomingRequest $request */
    public function get($request, ResponseInterface $response): ?ResponseInterface
    {
foreach ($locales as $locale) {
            $phraseLocale = $this->getLocale($locale);

            foreach ($domains as $domain) {
                $this->readConfig['tags'] = $domain;

                if ($this->isFallbackLocaleEnabled && null !== $fallbackLocale = $this->getFallbackLocale($locale)) {
                    $this->readConfig['fallback_locale_id'] = $fallbackLocale;
                }

                $cacheKey = $this->generateCacheKey($locale$domain$this->readConfig);
                $cacheItem = $this->cache->getItem($cacheKey);

                $headers = [];
                $cachedResponse = null;

                if ($cacheItem->isHit() && null !== $cachedResponse = $cacheItem->get()) {
                    $headers = ['If-None-Match' => $cachedResponse['etag']];
                }

                $response = $this->httpClient->request('GET', 'locales/'.$phraseLocale.'/download', [
                    'query' => $this->readConfig,
                    
            // Make sure to deal with an array in the rest of the code             if (!\is_array($constraints)) {
                $constraints = [$constraints];
            }

            $metadata = new GenericMetadata();
            $metadata->addConstraints($constraints);

            $this->validateGenericNode(
                $value,
                $previousObject,
                \is_object($value) ? $this->generateCacheKey($value) : null,
                $metadata,
                $this->defaultPropertyPath,
                $groups,
                null,
                TraversalStrategy::IMPLICIT,
                $this->context
            );

            $this->context->setNode($previousValue$previousObject$previousMetadata$previousPath);
            $this->context->setGroup($previousGroup);

            


    /** * Returns a cache key for the given Request. */
    private function getCacheKey(Request $request): string
    {
        if (isset($this->keyCache[$request])) {
            return $this->keyCache[$request];
        }

        return $this->keyCache[$request] = $this->generateCacheKey($request);
    }

    /** * Persists the Request HTTP headers. */
    private function persistRequest(Request $request): array
    {
        return $request->headers->all();
    }

    /** * Persists the Response HTTP headers. */
Home | Imprint | This part of the site doesn't use cookies.