From 02a6f248b5041d68bf6e18d3e254aa7a79039a78 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 17 Jun 2019 19:18:24 +0200 Subject: [PATCH 1/2] [Cache] fix versioning with SimpleCacheAdapter --- .../Component/Cache/Adapter/SimpleCacheAdapter.php | 2 ++ src/Symfony/Component/Cache/Traits/AbstractTrait.php | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php index de4dd745cd..dc3a9cec2d 100644 --- a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php @@ -78,6 +78,8 @@ class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface /** * @return string the namespace separator for cache keys + * + * @internal */ protected static function getNsSeparator() { diff --git a/src/Symfony/Component/Cache/Traits/AbstractTrait.php b/src/Symfony/Component/Cache/Traits/AbstractTrait.php index 83a86a5ee9..e225e07542 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractTrait.php @@ -108,7 +108,7 @@ trait AbstractTrait if ($cleared = $this->versioningIsEnabled) { $namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::getNsSeparator(), 5); try { - $cleared = $this->doSave(['@'.$this->namespace => $namespaceVersion], 0); + $cleared = $this->doSave([static::getNsSeparator().$this->namespace => $namespaceVersion], 0); } catch (\Exception $e) { $cleared = false; } @@ -237,12 +237,12 @@ trait AbstractTrait if ($this->versioningIsEnabled && '' === $this->namespaceVersion) { $this->namespaceVersion = '1'.static::getNsSeparator(); try { - foreach ($this->doFetch(['@'.$this->namespace]) as $v) { + foreach ($this->doFetch([static::getNsSeparator().$this->namespace]) as $v) { $this->namespaceVersion = $v; } if ('1'.static::getNsSeparator() === $this->namespaceVersion) { $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5); - $this->doSave(['@'.$this->namespace => $this->namespaceVersion], 0); + $this->doSave([static::getNsSeparator().$this->namespace => $this->namespaceVersion], 0); } } catch (\Exception $e) { } @@ -268,6 +268,8 @@ trait AbstractTrait /** * @return string the namespace separator for cache keys + * + * @internal */ protected static function getNsSeparator() { From 2bf5da51da24fb8b490f18ec973086df112840dd Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 17 Jun 2019 19:26:15 +0200 Subject: [PATCH 2/2] [Cache] replace getNsSeparator by NS_SEPARATOR on AbstractTrait --- .../Cache/Adapter/AbstractAdapter.php | 7 ++++- .../Cache/Adapter/SimpleCacheAdapter.php | 15 ++++------- .../Component/Cache/Simple/AbstractCache.php | 5 ++++ .../Component/Cache/Traits/AbstractTrait.php | 26 ++++++------------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index 70af0c7314..0868c16d47 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -25,6 +25,11 @@ use Symfony\Component\Cache\Traits\AbstractTrait; */ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface { + /** + * @internal + */ + const NS_SEPARATOR = ':'; + use AbstractTrait; private static $apcuSupported; @@ -39,7 +44,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface */ protected function __construct($namespace = '', $defaultLifetime = 0) { - $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::getNsSeparator(); + $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR; if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) { throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, \strlen($namespace), $namespace)); } diff --git a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php index dc3a9cec2d..d3d0ede648 100644 --- a/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php @@ -20,6 +20,11 @@ use Symfony\Component\Cache\Traits\ProxyTrait; */ class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface { + /** + * @internal + */ + const NS_SEPARATOR = '_'; + use ProxyTrait; private $miss; @@ -75,14 +80,4 @@ class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface { return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); } - - /** - * @return string the namespace separator for cache keys - * - * @internal - */ - protected static function getNsSeparator() - { - return '_'; - } } diff --git a/src/Symfony/Component/Cache/Simple/AbstractCache.php b/src/Symfony/Component/Cache/Simple/AbstractCache.php index 0d715e48d2..23b401c54b 100644 --- a/src/Symfony/Component/Cache/Simple/AbstractCache.php +++ b/src/Symfony/Component/Cache/Simple/AbstractCache.php @@ -23,6 +23,11 @@ use Symfony\Component\Cache\Traits\AbstractTrait; */ abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface { + /** + * @internal + */ + const NS_SEPARATOR = ':'; + use AbstractTrait { deleteItems as private; AbstractTrait::deleteItem as delete; diff --git a/src/Symfony/Component/Cache/Traits/AbstractTrait.php b/src/Symfony/Component/Cache/Traits/AbstractTrait.php index e225e07542..cd1f204139 100644 --- a/src/Symfony/Component/Cache/Traits/AbstractTrait.php +++ b/src/Symfony/Component/Cache/Traits/AbstractTrait.php @@ -106,9 +106,9 @@ trait AbstractTrait { $this->deferred = []; if ($cleared = $this->versioningIsEnabled) { - $namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::getNsSeparator(), 5); + $namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::NS_SEPARATOR, 5); try { - $cleared = $this->doSave([static::getNsSeparator().$this->namespace => $namespaceVersion], 0); + $cleared = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0); } catch (\Exception $e) { $cleared = false; } @@ -235,14 +235,14 @@ trait AbstractTrait CacheItem::validateKey($key); if ($this->versioningIsEnabled && '' === $this->namespaceVersion) { - $this->namespaceVersion = '1'.static::getNsSeparator(); + $this->namespaceVersion = '1'.static::NS_SEPARATOR; try { - foreach ($this->doFetch([static::getNsSeparator().$this->namespace]) as $v) { + foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) { $this->namespaceVersion = $v; } - if ('1'.static::getNsSeparator() === $this->namespaceVersion) { - $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5); - $this->doSave([static::getNsSeparator().$this->namespace => $this->namespaceVersion], 0); + if ('1'.static::NS_SEPARATOR === $this->namespaceVersion) { + $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::NS_SEPARATOR, 5); + $this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0); } } catch (\Exception $e) { } @@ -252,7 +252,7 @@ trait AbstractTrait return $this->namespace.$this->namespaceVersion.$key; } if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) { - $id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), static::getNsSeparator(), -(\strlen($this->namespaceVersion) + 22)); + $id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 22)); } return $id; @@ -265,14 +265,4 @@ trait AbstractTrait { throw new \DomainException('Class not found: '.$class); } - - /** - * @return string the namespace separator for cache keys - * - * @internal - */ - protected static function getNsSeparator() - { - return ':'; - } }