-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #34133 [Cache] add DeflateMarshaller - remove phpredis compre…
…ssion (nicolas-grekas) This PR was merged into the 4.4 branch. Discussion ---------- [Cache] add DeflateMarshaller - remove phpredis compression | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - phpredis compression doesn't play well with lua scripting as used in #33939 Let's remove it and provide a `DeflateMarshaller` instead. Ppl can use it via decoration: ```yaml services: Symfony\Component\Cache\Marshaller\DeflateMarshaller: decorates: cache.default_marshaller arguments: ['@symfony\Component\Cache\Marshaller\DeflateMarshaller.inner'] ``` It's not enabled by default because that might break pools that are shared between different apps. /cc @andrerom FYI Commits ------- 452c863 [Cache] add DeflateMarshaller - remove phpredis compression
- Loading branch information
Showing
5 changed files
with
118 additions
and
10 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
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/Cache/Marshaller/DeflateMarshaller.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,53 @@ | ||
<?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; | ||
|
||
use Symfony\Component\Cache\Exception\CacheException; | ||
|
||
/** | ||
* Compresses values using gzdeflate(). | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class DeflateMarshaller implements MarshallerInterface | ||
{ | ||
private $marshaller; | ||
|
||
public function __construct(MarshallerInterface $marshaller) | ||
{ | ||
if (!\function_exists('gzdeflate')) { | ||
throw new CacheException('The "zlib" PHP extension is not loaded.'); | ||
} | ||
|
||
$this->marshaller = $marshaller; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function marshall(array $values, ?array &$failed): array | ||
{ | ||
return array_map('gzdeflate', $this->marshaller->marshall($values, $failed)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unmarshall(string $value) | ||
{ | ||
if (false !== $inflatedValue = @gzinflate($value)) { | ||
$value = $inflatedValue; | ||
} | ||
|
||
return $this->marshaller->unmarshall($value); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Cache/Tests/Marshaller/DeflateMarshallerTest.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,52 @@ | ||
<?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\Tests\Marshaller; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Cache\Marshaller\DefaultMarshaller; | ||
use Symfony\Component\Cache\Marshaller\DeflateMarshaller; | ||
|
||
/** | ||
* @requires extension zlib | ||
*/ | ||
class DeflateMarshallerTest extends TestCase | ||
{ | ||
public function testMarshall() | ||
{ | ||
$defaultMarshaller = new DefaultMarshaller(); | ||
$deflateMarshaller = new DeflateMarshaller($defaultMarshaller); | ||
|
||
$values = ['abc' => [str_repeat('def', 100)]]; | ||
|
||
$failed = []; | ||
$defaultResult = $defaultMarshaller->marshall($values, $failed); | ||
|
||
$deflateResult = $deflateMarshaller->marshall($values, $failed); | ||
$deflateResult['abc'] = gzinflate($deflateResult['abc']); | ||
|
||
$this->assertSame($defaultResult, $deflateResult); | ||
} | ||
|
||
public function testUnmarshall() | ||
{ | ||
$defaultMarshaller = new DefaultMarshaller(); | ||
$deflateMarshaller = new DeflateMarshaller($defaultMarshaller); | ||
|
||
$values = ['abc' => [str_repeat('def', 100)]]; | ||
|
||
$defaultResult = $defaultMarshaller->marshall($values, $failed); | ||
$deflateResult = $deflateMarshaller->marshall($values, $failed); | ||
|
||
$this->assertSame($values['abc'], $deflateMarshaller->unmarshall($deflateResult['abc'])); | ||
$this->assertSame($values['abc'], $deflateMarshaller->unmarshall($defaultResult['abc'])); | ||
} | ||
} |
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