public 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.

Parameters

Request $request A Request instance:

Response $response A Response instance:

Return value

string The key under which the response is stored

Throws

\RuntimeException

Overrides StoreInterface::write

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpCache/Store.php, line 163

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) {
  $key = $this
    ->getCacheKey($request);
  $storedEnv = $this
    ->persistRequest($request);

  // write the response body to the entity store if this is the original response
  if (!$response->headers
    ->has('X-Content-Digest')) {
    $digest = $this
      ->generateContentDigest($response);
    if (false === $this
      ->save($digest, $response
      ->getContent())) {
      throw new \RuntimeException('Unable to store the entity.');
    }
    $response->headers
      ->set('X-Content-Digest', $digest);
    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 = array();
  $vary = $response->headers
    ->get('vary');
  foreach ($this
    ->getMetadata($key) as $entry) {
    if (!isset($entry[1]['vary'][0])) {
      $entry[1]['vary'] = array(
        '',
      );
    }
    if ($vary != $entry[1]['vary'][0] || !$this
      ->requestsMatch($vary, $entry[0], $storedEnv)) {
      $entries[] = $entry;
    }
  }
  $headers = $this
    ->persistResponse($response);
  unset($headers['age']);
  array_unshift($entries, array(
    $storedEnv,
    $headers,
  ));
  if (false === $this
    ->save($key, serialize($entries))) {
    throw new \RuntimeException('Unable to store the metadata.');
  }
  return $key;
}