-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
vilartoni
wants to merge
5
commits into
doctrine:master
from
vilartoni:prevent-result-cache-key-collisions
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eda3820
Used connection params in cache key generation
vilartoni 536d192
Added tests for Cache\QueryCacheProfile
vilartoni dfbe772
Added new tests for QueryCacheProfile and Connection
vilartoni 73c8278
Some test tweaks
vilartoni d7f6b4c
Loaded bootstrap script in all phpunit travis configuration files
vilartoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure what the difference between this and line 44 is...