[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
{
/**
* @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));
}

View File

@ -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 '_';
}
}

View File

@ -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;

View File

@ -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 ':';
}
}