function ResourceObjectNormalizationCacher::set

Same name in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::set()
  2. 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::set()
  3. 11.x core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::set()

Writes a normalization to cache.

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObject $object: The resource object for which to generate a cache item.

array $normalization_parts: The normalization parts to cache.

1 call to ResourceObjectNormalizationCacher::set()
ResourceObjectNormalizationCacher::onTerminate in core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php
Writes normalizations of entities to cache, if any were created.

File

core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php, line 182

Class

ResourceObjectNormalizationCacher
Caches entity normalizations after the response has been sent.

Namespace

Drupal\jsonapi\EventSubscriber

Code

protected function set(ResourceObject $object, array $normalization_parts) {
    // @todo Investigate whether to cache POST and PATCH requests.
    // @todo Follow up on https://www.drupal.org/project/drupal/issues/3381898.
    if (!$this->requestStack
        ->getCurrentRequest()
        ->isMethodCacheable()) {
        return;
    }
    // Merge the entity's cacheability metadata with that of the normalization
    // parts, so that VariationCache can take care of cache redirects for us.
    $cacheability = CacheableMetadata::createFromObject($object)->merge(static::mergeCacheableDependencies($normalization_parts[static::RESOURCE_CACHE_SUBSET_BASE]))
        ->merge(static::mergeCacheableDependencies($normalization_parts[static::RESOURCE_CACHE_SUBSET_FIELDS]));
    $this->variationCache
        ->set($this->generateCacheKeys($object), $normalization_parts, $cacheability, new CacheableMetadata());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.