Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #33939 [Cache] add TagAwareMarshaller to optimize data storag…
…e when using AbstractTagAwareAdapter (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [Cache] add TagAwareMarshaller to optimize data storage when using AbstractTagAwareAdapter | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | #33924 | License | MIT | Doc PR | - This is the final touch in my series of PR that fixes the linked issue. Remarkably, the solutions I implemented for this issue are completely different than the one I described there. Fortunately, the issues themselves were correctly identified. Plannification of implementation is gambling :) /cc @andrerom Commits ------- 5a4a30c [Cache] add TagAwareMarshaller to optimize data storage when using AbstractTagAwareAdapter
- Loading branch information
Showing
7 changed files
with
185 additions
and
11 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
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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
<?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> | ||
*/ | ||
class TagAwareMarshaller implements MarshallerInterface | ||
{ | ||
private $marshaller; | ||
|
||
public function __construct(MarshallerInterface $marshaller = null) | ||
{ | ||
$this->marshaller = $marshaller ?? new DefaultMarshaller(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function marshall(array $values, ?array &$failed): array | ||
{ | ||
$failed = $notSerialized = $serialized = []; | ||
|
||
foreach ($values as $id => $value) { | ||
if (\is_array($value) && \is_array($value['tags'] ?? null) && \array_key_exists('value', $value) && \count($value) === 2 + (\is_string($value['meta'] ?? null) && 8 === \strlen($value['meta']))) { | ||
// if the value is an array with keys "tags", "value" and "meta", use a compact serialization format | ||
// magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F allow detecting this format quickly in unmarshall() | ||
|
||
$v = $this->marshaller->marshall($value, $f); | ||
|
||
if ($f) { | ||
$f = []; | ||
$failed[] = $id; | ||
} else { | ||
if ([] === $value['tags']) { | ||
$v['tags'] = ''; | ||
} | ||
|
||
$serialized[$id] = "\x9D".($value['meta'] ?? "\0\0\0\0\0\0\0\0").pack('N', \strlen($v['tags'])).$v['tags'].$v['value']; | ||
$serialized[$id][9] = "\x5F"; | ||
} | ||
} else { | ||
// other arbitratry values are serialized using the decorated marshaller below | ||
$notSerialized[$id] = $value; | ||
} | ||
} | ||
|
||
if ($notSerialized) { | ||
$serialized += $this->marshaller->marshall($notSerialized, $f); | ||
$failed = array_merge($failed, $f); | ||
} | ||
|
||
return $serialized; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unmarshall(string $value) | ||
{ | ||
// detect the compact format used in marshall() using magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F | ||
if (13 >= \strlen($value) || "\x9D" !== $value[0] || "\0" !== $value[5] || "\x5F" !== $value[9]) { | ||
return $this->marshaller->unmarshall($value); | ||
} | ||
|
||
// data consists of value, tags and metadata which we need to unpack | ||
$meta = substr($value, 1, 12); | ||
$meta[8] = "\0"; | ||
$tagLen = unpack('Nlen', $meta, 8)['len']; | ||
$meta = substr($meta, 0, 8); | ||
|
||
return [ | ||
'value' => $this->marshaller->unmarshall(substr($value, 13 + $tagLen)), | ||
'tags' => $tagLen ? $this->marshaller->unmarshall(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