Skip to content
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());
}
}