Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ResponseCacheStrategy.php

function ResponseCacheStrategy::add

Overrides ResponseCacheStrategyInterface::add

1 call to ResponseCacheStrategy::add()
ResponseCacheStrategy::update in vendor/symfony/http-kernel/HttpCache/ResponseCacheStrategy.php
Updates the Response HTTP headers based on the embedded Responses.

File

vendor/symfony/http-kernel/HttpCache/ResponseCacheStrategy.php, line 57

Class

ResponseCacheStrategy
ResponseCacheStrategy knows how to compute the Response cache HTTP header based on the different response cache headers.

Namespace

Symfony\Component\HttpKernel\HttpCache

Code

public function add(Response $response) : void {
    ++$this->embeddedResponses;
    foreach (self::OVERRIDE_DIRECTIVES as $directive) {
        if ($response->headers
            ->hasCacheControlDirective($directive)) {
            $this->flagDirectives[$directive] = true;
        }
    }
    foreach (self::INHERIT_DIRECTIVES as $directive) {
        if (false !== $this->flagDirectives[$directive]) {
            $this->flagDirectives[$directive] = $response->headers
                ->hasCacheControlDirective($directive);
        }
    }
    $age = $response->getAge();
    $this->age = max($this->age, $age);
    if ($this->willMakeFinalResponseUncacheable($response)) {
        $this->isNotCacheableResponseEmbedded = true;
        return;
    }
    $maxAge = $response->headers
        ->hasCacheControlDirective('max-age') ? (int) $response->headers
        ->getCacheControlDirective('max-age') : null;
    $sharedMaxAge = $response->headers
        ->hasCacheControlDirective('s-maxage') ? (int) $response->headers
        ->getCacheControlDirective('s-maxage') : $maxAge;
    $expires = $response->getExpires();
    $expires = null !== $expires ? (int) $expires->format('U') - (int) $response->getDate()
        ->format('U') : null;
    // See https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2
    // If a response is "public" but does not have maximum lifetime, heuristics might be applied.
    // Do not store NULL values so the final response can have more limiting value from other responses.
    $isHeuristicallyCacheable = $response->headers
        ->hasCacheControlDirective('public') && null === $maxAge && null === $sharedMaxAge && null === $expires;
    if (!$isHeuristicallyCacheable || null !== $maxAge || null !== $expires) {
        $this->storeRelativeAgeDirective('max-age', $maxAge, $expires, $age);
    }
    if (!$isHeuristicallyCacheable || null !== $sharedMaxAge || null !== $expires) {
        $this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $expires, $age);
    }
    if (null !== $expires) {
        $this->ageDirectives['expires'] = true;
    }
    if (false !== $this->lastModified) {
        $lastModified = $response->getLastModified();
        $this->lastModified = $lastModified ? max($this->lastModified, $lastModified) : false;
    }
}
RSS feed
Powered by Drupal