Skip to content

Commit 996f69d

Browse files
committed
Merge pull request #2 from nicolas-grekas/ser-cache
[Serializer] Use $context['cache_key'] to enhance caching
2 parents c1740fc + 8560c13 commit 996f69d

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

+34-16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function supportsNormalization($data, $format = null)
5555
*/
5656
public function normalize($object, $format = null, array $context = array())
5757
{
58+
if (!isset($context['cache_key'])) {
59+
$context['cache_key'] = $this->getCacheKey($context);
60+
}
5861
if ($this->isCircularReference($object, $context)) {
5962
return $this->handleCircularReference($object);
6063
}
@@ -104,6 +107,9 @@ public function supportsDenormalization($data, $type, $format = null)
104107
*/
105108
public function denormalize($data, $class, $format = null, array $context = array())
106109
{
110+
if (!isset($context['cache_key'])) {
111+
$context['cache_key'] = $this->getCacheKey($context);
112+
}
107113
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
108114
$normalizedData = $this->prepareForDenormalization($data);
109115

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

139+
private function getCacheKey(array $context)
140+
{
141+
try {
142+
return md5(serialize($context));
143+
} catch (\Exception $exception) {
144+
// The context cannot be serialized, skip the cache
145+
return false;
146+
}
147+
}
148+
133149
/**
134150
* Gets and caches attributes for this class and context.
135151
*
@@ -140,37 +156,39 @@ public function denormalize($data, $class, $format = null, array $context = arra
140156
*/
141157
private function getAttributes($object, array $context)
142158
{
143-
try {
144-
$serializedContext = serialize($context);
145-
} catch (\Exception $exception) {
146-
// The context cannot be serialized, skip the cache
147-
return $this->extractAttributes($object, $context);
148-
}
149-
150-
$key = sprintf('%s-%s', get_class($object), $serializedContext);
159+
$class = get_class($object);
160+
$key = $class.'-'.$context['cache_key'];
151161

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

156-
return $this->attributesCache[$key] = $this->extractAttributes($object, $context);
166+
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
167+
168+
if (false !== $allowedAttributes) {
169+
if ($context['cache_key']) {
170+
$this->attributesCache[$key] = $allowedAttributes;
171+
}
172+
173+
return $allowedAttributes;
174+
}
175+
176+
if (isset($this->attributesCache[$class])) {
177+
return $this->attributesCache[$class];
178+
}
179+
180+
return $this->attributesCache[$class] = $this->extractAttributes($object);
157181
}
158182

159183
/**
160184
* Extracts attributes for this class and context.
161185
*
162186
* @param object $object
163-
* @param array $context
164187
*
165188
* @return string[]
166189
*/
167-
private function extractAttributes($object, array $context)
190+
private function extractAttributes($object)
168191
{
169-
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
170-
if (false !== $allowedAttributes) {
171-
return $allowedAttributes;
172-
}
173-
174192
// If not using groups, detect manually
175193
$attributes = array();
176194

0 commit comments

Comments
 (0)