Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] Include the format in the cache key #19352

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
[Serializer] Include the format in the cache key
  • Loading branch information
dunglas committed Jul 12, 2016
commit 0385d103875aa2464a7443fc85b367911b1c1b80
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ 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);
$context['cache_key'] = $this->getCacheKey($format, $context);
}

if ($this->isCircularReference($object, $context)) {
Expand Down Expand Up @@ -169,7 +169,7 @@ 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);
$context['cache_key'] = $this->getCacheKey($format, $context);
}
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
$normalizedData = $this->prepareForDenormalization($data);
Expand Down Expand Up @@ -336,14 +336,15 @@ private function isMaxDepthReached($class, $attribute, array &$context)
/**
* Gets the cache key to use.
*
* @param array $context
* @param string|null $format
* @param array $context
*
* @return bool|string
*/
private function getCacheKey(array $context)
private function getCacheKey($format, array $context)
{
try {
return md5(serialize($context));
return $format.'-'.md5(serialize($context));
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be inside the md5?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's already a string and it makes the key more explicit. On the other hand if the format name is very long, it can hit the max key size. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer inside the md5: not format change for keys, shorters keys.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

} catch (\Exception $exception) {
// The context cannot be serialized, skip the cache
return false;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.