Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
017991a0
Unverified
Commit
017991a0
authored
Feb 22, 2018
by
Full
Committed by
Sergei Morozov
Mar 28, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Connection parameters are cached hashed
parent
16937314
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
88 deletions
+89
-88
QueryCacheProfile.php
lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
+13
-14
QueryCacheProfileTest.php
tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
+76
-74
No files found.
lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
View file @
017991a0
...
@@ -20,18 +20,19 @@
...
@@ -20,18 +20,19 @@
namespace
Doctrine\DBAL\Cache
;
namespace
Doctrine\DBAL\Cache
;
use
Doctrine\Common\Cache\Cache
;
use
Doctrine\Common\Cache\Cache
;
use
function
hash
;
use
function
serialize
;
use
function
sha1
;
/**
/**
* Query Cache Profile handles the data relevant for query caching.
* Query Cache Profile handles the data relevant for query caching.
*
*
* It is a value object, setter methods return NEW instances.
* It is a value object, setter methods return NEW instances.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
*/
class
QueryCacheProfile
class
QueryCacheProfile
{
{
/**
/**
* @var
\Doctrine\Common\Cache\
Cache|null
* @var Cache|null
*/
*/
private
$resultCacheDriver
;
private
$resultCacheDriver
;
...
@@ -48,9 +49,8 @@ class QueryCacheProfile
...
@@ -48,9 +49,8 @@ class QueryCacheProfile
/**
/**
* @param int $lifetime
* @param int $lifetime
* @param string|null $cacheKey
* @param string|null $cacheKey
* @param \Doctrine\Common\Cache\Cache|null $resultCache
*/
*/
public
function
__construct
(
$lifetime
=
0
,
$cacheKey
=
null
,
Cache
$resultCache
=
null
)
public
function
__construct
(
$lifetime
=
0
,
$cacheKey
=
null
,
?
Cache
$resultCache
=
null
)
{
{
$this
->
lifetime
=
$lifetime
;
$this
->
lifetime
=
$lifetime
;
$this
->
cacheKey
=
$cacheKey
;
$this
->
cacheKey
=
$cacheKey
;
...
@@ -58,7 +58,7 @@ class QueryCacheProfile
...
@@ -58,7 +58,7 @@ class QueryCacheProfile
}
}
/**
/**
* @return
\Doctrine\Common\Cache\
Cache|null
* @return Cache|null
*/
*/
public
function
getResultCacheDriver
()
public
function
getResultCacheDriver
()
{
{
...
@@ -76,7 +76,7 @@ class QueryCacheProfile
...
@@ -76,7 +76,7 @@ class QueryCacheProfile
/**
/**
* @return string
* @return string
*
*
* @throws
\Doctrine\DBAL\Cache\
CacheException
* @throws CacheException
*/
*/
public
function
getCacheKey
()
public
function
getCacheKey
()
{
{
...
@@ -102,7 +102,7 @@ class QueryCacheProfile
...
@@ -102,7 +102,7 @@ class QueryCacheProfile
$realCacheKey
=
'query='
.
$query
.
$realCacheKey
=
'query='
.
$query
.
'¶ms='
.
serialize
(
$params
)
.
'¶ms='
.
serialize
(
$params
)
.
'&types='
.
serialize
(
$types
)
.
'&types='
.
serialize
(
$types
)
.
'&connectionParams='
.
serialize
(
$connectionParams
);
'&connectionParams='
.
hash
(
'sha256'
,
serialize
(
$connectionParams
)
);
// should the key be automatically generated using the inputs or is the cache key set?
// should the key be automatically generated using the inputs or is the cache key set?
if
(
$this
->
cacheKey
===
null
)
{
if
(
$this
->
cacheKey
===
null
)
{
...
@@ -111,11 +111,10 @@ class QueryCacheProfile
...
@@ -111,11 +111,10 @@ class QueryCacheProfile
$cacheKey
=
$this
->
cacheKey
;
$cacheKey
=
$this
->
cacheKey
;
}
}
return
array
(
$cacheKey
,
$realCacheKey
)
;
return
[
$cacheKey
,
$realCacheKey
]
;
}
}
/**
/**
* @param \Doctrine\Common\Cache\Cache $cache
*
*
* @return \Doctrine\DBAL\Cache\QueryCacheProfile
* @return \Doctrine\DBAL\Cache\QueryCacheProfile
*/
*/
...
...
tests/Doctrine/Tests/DBAL/Cache/QueryCacheProfileTest.php
View file @
017991a0
...
@@ -5,15 +5,44 @@ namespace Doctrine\Tests\DBAL\Cache;
...
@@ -5,15 +5,44 @@ namespace Doctrine\Tests\DBAL\Cache;
use
Doctrine\DBAL\Cache\QueryCacheProfile
;
use
Doctrine\DBAL\Cache\QueryCacheProfile
;
use
Doctrine\DBAL\ParameterType
;
use
Doctrine\DBAL\ParameterType
;
use
Doctrine\Tests\DbalTestCase
;
use
Doctrine\Tests\DbalTestCase
;
use
function
parse_str
;
class
QueryCacheProfileTest
extends
DbalTestCase
class
QueryCacheProfileTest
extends
DbalTestCase
{
{
const
LIFETIME
=
3600
;
private
const
LIFETIME
=
3600
;
const
CACHE_KEY
=
'user_specified_cache_key'
;
private
const
CACHE_KEY
=
'user_specified_cache_key'
;
/** @var QueryCacheProfile */
/**
* @var QueryCacheProfile
*/
private
$queryCacheProfile
;
private
$queryCacheProfile
;
/**
* @var string
*/
private
$query
=
'SELECT * FROM foo WHERE bar = ?'
;
/**
* @var int[]
*/
private
$params
=
[
666
];
/**
* @var string[]
*/
private
$types
=
[
ParameterType
::
INTEGER
];
/**
* @var string[]
*/
private
$connectionParams
=
[
'dbname'
=>
'database_name'
,
'user'
=>
'database_user'
,
'password'
=>
'database_password'
,
'host'
=>
'database_host'
,
'driver'
=>
'database_driver'
,
];
protected
function
setUp
()
protected
function
setUp
()
{
{
$this
->
queryCacheProfile
=
new
QueryCacheProfile
(
self
::
LIFETIME
,
self
::
CACHE_KEY
);
$this
->
queryCacheProfile
=
new
QueryCacheProfile
(
self
::
LIFETIME
,
self
::
CACHE_KEY
);
...
@@ -21,23 +50,11 @@ class QueryCacheProfileTest extends DbalTestCase
...
@@ -21,23 +50,11 @@ class QueryCacheProfileTest extends DbalTestCase
public
function
testShouldUseTheGivenCacheKeyIfPresent
()
public
function
testShouldUseTheGivenCacheKeyIfPresent
()
{
{
$query
=
'SELECT * FROM foo WHERE bar = ?'
;
$params
=
[
666
];
$types
=
[
ParameterType
::
INTEGER
];
$connectionParams
=
array
(
'dbname'
=>
'database_name'
,
'user'
=>
'database_user'
,
'password'
=>
'database_password'
,
'host'
=>
'database_host'
,
'driver'
=>
'database_driver'
);
list
(
$cacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$cacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
self
::
assertEquals
(
self
::
CACHE_KEY
,
$cacheKey
,
'The returned cache key should match the given one'
);
self
::
assertEquals
(
self
::
CACHE_KEY
,
$cacheKey
,
'The returned cache key should match the given one'
);
...
@@ -45,25 +62,13 @@ class QueryCacheProfileTest extends DbalTestCase
...
@@ -45,25 +62,13 @@ class QueryCacheProfileTest extends DbalTestCase
public
function
testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven
()
public
function
testShouldGenerateAnAutomaticKeyIfNoKeyHasBeenGiven
()
{
{
$query
=
'SELECT * FROM foo WHERE bar = ?'
;
$params
=
[
666
];
$types
=
[
ParameterType
::
INTEGER
];
$connectionParams
=
array
(
'dbname'
=>
'database_name'
,
'user'
=>
'database_user'
,
'password'
=>
'database_password'
,
'host'
=>
'database_host'
,
'driver'
=>
'database_driver'
);
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
list
(
$cacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$cacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
self
::
assertNotEquals
(
self
::
assertNotEquals
(
...
@@ -77,67 +82,64 @@ class QueryCacheProfileTest extends DbalTestCase
...
@@ -77,67 +82,64 @@ class QueryCacheProfileTest extends DbalTestCase
public
function
testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections
()
public
function
testShouldGenerateDifferentKeysForSameQueryAndParamsAndDifferentConnections
()
{
{
$query
=
'SELECT * FROM foo WHERE bar = ?'
;
$params
=
[
666
];
$types
=
[
ParameterType
::
INTEGER
];
$connectionParams
=
array
(
'dbname'
=>
'database_name'
,
'user'
=>
'database_user'
,
'password'
=>
'database_password'
,
'host'
=>
'database_host'
,
'driver'
=>
'database_driver'
);
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
list
(
$firstCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$firstCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
$connectionParams
[
'host'
]
=
'a_different_host'
;
$
this
->
connectionParams
[
'host'
]
=
'a_different_host'
;
list
(
$secondCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$secondCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
self
::
assertNotEquals
(
$firstCacheKey
,
$secondCacheKey
,
'Cache keys should be different'
);
self
::
assertNotEquals
(
$firstCacheKey
,
$secondCacheKey
,
'Cache keys should be different'
);
}
}
public
function
test
ShouldGenerateSameKeysIfNoneOfTheParamsChanges
()
public
function
test
ConnectionParamsShouldBeHashed
()
{
{
$query
=
'SELECT * FROM foo WHERE bar = ?'
;
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
$params
=
[
666
];
$types
=
[
ParameterType
::
INTEGER
];
$connectionParams
=
array
(
list
(
$cacheKey
,
$queryString
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
'dbname'
=>
'database_name'
,
$this
->
query
,
'user'
=>
'database_user'
,
$this
->
params
,
'password'
=>
'database_password'
,
$this
->
types
,
'host'
=>
'database_host'
,
$this
->
connectionParams
'driver'
=>
'database_driver'
);
);
$params
=
[];
parse_str
(
$queryString
,
$params
);
self
::
assertArrayHasKey
(
'connectionParams'
,
$params
);
foreach
(
$this
->
connectionParams
as
$param
)
{
self
::
assertNotContains
(
$param
,
$params
[
'connectionParams'
]);
}
}
public
function
testShouldGenerateSameKeysIfNoneOfTheParamsChanges
()
{
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
$this
->
queryCacheProfile
=
$this
->
queryCacheProfile
->
setCacheKey
(
null
);
list
(
$firstCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$firstCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
list
(
$secondCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
list
(
$secondCacheKey
)
=
$this
->
queryCacheProfile
->
generateCacheKeys
(
$query
,
$
this
->
query
,
$params
,
$
this
->
params
,
$types
,
$t
his
->
t
ypes
,
$connectionParams
$
this
->
connectionParams
);
);
self
::
assertEquals
(
$firstCacheKey
,
$secondCacheKey
,
'Cache keys should be the same'
);
self
::
assertEquals
(
$firstCacheKey
,
$secondCacheKey
,
'Cache keys should be the same'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment