Skip to content

Commit bef15ce

Browse files
[Cache] Fix exception handling in AbstractTrait::clear()
1 parent bb644c1 commit bef15ce

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Symfony/Component/Cache/Tests/Simple/CacheTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function testDefaultLifeTime()
4242
}
4343

4444
$cache = $this->createSimpleCache(2);
45+
$cache->clear();
4546

4647
$cache->set('key.dlt', 'value');
4748
sleep(1);
@@ -50,6 +51,8 @@ public function testDefaultLifeTime()
5051

5152
sleep(2);
5253
$this->assertNull($cache->get('key.dlt'));
54+
55+
$cache->clear();
5356
}
5457

5558
public function testNotUnserializable()
@@ -59,6 +62,7 @@ public function testNotUnserializable()
5962
}
6063

6164
$cache = $this->createSimpleCache();
65+
$cache->clear();
6266

6367
$cache->set('foo', new NotUnserializable());
6468

@@ -69,6 +73,8 @@ public function testNotUnserializable()
6973
foreach ($cache->getMultiple(array('foo')) as $value) {
7074
}
7175
$this->assertNull($value);
76+
77+
$cache->clear();
7278
}
7379

7480
public function testPrune()
@@ -83,6 +89,7 @@ public function testPrune()
8389

8490
/** @var PruneableInterface|CacheInterface $cache */
8591
$cache = $this->createSimpleCache();
92+
$cache->clear();
8693

8794
$cache->set('foo', 'foo-val', new \DateInterval('PT05S'));
8895
$cache->set('bar', 'bar-val', new \DateInterval('PT10S'));
@@ -124,6 +131,8 @@ public function testPrune()
124131
$cache->prune();
125132
$this->assertFalse($this->isPruned($cache, 'foo'));
126133
$this->assertTrue($this->isPruned($cache, 'qux'));
134+
135+
$cache->clear();
127136
}
128137
}
129138

src/Symfony/Component/Cache/Traits/AbstractTrait.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,20 @@ public function hasItem($key)
104104
*/
105105
public function clear()
106106
{
107-
if ($cleared = $this->versioningIsEnabled) {
108-
$this->namespaceVersion = 2;
109-
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) {
110-
$this->namespaceVersion = 1 + (int) $v;
111-
}
112-
$this->namespaceVersion .= ':';
113-
$cleared = $this->doSave(array('@'.$this->namespace => $this->namespaceVersion), 0);
114-
}
115107
$this->deferred = array();
116-
117108
try {
109+
if ($cleared = $this->versioningIsEnabled) {
110+
$namespaceVersion = 2;
111+
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) {
112+
$namespaceVersion = 1 + (int) $v;
113+
}
114+
$namespaceVersion .= ':';
115+
$cleared = $this->doSave(array('@'.$this->namespace => $namespaceVersion), 0);
116+
if ($cleared = true === $cleared || array() === $cleared) {
117+
$this->namespaceVersion = $namespaceVersion;
118+
}
119+
}
120+
118121
return $this->doClear($this->namespace) || $cleared;
119122
} catch (\Exception $e) {
120123
CacheItem::log($this->logger, 'Failed to clear the cache', array('exception' => $e));

0 commit comments

Comments
 (0)