Skip to content

Commit

Permalink
[Serializer] Use $context['cache_key'] to enhance caching
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 12, 2016
1 parent c1740fc commit 8560c13
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Expand Up @@ -55,6 +55,9 @@ public function supportsNormalization($data, $format = null)
*/
public function normalize($object, $format = null, array $context = array())
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($context);
}
if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object);
}
Expand Down Expand Up @@ -104,6 +107,9 @@ public function supportsDenormalization($data, $type, $format = null)
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($context);
}
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
$normalizedData = $this->prepareForDenormalization($data);

Expand All @@ -130,6 +136,16 @@ public function denormalize($data, $class, $format = null, array $context = arra
return $object;
}

private function getCacheKey(array $context)
{
try {
return md5(serialize($context));
} catch (\Exception $exception) {
// The context cannot be serialized, skip the cache
return false;
}
}

/**
* Gets and caches attributes for this class and context.
*
Expand All @@ -140,37 +156,39 @@ public function denormalize($data, $class, $format = null, array $context = arra
*/
private function getAttributes($object, array $context)
{
try {
$serializedContext = serialize($context);
} catch (\Exception $exception) {
// The context cannot be serialized, skip the cache
return $this->extractAttributes($object, $context);
}

$key = sprintf('%s-%s', get_class($object), $serializedContext);
$class = get_class($object);
$key = $class.'-'.$context['cache_key'];

if (isset($this->attributesCache[$key])) {
return $this->attributesCache[$key];
}

return $this->attributesCache[$key] = $this->extractAttributes($object, $context);
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);

if (false !== $allowedAttributes) {
if ($context['cache_key']) {
$this->attributesCache[$key] = $allowedAttributes;
}

return $allowedAttributes;
}

if (isset($this->attributesCache[$class])) {
return $this->attributesCache[$class];
}

return $this->attributesCache[$class] = $this->extractAttributes($object);
}

/**
* Extracts attributes for this class and context.
*
* @param object $object
* @param array $context
*
* @return string[]
*/
private function extractAttributes($object, array $context)
private function extractAttributes($object)
{
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
if (false !== $allowedAttributes) {
return $allowedAttributes;
}

// If not using groups, detect manually
$attributes = array();

Expand Down

0 comments on commit 8560c13

Please sign in to comment.