Skip to content

[Cache] Add nonce based cache invalidation to ApcuAdapter #18716

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
May 12, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,8 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild

private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
$nonce = substr(str_replace('/', '-', base64_encode(md5(uniqid(mt_rand(), true), true))), 0, -2);
$container->getDefinition('cache.adapter.apcu')->replaceArgument(2, $nonce);
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2, $config['directory']);

foreach (array('doctrine', 'psr6', 'redis') as $name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<tag name="monolog.logger" channel="cache" />
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
<argument /> <!-- nonce -->
<call method="setLogger">
<argument type="service" id="logger" on-invalid="ignore" />
</call>
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

namespace Symfony\Component\Cache\Adapter;

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

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ApcuAdapter extends AbstractAdapter
{
public function __construct($namespace = '', $defaultLifetime = 0)
public function __construct($namespace = '', $defaultLifetime = 0, $nonce = null)
{
if (!function_exists('apcu_fetch') || !ini_get('apc.enabled') || ('cli' === PHP_SAPI && !ini_get('apc.enable_cli'))) {
throw new CacheException('APCu is not enabled');
Expand All @@ -27,6 +28,15 @@ public function __construct($namespace = '', $defaultLifetime = 0)
ini_set('apc.use_request_time', 0);
}
parent::__construct($namespace, $defaultLifetime);

if (null !== $nonce) {
CacheItem::validateKey($nonce);

if (!apcu_exists($nonce.':nonce'.$namespace)) {
$this->clear($namespace);
apcu_add($nonce.':nonce'.$namespace, null);
}
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,29 @@ public function testUnserializable()
$item = $pool->getItem('foo');
$this->assertFalse($item->isHit());
}

public function testNonce()
{
$namespace = str_replace('\\', '.', __CLASS__);

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

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

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

$pool2 = new ApcuAdapter($namespace, 0, 'p2');

$item = $pool2->getItem('foo');
$this->assertFalse($item->isHit());
$this->assertNull($item->get());

$item = $pool1->getItem('foo');
$this->assertFalse($item->isHit());
$this->assertNull($item->get());
}
}