function ResourceObjectNormalizationCacher::get
Same name in other branches
- 9 core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::get()
- 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::get()
- 10 core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::get()
Reads an entity normalization from cache.
The returned normalization may only be a partial normalization because it was previously normalized with a sparse fieldset.
Parameters
\Drupal\jsonapi\JsonApiResource\ResourceObject $object: The resource object for which to generate a cache item.
Return value
array|false The cached normalization parts, or FALSE if not yet cached.
See also
\Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::renderArrayToResponse()
File
-
core/
modules/ jsonapi/ src/ EventSubscriber/ ResourceObjectNormalizationCacher.php, line 98
Class
- ResourceObjectNormalizationCacher
- Caches entity normalizations after the response has been sent.
Namespace
Drupal\jsonapi\EventSubscriberCode
public function get(ResourceObject $object) {
// @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 FALSE;
}
$cached = $this->variationCache
->get($this->generateCacheKeys($object), new CacheableMetadata());
if (!$cached) {
return FALSE;
}
// When a cache hit occurs, we first calculate the remaining time before the
// cached record expires, ensuring that we do not reset the expiration with
// one that might have been generated on an earlier timestamp. This is done
// by subtracting the current timestamp from the cached record's expiration
// timestamp. If the max-age is set, we adjust it by merging the calculated
// remaining time with the original max-age of the cached item, ensuring
// that the expiration remains accurate based on the current cache state
// and timestamp.
$normalizer_values = $cached->data;
assert(is_array($normalizer_values));
if ($cached->expire >= 0) {
$max_age = max($cached->expire - $this->requestStack
->getCurrentRequest()->server
->get('REQUEST_TIME'), 0);
$cacheability = new CacheableMetadata();
$cacheability->setCacheMaxAge($max_age);
$subsets = [
ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_BASE,
ResourceObjectNormalizationCacher::RESOURCE_CACHE_SUBSET_FIELDS,
];
foreach ($subsets as $subset) {
foreach ($normalizer_values[$subset] as $name => $normalization) {
assert($normalization instanceof CacheableNormalization);
if ($normalization->getCacheMaxAge() > 0) {
$normalizer_values[$subset][$name] = $normalization->withCacheableDependency($cacheability);
}
}
}
}
return $normalizer_values;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.