[Cache] Fix exception handling in AbstractTrait::clear()

This commit is contained in:
Nicolas Grekas 2018-06-20 11:04:54 +02:00
parent bb644c1df8
commit bef15cebca
2 changed files with 21 additions and 9 deletions

View File

@ -42,6 +42,7 @@ abstract class CacheTestCase extends SimpleCacheTest
}
$cache = $this->createSimpleCache(2);
$cache->clear();
$cache->set('key.dlt', 'value');
sleep(1);
@ -50,6 +51,8 @@ abstract class CacheTestCase extends SimpleCacheTest
sleep(2);
$this->assertNull($cache->get('key.dlt'));
$cache->clear();
}
public function testNotUnserializable()
@ -59,6 +62,7 @@ abstract class CacheTestCase extends SimpleCacheTest
}
$cache = $this->createSimpleCache();
$cache->clear();
$cache->set('foo', new NotUnserializable());
@ -69,6 +73,8 @@ abstract class CacheTestCase extends SimpleCacheTest
foreach ($cache->getMultiple(array('foo')) as $value) {
}
$this->assertNull($value);
$cache->clear();
}
public function testPrune()
@ -83,6 +89,7 @@ abstract class CacheTestCase extends SimpleCacheTest
/** @var PruneableInterface|CacheInterface $cache */
$cache = $this->createSimpleCache();
$cache->clear();
$cache->set('foo', 'foo-val', new \DateInterval('PT05S'));
$cache->set('bar', 'bar-val', new \DateInterval('PT10S'));
@ -124,6 +131,8 @@ abstract class CacheTestCase extends SimpleCacheTest
$cache->prune();
$this->assertFalse($this->isPruned($cache, 'foo'));
$this->assertTrue($this->isPruned($cache, 'qux'));
$cache->clear();
}
}

View File

@ -104,17 +104,20 @@ trait AbstractTrait
*/
public function clear()
{
if ($cleared = $this->versioningIsEnabled) {
$this->namespaceVersion = 2;
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) {
$this->namespaceVersion = 1 + (int) $v;
}
$this->namespaceVersion .= ':';
$cleared = $this->doSave(array('@'.$this->namespace => $this->namespaceVersion), 0);
}
$this->deferred = array();
try {
if ($cleared = $this->versioningIsEnabled) {
$namespaceVersion = 2;
foreach ($this->doFetch(array('@'.$this->namespace)) as $v) {
$namespaceVersion = 1 + (int) $v;
}
$namespaceVersion .= ':';
$cleared = $this->doSave(array('@'.$this->namespace => $namespaceVersion), 0);
if ($cleared = true === $cleared || array() === $cleared) {
$this->namespaceVersion = $namespaceVersion;
}
}
return $this->doClear($this->namespace) || $cleared;
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to clear the cache', array('exception' => $e));