Commit 120172a7 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-217 - Refactor result caching to use a value object for all the important...

DDC-217 - Refactor result caching to use a value object for all the important information which simplifies the code considerably and makes caching more powerful.
parent dbd2a100
<?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
<?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
...@@ -73,25 +73,20 @@ class ResultCacheStatement implements ResultStatement ...@@ -73,25 +73,20 @@ class ResultCacheStatement implements ResultStatement
/** /**
* @param Connection $conn * @param Connection $conn
* @param string $cacheKey
* @param int|null $lifetime
* @param string $query * @param string $query
* @param array $params * @param array $params
* @param array $types * @param array $types
* @param QueryCacheProfile $qcp
* @return RowCacheStatement * @return RowCacheStatement
*/ */
static public function create(Connection $conn, $query, $params, $types, $lifetime = 0, $cacheKey = null) static public function create(Connection $conn, $query, $params, $types, QueryCacheProfile $qcp)
{ {
$resultCache = $conn->getConfiguration()->getResultCacheImpl(); $resultCache = $qcp->getResultCacheDriver() ?: $conn->getConfiguration()->getResultCacheImpl();
if (!$resultCache) { if (!$resultCache) {
return $conn->executeQuery($query, $params, $types); throw CacheException::noResultDriverConfigured();
} }
$realKey = $query . "-" . serialize($params) . "-" . serialize($types); list($cacheKey, $realKey) = $qcp->generateCacheKeys($query, $params, $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 // fetch the row pointers entry
if ($data = $resultCache->fetch($cacheKey)) { if ($data = $resultCache->fetch($cacheKey)) {
...@@ -100,7 +95,7 @@ class ResultCacheStatement implements ResultStatement ...@@ -100,7 +95,7 @@ class ResultCacheStatement implements ResultStatement
return new ArrayStatement($data[$realKey]); return new ArrayStatement($data[$realKey]);
} }
} }
return new self($conn->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $lifetime); return new self($conn->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
} }
/** /**
......
...@@ -24,7 +24,8 @@ use PDO, Closure, Exception, ...@@ -24,7 +24,8 @@ use PDO, Closure, Exception,
Doctrine\DBAL\Driver\Connection as DriverConnection, Doctrine\DBAL\Driver\Connection as DriverConnection,
Doctrine\Common\EventManager, Doctrine\Common\EventManager,
Doctrine\DBAL\DBALException, Doctrine\DBAL\DBALException,
Doctrine\DBAL\Cache\ResultCacheStatement; Doctrine\DBAL\Cache\ResultCacheStatement,
Doctrine\DBAL\Cache\QueryCacheProfile;
/** /**
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like * A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
...@@ -595,16 +596,14 @@ class Connection implements DriverConnection ...@@ -595,16 +596,14 @@ class Connection implements DriverConnection
* @param string $query The SQL query to execute. * @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query, if any. * @param array $params The parameters to bind to the query, if any.
* @param array $types The types the previous parameters are in. * @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 QueryCacheProfile $qcp
* @param string|null $cacheResultKey name of the result cache key.
* @return Doctrine\DBAL\Driver\Statement The executed statement. * @return Doctrine\DBAL\Driver\Statement The executed statement.
* @internal PERF: Directly prepares a driver statement, not a wrapper. * @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) { if ($qcp !== null) {
$useCacheLifetime = $useCacheLifetime === true ? 0 : $useCacheLifetime; return ResultCacheStatement::create($this, $query, $params, $types, $qcp);
return ResultCacheStatement::create($this, $query, $params, $types, $useCacheLifetime, $cacheResultKey);
} }
$this->connect(); $this->connect();
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Functional; namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use PDO; use PDO;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -70,7 +71,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -70,7 +71,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
foreach ($this->expectedResult AS $v) { foreach ($this->expectedResult AS $v) {
$numExpectedResult[] = array_values($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(); $data = array();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
...@@ -80,7 +81,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -80,7 +81,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals($this->expectedResult, $data); $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(); $data = array();
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) { while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
...@@ -93,14 +94,14 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -93,14 +94,14 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testDontCloseNoCache() 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(); $data = array();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$data[] = $row; $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(); $data = array();
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) { while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
...@@ -112,12 +113,12 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -112,12 +113,12 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function testDontFinishNoCache() 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); $row = $stmt->fetch(\PDO::FETCH_ASSOC);
$stmt->closeCursor(); $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(); $data = array();
while ($row = $stmt->fetch(\PDO::FETCH_NUM)) { while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
...@@ -131,7 +132,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -131,7 +132,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchStyle) public function assertCacheNonCacheSelectSameFetchModeAreEqual($expectedResult, $fetchStyle)
{ {
$s = microtime(true); $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()); $this->assertEquals(2, $stmt->columnCount());
...@@ -145,7 +146,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -145,7 +146,7 @@ class ResultCacheTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertEquals($expectedResult, $data); $this->assertEquals($expectedResult, $data);
$s = microtime(true); $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()); $this->assertEquals(2, $stmt->columnCount());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment