Skip to content

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

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractAdapter implements AdapterInterface, CacheInterface, Logg

protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR;
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).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
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ abstract class AbstractTagAwareAdapter implements TagAwareAdapterInterface, TagA

private const TAGS_PREFIX = "\1tags\1";

/**
* @internal
*/
protected const NS_SEPARATOR = ':';

protected function __construct(string $namespace = '', int $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace, static::NS_SEPARATOR).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
11 changes: 8 additions & 3 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
{
use LoggerAwareTrait;

/**
* @internal
*/
protected const NS_SEPARATOR = ':';

private bool $storeSerialized;
private array $values = [];
private array $tags = [];
Expand Down Expand Up @@ -108,7 +113,7 @@ public function hasItem(mixed $key): bool

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

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

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

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

Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,17 @@ public function getMetadata(): array
*
* @throws InvalidArgumentException When $key is not valid
*/
public static function validateKey($key): string
public static function validateKey($key, string $allowChars = null): 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.');
}
if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) {
throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, self::RESERVED_CHARACTERS));
$reservedChars = null === $allowChars ? self::RESERVED_CHARACTERS : str_replace(str_split($allowChars), '', self::RESERVED_CHARACTERS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to get rid of the calls to str_replace and str_split? this is on the hotpath and we'd better make this as fast as possible

if ('' !== $reservedChars && false !== strpbrk($key, $reservedChars)) {
throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters "%s".', $key, $reservedChars));
}

return $key;
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

abstract class AdapterTestCase extends CachePoolTest
{
protected static ?string $allowPsr6Keys = ':';

protected function setUp(): void
{
parent::setUp();
Expand All @@ -40,6 +42,16 @@ protected function setUp(): void
}
}

public static function invalidKeys(): array
{
$keys = parent::invalidKeys();
if (null !== static::$allowPsr6Keys) {
$keys = array_filter($keys, fn ($key) => !\is_string($key[0] ?? null) || false === strpbrk($key[0], static::$allowPsr6Keys));
}

return $keys;
}

public function testGet()
{
if (isset($this->skippedTests[__FUNCTION__])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class Psr16AdapterTest extends AdapterTestCase
{
protected static ?string $allowPsr6Keys = null;

protected $skippedTests = [
'testPrune' => 'Psr16adapter just proxies',
'testClearPrefix' => 'SimpleCache cannot clear by prefix',
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Cache/Tests/Psr16CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function setUp(): void

public function createSimpleCache(int $defaultLifetime = 0): CacheInterface
{
return new Psr16Cache(new FilesystemAdapter('', $defaultLifetime));
return new Psr16Cache(new FilesystemTestAdapter('', $defaultLifetime));
}

public static function validKeys(): array
Expand Down Expand Up @@ -181,3 +181,8 @@ public function __wakeup()
throw new \Exception(__CLASS__);
}
}

class FilesystemTestAdapter extends FilesystemAdapter
{
protected const NS_SEPARATOR = '_';
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,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));
\assert('' !== CacheItem::validateKey($key, static::NS_SEPARATOR));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is used for PSR-6 also, isn't it?

$this->ids[$key] = $key;

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