Skip to content

Commit 67e8109

Browse files
committed
[Cache] Optimize CacheItem::validateKey - use override instead of allow
1 parent 0933178 commit 67e8109

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,19 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
3333
*/
3434
protected const NS_SEPARATOR = ':';
3535

36+
/**
37+
* @internal
38+
*/
39+
protected static ?string $reservedChars = null;
40+
3641
private static bool $apcuSupported;
3742

3843
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
3944
{
40-
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).static::NS_SEPARATOR;
45+
if (static::$reservedChars === null) {
46+
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
47+
}
48+
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::$reservedChars).static::NS_SEPARATOR;
4149
$this->defaultLifetime = $defaultLifetime;
4250
if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
4351
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
4242
*/
4343
protected const NS_SEPARATOR = ':';
4444

45+
/**
46+
* @internal
47+
*/
48+
protected static ?string $reservedChars = null;
49+
4550
protected function __construct(string $namespace = '', int $defaultLifetime = 0)
4651
{
47-
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).static::NS_SEPARATOR;
52+
if (static::$reservedChars === null) {
53+
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
54+
}
55+
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::$reservedChars).static::NS_SEPARATOR;
4856
$this->defaultLifetime = $defaultLifetime;
4957
if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) {
5058
throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace));

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
3535
*/
3636
protected const NS_SEPARATOR = ':';
3737

38-
private bool $storeSerialized;
38+
/**
39+
* @internal
40+
*/
41+
protected static ?string $reservedChars = null;
42+
3943
private array $values = [];
4044
private array $tags = [];
4145
private array $expiries = [];
@@ -51,6 +55,10 @@ public function __construct(
5155
private float $maxLifetime = 0,
5256
private int $maxItems = 0,
5357
) {
58+
if (static::$reservedChars === null) {
59+
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
60+
}
61+
5462
if (0 > $maxLifetime) {
5563
throw new InvalidArgumentException(sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime));
5664
}
@@ -110,7 +118,7 @@ public function hasItem(mixed $key): bool
110118

111119
return true;
112120
}
113-
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
121+
\assert('' !== CacheItem::validateKey($key, static::$reservedChars));
114122

115123
return isset($this->expiries[$key]) && !$this->deleteItem($key);
116124
}
@@ -140,7 +148,7 @@ public function getItems(array $keys = []): iterable
140148

141149
public function deleteItem(mixed $key): bool
142150
{
143-
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
151+
\assert('' !== CacheItem::validateKey($key, static::$reservedChars));
144152
unset($this->values[$key], $this->tags[$key], $this->expiries[$key]);
145153

146154
return true;
@@ -356,7 +364,7 @@ private function validateKeys(array $keys): bool
356364
{
357365
foreach ($keys as $key) {
358366
if (!\is_string($key) || !isset($this->expiries[$key])) {
359-
CacheItem::validateKey($key, static::NS_SEPARATOR);
367+
CacheItem::validateKey($key, static::$reservedChars);
360368
}
361369
}
362370

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ public function getMetadata(): array
124124
* Validates a cache key according to PSR-6.
125125
*
126126
* @param mixed $key The key to validate
127+
* @param string $reservedChars Can be used to override the list of reserved characters
127128
*
128129
* @throws InvalidArgumentException When $key is not valid
129130
*/
130-
public static function validateKey($key, string $allowChars = null): string
131+
public static function validateKey($key, string $reservedChars = self::RESERVED_CHARACTERS): string
131132
{
132133
if (!\is_string($key)) {
133134
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
134135
}
135136
if ('' === $key) {
136137
throw new InvalidArgumentException('Cache key length must be greater than zero.');
137138
}
138-
$reservedChars = null === $allowChars ? self::RESERVED_CHARACTERS : str_replace(str_split($allowChars), '', self::RESERVED_CHARACTERS);
139139
if ('' !== $reservedChars && false !== strpbrk($key, $reservedChars)) {
140140
throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, $reservedChars));
141141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ protected function getId(mixed $key): string
343343
if (\is_string($key) && isset($this->ids[$key])) {
344344
return $this->namespace.$this->namespaceVersion.$this->ids[$key];
345345
}
346-
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
346+
\assert('' !== CacheItem::validateKey($key, static::$reservedChars));
347347
$this->ids[$key] = $key;
348348

349349
if (\count($this->ids) > 1000) {

0 commit comments

Comments
 (0)