Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3bc0c33

Browse files
[Cache] Add nonce based cache invalidation to ApcuAdapter
1 parentb85ab60 commit3bc0c33

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10371037

10381038
privatefunctionregisterCacheConfiguration(array$config,ContainerBuilder$container,XmlFileLoader$loader)
10391039
{
1040+
$container->setParameter('kernel.cache_nonce',substr(base64_encode(md5(uniqid(mt_rand(),true),true)),0,4));
10401041
$container->getDefinition('cache.adapter.filesystem')->replaceArgument(2,$config['directory']);
10411042

10421043
foreach (array('doctrine','psr6','redis')as$name) {

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<tagname="monolog.logger"channel="cache" />
2828
<argument /><!-- namespace-->
2929
<argument /><!-- default lifetime-->
30+
<argument>%kernel.cache_nonce%</argument>
3031
<callmethod="setLogger">
3132
<argumenttype="service"id="logger"on-invalid="ignore" />
3233
</call>

‎src/Symfony/Component/Cache/Adapter/ApcuAdapter.php‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
*/
1919
class ApcuAdapterextends AbstractAdapter
2020
{
21-
publicfunction__construct($namespace ='',$defaultLifetime =0)
21+
private$nonce;
22+
private$nonceLen;
23+
24+
publicfunction__construct($namespace ='',$defaultLifetime =0,$nonce =null)
2225
{
2326
if (!function_exists('apcu_fetch') || !ini_get('apc.enabled') || ('cli' ===PHP_SAPI && !ini_get('apc.enable_cli'))) {
2427
thrownewCacheException('APCu is not enabled');
@@ -27,22 +30,49 @@ public function __construct($namespace = '', $defaultLifetime = 0)
2730
ini_set('apc.use_request_time',0);
2831
}
2932
parent::__construct($namespace,$defaultLifetime);
33+
$this->nonce = (string)$nonce;
34+
$this->nonceLen =strlen($nonce);
3035
}
3136

3237
/**
3338
* {@inheritdoc}
3439
*/
3540
protectedfunctiondoFetch(array$ids)
3641
{
37-
returnapcu_fetch($ids);
42+
$values =apcu_fetch($ids);
43+
44+
if ($this->nonceLen) {
45+
foreach ($valuesas$id =>$v) {
46+
if (!is_string($v) ||0 !==strpos($v,$this->nonce)) {
47+
apcu_delete($id);
48+
unset($values[$id]);
49+
}
50+
}
51+
foreach ($valuesas$id =>$v) {
52+
$values[$id] =unserialize(substr($v,$this->nonceLen));
53+
}
54+
}
55+
56+
return$values;
3857
}
3958

4059
/**
4160
* {@inheritdoc}
4261
*/
4362
protectedfunctiondoHave($id)
4463
{
45-
returnapcu_exists($id);
64+
if (!$this->nonceLen) {
65+
returnapcu_exists($id);
66+
}
67+
$v =apcu_fetch($id,$found);
68+
69+
if ($found && (!is_string($v) ||0 !==strpos($v,$this->nonce))) {
70+
apcu_delete($id);
71+
$found =false;
72+
}
73+
74+
return$found;
75+
4676
}
4777

4878
/**
@@ -73,12 +103,18 @@ protected function doDelete(array $ids)
73103
protectedfunctiondoSave(array$values,$lifetime)
74104
{
75105
try {
106+
if ($this->nonceLen) {
107+
foreach ($valuesas$id =>$v) {
108+
$values[$id] =$this->nonce.serialize($v);
109+
}
110+
}
111+
76112
returnarray_keys(apcu_store($values,null,$lifetime));
77113
}catch (\Error$e) {
78114
}catch (\Exception$e) {
79115
}
80116

81-
if (1 ===count($values)) {
117+
if (!$this->nonceLen &&1 ===count($values)) {
82118
// Workaround https://github.com/krakjoe/apcu/issues/170
83119
apcu_delete(key($values));
84120
}

‎src/Symfony/Component/Cache/Tests/Adapter/ApcuAdapterTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,24 @@ public function testUnserializable()
4343
$item =$pool->getItem('foo');
4444
$this->assertFalse($item->isHit());
4545
}
46+
47+
publicfunctiontestNonce()
48+
{
49+
$namespace =str_replace('\\','.',__CLASS__);
50+
$pool1 =newApcuAdapter($namespace,0,'p1');
51+
$pool2 =newApcuAdapter($namespace,0,'p2');
52+
53+
$item =$pool1->getItem('foo');
54+
$this->assertFalse($item->isHit());
55+
$this->assertTrue($pool1->save($item->set('bar')));
56+
$item =$pool1->getItem('foo');
57+
$this->assertTrue($item->isHit());
58+
$this->assertSame('bar',$item->get());
59+
$item =$pool2->getItem('foo');
60+
$this->assertFalse($item->isHit());
61+
$this->assertNull($item->get());
62+
$item =$pool1->getItem('foo');
63+
$this->assertFalse($item->isHit());
64+
$this->assertNull($item->get());
65+
}
4666
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp