Skip to content
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

SimpleCacheAdapter fails to cache any item if a namespace is used #32025

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Expand Up @@ -39,7 +39,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
*/
protected function __construct($namespace = '', $defaultLifetime = 0)
{
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':';
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::getNsSeparator();
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
8 changes: 8 additions & 0 deletions src/Symfony/Component/Cache/Adapter/SimpleCacheAdapter.php
Expand Up @@ -75,4 +75,12 @@ protected function doSave(array $values, $lifetime)
{
return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
}

/**
* @return string the namespace separator for cache keys
*/
protected static function getNsSeparator()
{
return '_';
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;

use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;
use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Cache\Simple\FilesystemCache;

/**
Expand All @@ -27,4 +28,14 @@ public function createCachePool($defaultLifetime = 0)
{
return new SimpleCacheAdapter(new FilesystemCache(), '', $defaultLifetime);
}

public function testValidCacheKeyWithNamespace()
{
$cache = new SimpleCacheAdapter(new ArrayCache(), 'some_namespace', 0);
$item = $cache->getItem('my_key');
$item->set('someValue');
$cache->save($item);

$this->assertTrue($cache->getItem('my_key')->isHit(), 'Stored item is successfully retrieved.');
}
}
18 changes: 13 additions & 5 deletions src/Symfony/Component/Cache/Traits/AbstractTrait.php
Expand Up @@ -106,7 +106,7 @@ public function clear()
{
$this->deferred = [];
if ($cleared = $this->versioningIsEnabled) {
$namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), ':', 5);
$namespaceVersion = substr_replace(base64_encode(pack('V', mt_rand())), static::getNsSeparator(), 5);
try {
$cleared = $this->doSave(['@'.$this->namespace => $namespaceVersion], 0);
} catch (\Exception $e) {
Expand Down Expand Up @@ -235,13 +235,13 @@ private function getId($key)
CacheItem::validateKey($key);

if ($this->versioningIsEnabled && '' === $this->namespaceVersion) {
$this->namespaceVersion = '1:';
$this->namespaceVersion = '1'.static::getNsSeparator();
try {
foreach ($this->doFetch(['@'.$this->namespace]) as $v) {
$this->namespaceVersion = $v;
}
if ('1:' === $this->namespaceVersion) {
$this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), ':', 5);
if ('1'.static::getNsSeparator() === $this->namespaceVersion) {
$this->namespaceVersion = substr_replace(base64_encode(pack('V', time())), static::getNsSeparator(), 5);
$this->doSave(['@'.$this->namespace => $this->namespaceVersion], 0);
}
} catch (\Exception $e) {
Expand All @@ -252,7 +252,7 @@ private function getId($key)
return $this->namespace.$this->namespaceVersion.$key;
}
if (\strlen($id = $this->namespace.$this->namespaceVersion.$key) > $this->maxIdLength) {
$id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), ':', -(\strlen($this->namespaceVersion) + 22));
$id = $this->namespace.$this->namespaceVersion.substr_replace(base64_encode(hash('sha256', $key, true)), static::getNsSeparator(), -(\strlen($this->namespaceVersion) + 22));
}

return $id;
Expand All @@ -265,4 +265,12 @@ public static function handleUnserializeCallback($class)
{
throw new \DomainException('Class not found: '.$class);
}

/**
* @return string the namespace separator for cache keys
*/
protected static function getNsSeparator()
{
return ':';
}
}