-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Handle arbitrary key length when the backend cant using hashing #20037
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Cache\CacheItem; | ||
use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
|
@@ -32,9 +33,17 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface | |
private $createCacheItem; | ||
private $mergeByLifetime; | ||
|
||
/** | ||
* @var int|null The maximum length to enforce for identifiers or null when no limit applies | ||
*/ | ||
protected $maxIdLength; | ||
|
||
protected function __construct($namespace = '', $defaultLifetime = 0) | ||
{ | ||
$this->namespace = '' === $namespace ? '' : $this->getId($namespace); | ||
$this->namespace = '' === $namespace ? '' : $this->getId($namespace).':'; | ||
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)); | ||
} | ||
$this->createCacheItem = \Closure::bind( | ||
function ($key, $value, $isHit) use ($defaultLifetime) { | ||
$item = new CacheItem(); | ||
|
@@ -401,7 +410,14 @@ private function getId($key) | |
{ | ||
CacheItem::validateKey($key); | ||
|
||
return $this->namespace.$key; | ||
if (null === $this->maxIdLength) { | ||
return $this->namespace.$key; | ||
} | ||
if (strlen($id = $this->namespace.$key) > $this->maxIdLength) { | ||
$id = $this->namespace.substr_replace(base64_encode(md5($key, true)), ':', -2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Namespace excluded, this is always 23 chars long, trailing padding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The added trailing |
||
} | ||
|
||
return $id; | ||
} | ||
|
||
private function generateItems($items, &$keys) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,9 +37,9 @@ public function __construct($namespace = '', $defaultLifetime = 0, $version = nu | |
if (null !== $version) { | ||
CacheItem::validateKey($version); | ||
|
||
if (!apcu_exists($version.':'.$namespace)) { | ||
if (!apcu_exists($version.'@'.$namespace)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's all about the looks ;) |
||
$this->clear($namespace); | ||
apcu_add($version.':'.$namespace, null); | ||
apcu_add($version.'@'.$namespace, null); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests\Adapter; | ||
|
||
use Symfony\Component\Cache\Adapter\AbstractAdapter; | ||
|
||
class MaxIdLengthAdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testLongKey() | ||
{ | ||
$cache = $this->getMockBuilder(MaxIdLengthAdapter::class) | ||
->setConstructorArgs(array(str_repeat('-', 10))) | ||
->setMethods(array('doHave', 'doFetch', 'doDelete', 'doSave', 'doClear')) | ||
->getMock(); | ||
|
||
$cache->expects($this->exactly(2)) | ||
->method('doHave') | ||
->withConsecutive( | ||
array($this->equalTo('----------:nWfzGiCgLczv3SSUzXL3kg:')), | ||
array($this->equalTo('----------:---------------------------------------')) | ||
); | ||
|
||
$cache->hasItem(str_repeat('-', 40)); | ||
$cache->hasItem(str_repeat('-', 39)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage Namespace must be 26 chars max, 40 given ("----------------------------------------") | ||
*/ | ||
public function testTooLongNamespace() | ||
{ | ||
$cache = $this->getMockBuilder(MaxIdLengthAdapter::class) | ||
->setConstructorArgs(array(str_repeat('-', 40))) | ||
->getMock(); | ||
} | ||
} | ||
|
||
abstract class MaxIdLengthAdapter extends AbstractAdapter | ||
{ | ||
protected $maxIdLength = 50; | ||
|
||
public function __construct($ns) | ||
{ | ||
parent::__construct($ns); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:
is a reserved char per psr6 so we use it internally to be free from any collision when concatenating keys and namespace later on