Skip to content

Commit 6b61439

Browse files
Merge branch '3.4' into 4.2
* 3.4: [Cache] replace getNsSeparator by NS_SEPARATOR on AbstractTrait [Cache] fix versioning with SimpleCacheAdapter
2 parents 06c17cd + 2bf5da5 commit 6b61439

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
*/
2828
abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2929
{
30+
/**
31+
* @internal
32+
*/
33+
protected const NS_SEPARATOR = ':';
34+
3035
use AbstractTrait;
3136
use ContractsTrait;
3237

@@ -38,7 +43,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
3843

3944
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
4045
{
41-
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::getNsSeparator();
46+
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR;
4247
if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
4348
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, \strlen($namespace), $namespace));
4449
}

src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface
2222
{
23+
/**
24+
* @internal
25+
*/
26+
protected const NS_SEPARATOR = '_';
27+
2328
use ProxyTrait;
2429

2530
private $miss;
@@ -75,12 +80,4 @@ protected function doSave(array $values, $lifetime)
7580
{
7681
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
7782
}
78-
79-
/**
80-
* @return string the namespace separator for cache keys
81-
*/
82-
protected static function getNsSeparator()
83-
{
84-
return '_';
85-
}
8683
}

src/Symfony/Component/Cache/Simple/AbstractCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
2525
{
26+
/**
27+
* @internal
28+
*/
29+
protected const NS_SEPARATOR = ':';
30+
2631
use AbstractTrait {
2732
deleteItems as private;
2833
AbstractTrait::deleteItem as delete;

src/Symfony/Component/Cache/Traits/AbstractTrait.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public function clear()
107107
{
108108
$this->deferred = [];
109109
if ($cleared = $this->versioningIsEnabled) {
110-
$namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::getNsSeparator(), 5);
110+
$namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::NS_SEPARATOR, 5);
111111
try {
112-
$cleared = $this->doSave([static::getNsSeparator().$this->namespace => $namespaceVersion], 0);
112+
$cleared = $this->doSave([static::NS_SEPARATOR.$this->namespace => $namespaceVersion], 0);
113113
} catch (\Exception $e) {
114114
$cleared = false;
115115
}
@@ -242,14 +242,14 @@ private function getId($key)
242242
{
243243
if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
244244
$this->ids = [];
245-
$this->namespaceVersion = '1'.static::getNsSeparator();
245+
$this->namespaceVersion = '1'.static::NS_SEPARATOR;
246246
try {
247-
foreach ($this->doFetch([static::getNsSeparator().$this->namespace]) as $v) {
247+
foreach ($this->doFetch([static::NS_SEPARATOR.$this->namespace]) as $v) {
248248
$this->namespaceVersion = $v;
249249
}
250-
if ('1'.static::getNsSeparator() === $this->namespaceVersion) {
251-
$this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5);
252-
$this->doSave([static::getNsSeparator().$this->namespace => $this->namespaceVersion], 0);
250+
if ('1'.static::NS_SEPARATOR === $this->namespaceVersion) {
251+
$this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::NS_SEPARATOR, 5);
252+
$this->doSave([static::NS_SEPARATOR.$this->namespace => $this->namespaceVersion], 0);
253253
}
254254
} catch (\Exception $e) {
255255
}
@@ -266,7 +266,7 @@ private function getId($key)
266266
}
267267
if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) {
268268
// Use MD5 to favor speed over security, which is not an issue here
269-
$this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::getNsSeparator(), -(\strlen($this->namespaceVersion) + 2));
269+
$this->ids[$key] = $id = substr_replace(base64_encode(hash('md5', $key, true)), static::NS_SEPARATOR, -(\strlen($this->namespaceVersion) + 2));
270270
$id = $this->namespace.$this->namespaceVersion.$id;
271271
}
272272

@@ -280,12 +280,4 @@ public static function handleUnserializeCallback($class)
280280
{
281281
throw new \DomainException('Class not found: '.$class);
282282
}
283-
284-
/**
285-
* @return string the namespace separator for cache keys
286-
*/
287-
protected static function getNsSeparator()
288-
{
289-
return ':';
290-
}
291283
}

0 commit comments

Comments
 (0)