diff --git a/src/Symfony/Component/Cache/Traits/AbstractTrait.php b/src/Symfony/Component/Cache/Traits/AbstractTrait.php index 04660dcab9698..a2914fa3578b6 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractTrait.php @@ -119,13 +119,16 @@ public function clear(/*string $prefix = ''*/) } } $namespaceToClear = $this->namespace.$namespaceVersionToClear; - $namespaceVersion = strtr(substr_replace(base64_encode(pack('V', mt_rand())), static::NS_SEPARATOR, 5), '/', '_'); + $namespaceVersion = self::formatNamespaceVersion(mt_rand()); try { - $cleared = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0); + $e = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0); } catch (\Exception $e) { - $cleared = false; } - if ($cleared = true === $cleared || [] === $cleared) { + if (true !== $e && [] !== $e) { + $cleared = false; + $message = 'Failed to save the new namespace'.($e instanceof \Exception ? ': '.$e->getMessage() : '.'); + CacheItem::log($this->logger, $message, ['exception' => $e instanceof \Exception ? $e : null]); + } else { $this->namespaceVersion = $namespaceVersion; $this->ids = []; } @@ -267,12 +270,17 @@ private function getId($key): string foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) { $this->namespaceVersion = $v; } + $e = true; if ('1'.static::NS_SEPARATOR === $this->namespaceVersion) { - $this->namespaceVersion = strtr(substr_replace(base64_encode(pack('V', time())), static::NS_SEPARATOR, 5), '/', '_'); - $this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0); + $this->namespaceVersion = self::formatNamespaceVersion(time()); + $e = $this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0); } } catch (\Exception $e) { } + if (true !== $e && [] !== $e) { + $message = 'Failed to save the new namespace'.($e instanceof \Exception ? ': '.$e->getMessage() : '.'); + CacheItem::log($this->logger, $message, ['exception' => $e instanceof \Exception ? $e : null]); + } } if (\is_string($key) && isset($this->ids[$key])) { @@ -304,4 +312,9 @@ public static function handleUnserializeCallback($class) { throw new \DomainException('Class not found: '.$class); } + + private static function formatNamespaceVersion(int $value): string + { + return strtr(substr_replace(base64_encode(pack('V', $value)), static::NS_SEPARATOR, 5), '/', '_'); + } }