[Cache] replace getNsSeparator by NS_SEPARATOR on AbstractTrait

This commit is contained in:
Nicolas Grekas 2019-06-17 19:26:15 +02:00
parent 02a6f248b5
commit 2bf5da51da
4 changed files with 24 additions and 29 deletions

View File

@ -25,6 +25,11 @@ use Symfony\Component\Cache\Traits\AbstractTrait;
*/ */
abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
{ {
/**
* @internal
*/
const NS_SEPARATOR = ':';
use AbstractTrait; use AbstractTrait;
private static $apcuSupported; private static $apcuSupported;
@ -39,7 +44,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
*/ */
protected function __construct($namespace = '', $defaultLifetime = 0) 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) { 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)); throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, \strlen($namespace), $namespace));
} }

View File

@ -20,6 +20,11 @@ use Symfony\Component\Cache\Traits\ProxyTrait;
*/ */
class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface
{ {
/**
* @internal
*/
const NS_SEPARATOR = '_';
use ProxyTrait; use ProxyTrait;
private $miss; private $miss;
@ -75,14 +80,4 @@ class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface
{ {
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
} }
/**
* @return string the namespace separator for cache keys
*
* @internal
*/
protected static function getNsSeparator()
{
return '_';
}
} }

View File

@ -23,6 +23,11 @@ use Symfony\Component\Cache\Traits\AbstractTrait;
*/ */
abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
{ {
/**
* @internal
*/
const NS_SEPARATOR = ':';
use AbstractTrait { use AbstractTrait {
deleteItems as private; deleteItems as private;
AbstractTrait::deleteItem as delete; AbstractTrait::deleteItem as delete;

View File

@ -106,9 +106,9 @@ trait AbstractTrait
{ {
$this->deferred = []; $this->deferred = [];
if ($cleared = $this->versioningIsEnabled) { 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 { try {
$cleared = $this->doSave([static::getNsSeparator().$this->namespace => $namespaceVersion], 0); $cleared = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0);
} catch (\Exception $e) { } catch (\Exception $e) {
$cleared = false; $cleared = false;
} }
@ -235,14 +235,14 @@ trait AbstractTrait
CacheItem::validateKey($key); CacheItem::validateKey($key);
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) { if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
$this->namespaceVersion = '1'.static::getNsSeparator(); $this->namespaceVersion = '1'.static::NS_SEPARATOR;
try { try {
foreach ($this->doFetch([static::getNsSeparator().$this->namespace]) as $v) { foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) {
$this->namespaceVersion = $v; $this->namespaceVersion = $v;
} }
if ('1'.static::getNsSeparator() === $this->namespaceVersion) { if ('1'.static::NS_SEPARATOR === $this->namespaceVersion) {
$this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5); $this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::NS_SEPARATOR, 5);
$this->doSave([static::getNsSeparator().$this->namespace => $this->namespaceVersion], 0); $this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
} }
@ -252,7 +252,7 @@ trait AbstractTrait
return $this->namespace.$this->namespaceVersion.$key; return $this->namespace.$this->namespaceVersion.$key;
} }
if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) { 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; return $id;
@ -265,14 +265,4 @@ trait AbstractTrait
{ {
throw new \DomainException('Class not found: '.$class); throw new \DomainException('Class not found: '.$class);
} }
/**
* @return string the namespace separator for cache keys
*
* @internal
*/
protected static function getNsSeparator()
{
return ':';
}
} }