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

Breadcrumb

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

function Store::write

Writes a cache entry to the store for the given Request and Response.

Existing entries are read and any that match the response are removed. This method calls write with the new list of cache entries.

Throws

\RuntimeException

Overrides StoreInterface::write

File

vendor/symfony/http-kernel/HttpCache/Store.php, line 178

Class

Store
Store implements all the logic for storing cache metadata (Request and Response headers).

Namespace

Symfony\Component\HttpKernel\HttpCache

Code

public function write(Request $request, Response $response) : string {
    $key = $this->getCacheKey($request);
    $storedEnv = $this->persistRequest($request);
    if ($response->headers
        ->has('X-Body-File')) {
        // Assume the response came from disk, but at least perform some safeguard checks
        if (!$response->headers
            ->has('X-Content-Digest')) {
            throw new \RuntimeException('A restored response must have the X-Content-Digest header.');
        }
        $digest = $response->headers
            ->get('X-Content-Digest');
        if ($this->getPath($digest) !== $response->headers
            ->get('X-Body-File')) {
            throw new \RuntimeException('X-Body-File and X-Content-Digest do not match.');
        }
        // Everything seems ok, omit writing content to disk
    }
    else {
        $digest = $this->generateContentDigest($response);
        $response->headers
            ->set('X-Content-Digest', $digest);
        if (!$this->save($digest, $response->getContent(), false)) {
            throw new \RuntimeException('Unable to store the entity.');
        }
        if (!$response->headers
            ->has('Transfer-Encoding')) {
            $response->headers
                ->set('Content-Length', \strlen($response->getContent()));
        }
    }
    // read existing cache entries, remove non-varying, and add this one to the list
    $entries = [];
    $vary = $response->headers
        ->get('vary');
    foreach ($this->getMetadata($key) as $entry) {
        if (!isset($entry[1]['vary'][0])) {
            $entry[1]['vary'] = [
                '',
            ];
        }
        if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
            $entries[] = $entry;
        }
    }
    $headers = $this->persistResponse($response);
    unset($headers['age']);
    foreach ($this->options['private_headers'] as $h) {
        unset($headers[strtolower($h)]);
    }
    array_unshift($entries, [
        $storedEnv,
        $headers,
    ]);
    if (!$this->save($key, serialize($entries))) {
        throw new \RuntimeException('Unable to store the metadata.');
    }
    return $key;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal