Merge branch '3.4' into 4.2

* 3.4:
  [Cache] replace getNsSeparator by NS_SEPARATOR on AbstractTrait
  [Cache] fix versioning with SimpleCacheAdapter
This commit is contained in:
Nicolas Grekas 2019-06-17 19:31:01 +02:00
commit 6b61439fb9
4 changed files with 24 additions and 25 deletions

View File

@ -27,6 +27,11 @@ use Symfony\Contracts\Cache\CacheInterface;
*/
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
{
/**
* @internal
*/
protected const NS_SEPARATOR = ':';
use AbstractTrait;
use ContractsTrait;
@ -38,7 +43,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
protected function __construct(string $namespace = '', int $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
*/
protected const NS_SEPARATOR = '_';
use ProxyTrait;
private $miss;
@ -75,12 +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
*/
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
*/
protected const NS_SEPARATOR = ':';
use AbstractTrait {
deleteItems as private;
AbstractTrait::deleteItem as delete;

View File

@ -107,9 +107,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;
}
@ -242,14 +242,14 @@ trait AbstractTrait
{
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
$this->ids = [];
$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) {
}
@ -266,7 +266,7 @@ trait AbstractTrait
}
if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) {
// Use MD5 to favor speed over security, which is not an issue here
$this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::getNsSeparator(), -(\strlen($this->namespaceVersion) + 2));
$this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 2));
$id = $this->namespace.$this->namespaceVersion.$id;
}
@ -280,12 +280,4 @@ trait AbstractTrait
{
throw new \DomainException('Class not found: '.$class);
}
/**
* @return string the namespace separator for cache keys
*/
protected static function getNsSeparator()
{
return ':';
}
}