10000 [Cache] Allow to use namespace delimiter in cache key by dorrogeray · Pull Request #54710 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Allow to use namespace delimiter in cache key #54710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

< 10000 /header>
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Cache] Optimize CacheItem::validateKey - use override instead of allow
  • Loading branch information
dorrogeray committed Apr 27, 2024
commit 67e810938d2f9fa9ad4faad546bde3576da58b6c
10 changes: 9 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg
*/
protected const NS_SEPARATOR = ':';

/**
* @internal
*/
protected static ?string $reservedChars = null;

private static bool $apcuSupported;

protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).static::NS_SEPARATOR;
if (static::$reservedChars === null) {
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
}
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::$reservedChars).static::NS_SEPARATOR;
$this->defaultLifetime = $defaultLifetime;
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));
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA
*/
protected const NS_SEPARATOR = ':';

/**
* @internal
*/
protected static ?string $reservedChars = null;

protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).static::NS_SEPARATOR;
if (static::$reservedChars === null) {
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
}
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::$reservedChars).static::NS_SEPARATOR;
$this->defaultLifetime = $defaultLifetime;
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));
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
*/
protected const NS_SEPARATOR = ':';

private bool $storeSerialized;
/**
* @internal
*/
protected static ?string $reservedChars = null;

private array $values = [];
private array $tags = [];
private array $expiries = [];
Expand All @@ -51,6 +55,10 @@ public function __construct(
private float $maxLifetime = 0,
private int $maxItems = 0,
) {
if (static::$reservedChars === null) {
static::$reservedChars = str_replace(self::NS_SEPARATOR, '', CacheItem::RESERVED_CHARACTERS);
}

if (0 > $maxLifetime) {
throw new InvalidArgumentException(sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime));
}
Expand Down Expand Up @@ -110,7 +118,7 @@ public function hasItem(mixed $key): bool

return true;
}
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
\assert('' !== CacheItem::validateKey($key, static::$reservedChars));

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

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

return true;
Expand Down Expand Up @@ -356,7 +364,7 @@ private function validateKeys(array $keys): bool
{
foreach ($keys as $key) {
if (!\is_string($key) || !isset($this->expiries[$key])) {
CacheItem::validateKey($key, static::NS_SEPARATOR);
CacheItem::validateKey($key, static::$reservedChars);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ public function getMetadata(): array
* Validates a cache key according to PSR-6.
*
* @param mixed $key The key to validate
* @param string $reservedChars Can be used to override the list of reserved characters
*
* @throws InvalidArgumentException When $key is not valid
*/
public static function validateKey($key, string $allowChars = null): string
public static function validateKey($key, string $reservedChars = self::RESERVED_CHARACTERS): string
{
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', get_debug_type($key)));
}
if ('' === $key) {
throw new InvalidArgumentException('Cache key length must be greater than zero.');
}
$reservedChars = null === $allowChars ? self::RESERVED_CHARACTERS : str_replace(str_split($allowChars), '', self::RESERVED_CHARACTERS);
if ('' !== $reservedChars && false !== strpbrk($key, $reservedChars)) {
throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, $reservedChars));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ protected function getId(mixed $key): string
if (\is_string($key) && isset($this->ids[$key])) {
return $this->namespace.$this->namespaceVersion.$this->ids[$key];
}
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
\assert('' !== CacheItem::validateKey($key, static::$reservedChars));
$this->ids[$key] = $key;

if (\count($this->ids) > 1000) {
Expand Down
0