Skip to content

[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

Merged
merged 1 commit into from
Sep 24, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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).':';
Copy link
Member Author

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

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();
Expand Down Expand Up @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

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

Namespace excluded, this is always 23 chars long, trailing padding = removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

The added trailing : is to prevent any collision between hashed keys and non-hasked keys.

}

return $id;
}

private function generateItems($items, &$keys)
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Just because : is a nice separator for namespace, thus had to change here. @ looks good also for version :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}
}
Expand Down
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);
}
}