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

Prevent result cache key collisions when sharing caches across different connections #713

Closed
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
11 changes: 8 additions & 3 deletions lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,22 @@ public function getCacheKey()
}

/**
* Generates the real cache key from query, params and types.
* Generates the real cache key from query, params, types and connection parameters.
*
* @param string $query
* @param array $params
* @param array $types
* @param array $connectionParams
*
* @return array
*/
public function generateCacheKeys($query, $params, $types)
public function generateCacheKeys($query, $params, $types, array $connectionParams = array())
{
$realCacheKey = $query . "-" . serialize($params) . "-" . serialize($types);
$realCacheKey = 'query=' . $query .
'&params=' . serialize($params) .
'&types=' . serialize($types) .
(!empty($connectionParams) ? serialize($connectionParams) : '');

// should the key be automatically generated using the inputs or is the cache key set?
if ($this->cacheKey === null) {
$cacheKey = sha1($realCacheKey);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc
throw CacheException::noResultDriverConfigured();
}

list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types);
list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $types, $this->getParams());

// fetch the row pointers entry
if ($data = $resultCache->fetch($cacheKey)) {
Expand Down
152 changes: 152 additions & 0 deletions tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Doctrine\Tests\DBAL\Cache;

use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\Tests\DbalTestCase;

class QueryCacheProfileTest extends DbalTestCase
{
const LIFETIME = 3600;
const CACHE_KEY = 'user_specified_cache_key';

/** @var QueryCacheProfile */
private $queryCacheProfile;

protected function setUp()
{
$this->queryCacheProfile = new QueryCacheProfile(self::LIFETIME, self::CACHE_KEY);
}

public function testShouldUseTheGivenCacheKeyIfPresent()
{
$query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666);
$types = array(\PDO::PARAM_INT);

$connectionParams = array(
'dbname' => 'database_name',
'user' => 'database_user',
'password' => 'database_password',
'host' => 'database_host',
'driver' => 'database_driver'
);

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$this->assertEquals(self::CACHE_KEY, $generatedKeys[0], 'The returned cache key should match the given one');
}

public function testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven()
{
$query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666);
$types = array(\PDO::PARAM_INT);

$connectionParams = array(
'dbname' => 'database_name',
'user' => 'database_user',
'password' => 'database_password',
'host' => 'database_host',
'driver' => 'database_driver'
);

$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$this->assertNotEquals(
self::CACHE_KEY,
$generatedKeys[0],
'The returned cache key should be generated automatically'
);

$this->assertNotEmpty($generatedKeys[0], 'The generated cache key should not be empty');
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what the difference between this and line 44 is...

}

public function testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections()
{
$query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666);
$types = array(\PDO::PARAM_INT);

$connectionParams = array(
'dbname' => 'database_name',
'user' => 'database_user',
'password' => 'database_password',
'host' => 'database_host',
'driver' => 'database_driver'
);

$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$firstCacheKey = $generatedKeys[0];

$connectionParams['host'] = 'a_different_host';

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$secondCacheKey = $generatedKeys[0];

$this->assertNotEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be different');
}

public function testShouldGenerateSameKeysIfNoneOfTheParamsChanges()
{
$query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666);
$types = array(\PDO::PARAM_INT);

$connectionParams = array(
'dbname' => 'database_name',
'user' => 'database_user',
'password' => 'database_password',
'host' => 'database_host',
'driver' => 'database_driver'
);

$this->queryCacheProfile = $this->queryCacheProfile->setCacheKey(null);

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$firstCacheKey = $generatedKeys[0];

$generatedKeys = $this->queryCacheProfile->generateCacheKeys(
$query,
$params,
$types,
$connectionParams
);

$secondCacheKey = $generatedKeys[0];

$this->assertEquals($firstCacheKey, $secondCacheKey, 'Cache keys should be the same');
}
}
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/DBAL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,39 @@ public function testCallingDeleteWithNoDeletionCriteriaResultsInSqlWithoutWhereC

$conn->delete('kittens', array());
}

public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery()
{
$resultCacheDriverMock = $this->getMock('Doctrine\Common\Cache\Cache');

$resultCacheDriverMock->expects($this->atLeastOnce())
->method('fetch')
->with('cacheKey')
->will($this->returnValue(array('realKey' => array())));

$query = 'SELECT * FROM foo WHERE bar = ?';
$params = array(666);
$types = array(\PDO::PARAM_INT);

$queryCacheProfileMock = $this->getMock('Doctrine\DBAL\Cache\QueryCacheProfile');

$queryCacheProfileMock->expects($this->any())
->method('getResultCacheDriver')
->will($this->returnValue($resultCacheDriverMock));

// This is our main expectation
$queryCacheProfileMock->expects($this->once())
->method('generateCacheKeys')
->with($query, $params, $types, $this->params)
->will($this->returnValue(array('cacheKey', 'realKey')));

$conn = new Connection(
$this->params,
$this->getMock('Doctrine\DBAL\Driver')
);

$this->assertInstanceOf('Doctrine\DBAL\Cache\ArrayStatement',
$conn->executeCacheQuery($query, $params, $types, $queryCacheProfileMock)
);
}
}
2 changes: 1 addition & 1 deletion tests/travis/mysql.travis.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit>
<phpunit bootstrap="../Doctrine/Tests/TestInit.php">
<php>
<var name="db_type" value="pdo_mysql"/>
<var name="db_host" value="localhost" />
Expand Down
2 changes: 1 addition & 1 deletion tests/travis/mysqli.travis.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit>
<phpunit bootstrap="../Doctrine/Tests/TestInit.php">
<php>
<var name="db_type" value="mysqli"/>
<var name="db_host" value="localhost" />
Expand Down
4 changes: 2 additions & 2 deletions tests/travis/pgsql.travis.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit>
<phpunit bootstrap="../Doctrine/Tests/TestInit.php">
<php>
<var name="db_type" value="pdo_pgsql"/>
<var name="db_host" value="localhost" />
Expand All @@ -25,4 +25,4 @@
<group>locking_functional</group>
</exclude>
</groups>
</phpunit>
</phpunit>
4 changes: 2 additions & 2 deletions tests/travis/sqlite.travis.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit>
<phpunit bootstrap="../Doctrine/Tests/TestInit.php">
<testsuites>
<testsuite name="Doctrine DBAL Test Suite">
<directory>./../Doctrine/Tests/DBAL</directory>
Expand All @@ -11,4 +11,4 @@
<group>locking_functional</group>
</exclude>
</groups>
</phpunit>
</phpunit>