You are here

protected function DataProducerProxy::cacheWrite in GraphQL 8.4

Store result values in cache.

Parameters

string $prefix:

mixed $value:

\Drupal\graphql\GraphQL\Execution\FieldContext $field:

1 call to DataProducerProxy::cacheWrite()
DataProducerProxy::resolveCached in src/Plugin/GraphQL/DataProducer/DataProducerProxy.php
Try to return a value from cache, otherwise invoke data producer.

File

src/Plugin/GraphQL/DataProducer/DataProducerProxy.php, line 314

Class

DataProducerProxy
A proxy class that lazy resolves data producers and has a result cache.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer

Code

protected function cacheWrite($prefix, $value, FieldContext $field) : void {

  // Bail out early if the field context is already uncacheable.
  if ($field
    ->getCacheMaxAge() === 0) {
    return;
  }
  $metadata = new CacheableMetadata();
  $metadata
    ->addCacheableDependency($field);

  // Do not add the cache contexts from the result value because they are not
  // known at fetch time and would render the written cache unusable.
  if ($value instanceof CacheableDependencyInterface) {
    $metadata
      ->addCacheTags($value
      ->getCacheTags());
    $metadata
      ->mergeCacheMaxAge($value
      ->getCacheMaxAge());
  }
  if ($metadata
    ->getCacheMaxAge() === 0) {
    return;
  }
  $expire = $this
    ->maxAgeToExpire($metadata
    ->getCacheMaxAge());
  $tags = $metadata
    ->getCacheTags();
  $tokens = $metadata
    ->getCacheContexts();
  $keys = !empty($tokens) ? $this->contextsManager
    ->convertTokensToKeys($tokens)
    ->getKeys() : [];
  $keys = serialize($keys);
  $this->cacheBackend
    ->setMultiple([
    "{$prefix}:context" => [
      'data' => $tokens,
      'expire' => $expire,
      'tags' => $tags,
    ],
    "{$prefix}:result:{$keys}" => [
      'data' => [
        $value,
        $metadata,
      ],
      'expire' => $expire,
      'tags' => $tags,
    ],
  ]);
}