Skip to content

[Cache] Support a custom serializer in the ApcuAdapter class #40602

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

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@

use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ApcuAdapter extends AbstractAdapter
{
private $marshaller;

/**
* @throws CacheException if APCu is not enabled
*/
public function __construct(string $namespace = '', int $defaultLifetime = 0, string $version = null)
public function __construct(string $namespace = '', int $defaultLifetime = 0, string $version = null, ?MarshallerInterface $marshaller = null)
{
if (!static::isSupported()) {
throw new CacheException('APCu is not enabled.');
Expand All @@ -40,6 +43,8 @@ public function __construct(string $namespace = '', int $defaultLifetime = 0, st
apcu_add($version.'@'.$namespace, null);
}
}

$this->marshaller = $marshaller;
}

public static function isSupported()
Expand All @@ -57,7 +62,7 @@ protected function doFetch(array $ids)
$values = [];
foreach (apcu_fetch($ids, $ok) ?: [] as $k => $v) {
if (null !== $v || $ok) {
$values[$k] = $v;
$values[$k] = null !== $this->marshaller ? $this->marshaller->unmarshall($v) : $v;
}
}

Expand Down Expand Up @@ -104,6 +109,10 @@ protected function doDelete(array $ids)
*/
protected function doSave(array $values, int $lifetime)
{
if (null !== $this->marshaller && (!$values = $this->marshaller->marshall($values, $failed))) {
return $failed;
}

try {
if (false === $failures = apcu_store($values, null, $lifetime)) {
$failures = $values;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* added support for connecting to Redis Sentinel clusters when using the Redis PHP extension
* add support for a custom serializer to the `ApcuAdapter` class

5.2.0
-----
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Marshaller\MarshallerInterface;

class ApcuAdapterTest extends AdapterTestCase
{
Expand Down Expand Up @@ -122,4 +123,30 @@ public function testWithCliSapi()
restore_error_handler();
}
}

public function testCacheItemValueRunsThroughMarshaller()
{
$namespace = str_replace('\\', '.', static::class);

$marshaller = $this->createMock(MarshallerInterface::class);
$marshaller->expects($this->once())
->method('marshall')
->with([$namespace.':foo' => 'bar'])
->willReturn([$namespace.':foo' => 'bar_serialized']);

$marshaller->expects($this->once())
->method('unmarshall')
->with('bar_serialized')
->willReturn('bar');

$pool = new ApcuAdapter($namespace, 0, 'p1', $marshaller);

$item = $pool->getItem('foo');
$this->assertFalse($item->isHit());
$this->assertTrue($pool->save($item->set('bar')));

$item = $pool->getItem('foo');
$this->assertTrue($item->isHit());
$this->assertSame('bar', $item->get());
}
}