Skip to content

fix normalizer cache key generation #1894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Hal/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public function supportsNormalization($data, $format = null)
*/
public function normalize($object, $format = null, array $context = [])
{
$context['cache_key'] = $this->getHalCacheKey($format, $context);
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getHalCacheKey($format, $context);
}

$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
$context = $this->initContext($resourceClass, $context);
$context['iri'] = $this->iriConverter->getIriFromItem($object);
Expand Down Expand Up @@ -99,8 +102,10 @@ protected function getAttributes($object, $format = null, array $context)
*/
private function getComponents($object, string $format = null, array $context)
{
if (false !== $context['cache_key'] && isset($this->componentsCache[$context['cache_key']])) {
return $this->componentsCache[$context['cache_key']];
$cacheKey = \get_class($object).'-'.$context['cache_key'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know the object class in the normalize method right? Could we pass the information to this function to avoid calling get_class? Might be a micro-optimization though. (same below)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\get_class is transformed in opcode, it's almost costless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For info I've copied the method used in core symfony, which is to add the class here


if (isset($this->componentsCache[$cacheKey])) {
return $this->componentsCache[$cacheKey];
}

$attributes = parent::getAttributes($object, $format, $context);
Expand Down Expand Up @@ -142,7 +147,7 @@ private function getComponents($object, string $format = null, array $context)
}

if (false !== $context['cache_key']) {
$this->componentsCache[$context['cache_key']] = $components;
$this->componentsCache[$cacheKey] = $components;
}

return $components;
Expand Down
16 changes: 12 additions & 4 deletions src/JsonApi/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function supportsNormalization($data, $format = null)
*/
public function normalize($object, $format = null, array $context = [])
{
$context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getJsonApiCacheKey($format, $context);
}

// Get and populate attributes data
$objectAttributesData = parent::normalize($object, $format, $context);
Expand Down Expand Up @@ -221,8 +223,10 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
*/
private function getComponents($object, string $format = null, array $context)
{
if (isset($this->componentsCache[$context['cache_key']])) {
return $this->componentsCache[$context['cache_key']];
$cacheKey = \get_class($object).'-'.$context['cache_key'];

if (isset($this->componentsCache[$cacheKey])) {
return $this->componentsCache[$cacheKey];
}

$attributes = parent::getAttributes($object, $format, $context);
Expand Down Expand Up @@ -267,7 +271,11 @@ private function getComponents($object, string $format = null, array $context)
$components['relationships'][] = $relation;
}

return $this->componentsCache[$context['cache_key']] = $components;
if (false !== $context['cache_key']) {
$this->componentsCache[$cacheKey] = $components;
}

return $components;
}

/**
Expand Down