Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cache] serialize tags separately from values in AbstractTagAwareAdapter
- Loading branch information
1 parent
6e7f325
commit a60d466
Showing
6 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Cache/Marshaller/TagAwareMarshaller.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Marshaller; | ||
|
||
/** | ||
* A marshaller optimized for data structures generated by AbstractTagAwareAdapter. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
* | ||
* @internal | ||
*/ | ||
class TagAwareMarshaller implements MarshallerInterface | ||
{ | ||
private $marshaller = true; | ||
|
||
public function __construct(MarshallerInterface $marshaller) | ||
{ | ||
$this->marshaller = $marshaller; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function marshall(array $values, ?array &$failed): array | ||
{ | ||
$failed = $notSerialized = $serialized = []; | ||
|
||
foreach ($values as $id => $value) { | ||
if (\is_array($value) && \is_string($value['tags'] ?? null) && \is_string($value['value'] ?? null) && \count($value) === 2 + (\is_string($value['meta'] ?? null) && 8 === \strlen($value['meta']))) { | ||
$serialized[$id] = "\x9D".($value['meta'] ?? "\0\0\0\0\0\0\0\0").pack('N', \strlen($value['tags'])).$value['tags'].$value['value']; | ||
$serialized[$id][9] = "\x5F"; | ||
} else { | ||
$notSerialized[$id] = $value; | ||
} | ||
} | ||
|
||
return $notSerialized ? $serialized + $this->marshaller->marshall($notSerialized, $failed) : $serialized; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unmarshall(string $value) | ||
{ | ||
if (13 >= \strlen($value) || "\x9D" !== $value[0] || "\0" !== $value[5] || "\x5F" !== $value[9]) { | ||
return $this->marshaller->unmarshall($value); | ||
} | ||
|
||
$meta = substr($value, 1, 12); | ||
$meta[8] = "\0"; | ||
$tagLen = unpack('Nlen', $meta, 8)['len']; | ||
$meta = substr($meta, 0, 8); | ||
|
||
return [ | ||
'value' => substr($value, 13 + $tagLen), | ||
'tags' => substr($value, 13, $tagLen), | ||
'meta' => "\0\0\0\0\0\0\0\0" === $meta ? null : $meta, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters