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
230548ce
Commit
230548ce
authored
Oct 23, 2011
by
Benjamin Eberlei
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'DDC-217'
parents
caa8ac48
f56fef02
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
186 additions
and
50 deletions
+186
-50
CacheException.php
lib/Doctrine/DBAL/Cache/CacheException.php
+37
-0
QueryCacheProfile.php
lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
+100
-0
ResultCacheStatement.php
lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
+5
-35
Connection.php
lib/Doctrine/DBAL/Connection.php
+35
-7
ResultCacheTest.php
tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
+9
-8
No files found.
lib/Doctrine/DBAL/Cache/CacheException.php
0 → 100644
View file @
230548ce
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Cache
;
/**
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.2
*/
class
CacheException
extends
\Doctrine\DBAL\DBALException
{
static
public
function
noCacheKey
()
{
return
new
self
(
"No cache key was set."
);
}
static
public
function
noResultDriverConfigured
()
{
return
new
self
(
"Trying to cache a query but no result driver is configured."
);
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
0 → 100644
View file @
230548ce
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\DBAL\Cache
;
/**
* Query Cache Profile handles the data relevant for query caching.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class
QueryCacheProfile
{
/**
* @var Cache
*/
private
$resultCacheDriver
;
/**
* @var int
*/
private
$lifetime
=
0
;
/**
* @var string
*/
private
$cacheKey
;
/**
* @param int $lifetime
* @param string $cacheKey
* @param Cache $resultCache
*/
public
function
__construct
(
$lifetime
=
0
,
$cacheKey
=
null
,
Cache
$resultCache
=
null
)
{
$this
->
lifetime
=
$lifetime
;
$this
->
cacheKey
=
$cacheKey
;
$this
->
resultCacheDriver
=
$resultCache
;
}
/**
* @return Cache
*/
public
function
getResultCacheDriver
()
{
return
$this
->
resultCacheDriver
;
}
/**
* @return int
*/
public
function
getLifetime
()
{
return
$this
->
lifetime
;
}
/**
* @return string
*/
public
function
getCacheKey
()
{
if
(
$this
->
cacheKey
===
null
)
{
throw
CacheException
::
noCacheKey
();
}
return
$this
->
cacheKey
;
}
/**
* Generate the real cache key from query, params and types.
*
* @param string $query
* @param array $params
* @param array $types
* @return array
*/
public
function
generateCacheKeys
(
$query
,
$params
,
$types
)
{
$realCacheKey
=
$query
.
"-"
.
serialize
(
$params
)
.
"-"
.
serialize
(
$types
);
// should the key be automatically generated using the inputs or is the cache key set?
if
(
$this
->
cacheKey
===
null
)
{
$cacheKey
=
sha1
(
$realCacheKey
);
}
else
{
$cacheKey
=
$this
->
cacheKey
;
}
return
array
(
$cacheKey
,
$realCacheKey
);
}
}
\ No newline at end of file
lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
View file @
230548ce
...
...
@@ -19,9 +19,11 @@
namespace
Doctrine\DBAL\Cache
;
use
Doctrine\DBAL\Driver\Statement
;
use
Doctrine\DBAL\Driver\ResultStatement
;
use
PDO
;
use
Doctrine\DBAL\Connection
;
use
Doctrine\Common\Cache\Cache
;
use
PDO
;
/**
* Cache statement for SQL results.
...
...
@@ -72,45 +74,13 @@ class ResultCacheStatement implements ResultStatement
private
$emptied
=
false
;
/**
* @param Connection $conn
* @param string $cacheKey
* @param int|null $lifetime
* @param string $query
* @param array $params
* @param array $types
* @return RowCacheStatement
*/
static
public
function
create
(
Connection
$conn
,
$query
,
$params
,
$types
,
$lifetime
=
0
,
$cacheKey
=
null
)
{
$resultCache
=
$conn
->
getConfiguration
()
->
getResultCacheImpl
();
if
(
!
$resultCache
)
{
return
$conn
->
executeQuery
(
$query
,
$params
,
$types
);
}
$realKey
=
$query
.
"-"
.
serialize
(
$params
)
.
"-"
.
serialize
(
$types
);
// should the key be automatically generated using the inputs or is the cache key set?
if
(
$cacheKey
===
null
)
{
$cacheKey
=
sha1
(
$realKey
);
}
// fetch the row pointers entry
if
(
$data
=
$resultCache
->
fetch
(
$cacheKey
))
{
// is the real key part of this row pointers map or is the cache only pointing to other cache keys?
if
(
isset
(
$data
[
$realKey
]))
{
return
new
ArrayStatement
(
$data
[
$realKey
]);
}
}
return
new
self
(
$conn
->
executeQuery
(
$query
,
$params
,
$types
),
$resultCache
,
$cacheKey
,
$realKey
,
$lifetime
);
}
/**
*
* @param Statement $stmt
* @param Cache $resultCache
* @param string $cacheKey
* @param string $realKey
* @param int $lifetime
*/
p
rivate
function
__construct
(
$stmt
,
$resultCache
,
$cacheKey
,
$realKey
,
$lifetime
)
p
ublic
function
__construct
(
Statement
$stmt
,
Cache
$resultCache
,
$cacheKey
,
$realKey
,
$lifetime
)
{
$this
->
statement
=
$stmt
;
$this
->
resultCache
=
$resultCache
;
...
...
lib/Doctrine/DBAL/Connection.php
View file @
230548ce
...
...
@@ -24,7 +24,9 @@ use PDO, Closure, Exception,
Doctrine\DBAL\Driver\Connection
as
DriverConnection
,
Doctrine\Common\EventManager
,
Doctrine\DBAL\DBALException
,
Doctrine\DBAL\Cache\ResultCacheStatement
;
Doctrine\DBAL\Cache\ResultCacheStatement
,
Doctrine\DBAL\Cache\QueryCacheProfile
,
Doctrine\DBAL\Cache\ArrayStatement
;
/**
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
...
...
@@ -595,16 +597,14 @@ class Connection implements DriverConnection
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in.
* @param int $useCacheLifetime lifetime of the cache result, set to true for infinite lifetime.
* @param string|null $cacheResultKey name of the result cache key.
* @param QueryCacheProfile $qcp
* @return Doctrine\DBAL\Driver\Statement The executed statement.
* @internal PERF: Directly prepares a driver statement, not a wrapper.
*/
public
function
executeQuery
(
$query
,
array
$params
=
array
(),
$types
=
array
(),
$useCacheLifetime
=
false
,
$cacheResultKey
=
null
)
public
function
executeQuery
(
$query
,
array
$params
=
array
(),
$types
=
array
(),
QueryCacheProfile
$qcp
=
null
)
{
if
(
$useCacheLifetime
!==
false
)
{
$useCacheLifetime
=
$useCacheLifetime
===
true
?
0
:
$useCacheLifetime
;
return
ResultCacheStatement
::
create
(
$this
,
$query
,
$params
,
$types
,
$useCacheLifetime
,
$cacheResultKey
);
if
(
$qcp
!==
null
)
{
return
$this
->
executeCacheQuery
(
$query
,
$params
,
$types
,
$qcp
);
}
$this
->
connect
();
...
...
@@ -635,6 +635,34 @@ class Connection implements DriverConnection
return
$stmt
;
}
/**
* Execute a caching query and
*
* @param string $query
* @param array $params
* @param array $types
* @param QueryCacheProfile $qcp
* @return \Doctrine\DBAL\Driver\ResultStatement
*/
public
function
executeCacheQuery
(
$query
,
$params
,
$types
,
QueryCacheProfile
$qcp
)
{
$resultCache
=
$qcp
->
getResultCacheDriver
()
?:
$this
->
_config
->
getResultCacheImpl
();
if
(
!
$resultCache
)
{
throw
CacheException
::
noResultDriverConfigured
();
}
list
(
$cacheKey
,
$realKey
)
=
$qcp
->
generateCacheKeys
(
$query
,
$params
,
$types
);
// fetch the row pointers entry
if
(
$data
=
$resultCache
->
fetch
(
$cacheKey
))
{
// is the real key part of this row pointers map or is the cache only pointing to other cache keys?
if
(
isset
(
$data
[
$realKey
]))
{
return
new
ArrayStatement
(
$data
[
$realKey
]);
}
}
return
new
ResultCacheStatement
(
$this
->
executeQuery
(
$query
,
$params
,
$types
),
$resultCache
,
$cacheKey
,
$realKey
,
$qcp
->
getLifetime
());
}
/**
* Executes an, optionally parameterized, SQL query and returns the result,
* applying a given projection/transformation function on each row of the result.
...
...
tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
View file @
230548ce
...
...
@@ -2,6 +2,7 @@
namespace
Doctrine\Tests\DBAL\Functional
;
use
Doctrine\DBAL\Types\Type
;
use
Doctrine\DBAL\Cache\QueryCacheProfile
;
use
PDO
;
require_once
__DIR__
.
'/../../TestInit.php'
;
...
...
@@ -70,7 +71,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
foreach
(
$this
->
expectedResult
AS
$v
)
{
$numExpectedResult
[]
=
array_values
(
$v
);
}
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$data
=
array
();
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_ASSOC
))
{
...
...
@@ -80,7 +81,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertEquals
(
$this
->
expectedResult
,
$data
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$data
=
array
();
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_NUM
))
{
...
...
@@ -93,14 +94,14 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public
function
testDontCloseNoCache
()
{
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$data
=
array
();
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_ASSOC
))
{
$data
[]
=
$row
;
}
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$data
=
array
();
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_NUM
))
{
...
...
@@ -112,12 +113,12 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public
function
testDontFinishNoCache
()
{
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_ASSOC
);
$stmt
->
closeCursor
();
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$data
=
array
();
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_NUM
))
{
...
...
@@ -131,7 +132,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public
function
assertCacheNonCacheSelectSameFetchModeAreEqual
(
$expectedResult
,
$fetchStyle
)
{
$s
=
microtime
(
true
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$this
->
assertEquals
(
2
,
$stmt
->
columnCount
());
...
...
@@ -145,7 +146,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertEquals
(
$expectedResult
,
$data
);
$s
=
microtime
(
true
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
10
,
"testcachekey"
);
$stmt
=
$this
->
_conn
->
executeQuery
(
"SELECT * FROM caching"
,
array
(),
array
(),
new
QueryCacheProfile
(
10
,
"testcachekey"
)
);
$this
->
assertEquals
(
2
,
$stmt
->
columnCount
());
...
...
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