From 0624cbd86757ce77080df158e9ea60e0837938c6 Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 29 Jun 2007 10:18:05 +0000 Subject: [PATCH] --- lib/Doctrine.php | 4 +- lib/Doctrine/Configurable.php | 23 +++++++---- lib/Doctrine/Hydrate.php | 72 ++++++++++++++++++++++++++++++----- lib/Doctrine/Manager.php | 4 +- tests/Query/CacheTestCase.php | 50 +++++++++++++++--------- tests/run.php | 15 +++++--- 6 files changed, 123 insertions(+), 45 deletions(-) diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 869d137d7..3f7116357 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -190,9 +190,7 @@ final class Doctrine const ATTR_NS_GAP_SIZE = 131; const ATTR_NS_GAP_DECREASE_EXP = 132; - const ATTR_DQL_PARSER_CACHE = 150; - const ATTR_DQL_CACHE = 151; - const ATTR_SQL_CACHE = 152; + const ATTR_CACHE = 150; const ATTR_LOAD_REFERENCES = 153; /** diff --git a/lib/Doctrine/Configurable.php b/lib/Doctrine/Configurable.php index d5237bf8e..9b63a44d9 100644 --- a/lib/Doctrine/Configurable.php +++ b/lib/Doctrine/Configurable.php @@ -110,13 +110,11 @@ abstract class Doctrine_Configurable throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'"); } break; - case Doctrine::ATTR_DQL_CACHE: - case Doctrine::ATTR_DQL_PARSER_CACHE: - case Doctrine::ATTR_SQL_CACHE: + case Doctrine::ATTR_CACHE: if ($value !== null) { if ( ! ($value instanceof Doctrine_Cache_Interface)) { throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface'); - } + } } break; case Doctrine::ATTR_VLD: @@ -158,6 +156,19 @@ abstract class Doctrine_Configurable $this->attributes[$attribute] = $value; } + /** + * getCacheDriver + * + * @return Doctrine_Cache_Interface + */ + public function getCacheDriver() + { + if ( ! isset($this->attributes[Doctrine::ATTR_CACHE])) { + throw new Doctrine_Exception('Cache driver not initialized.'); + } + + return $this->attributes[Doctrine::ATTR_CACHE]; + } /** * @param Doctrine_EventListener $listener * @return void @@ -204,9 +215,7 @@ abstract class Doctrine_Configurable * setListener * * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener - * @return Doctrine_Connection_Informix|Doctrine_Connection_Mssql|Doctrine_Connection_Oracle| - * Doctrine_Connection_Db2|Doctrine_Connection_Firebird|Doctrine_Connection_Common| - * Doctrine_Manager|Doctrine_Connection|Doctrine_Table + * @return Doctrine_Configurable this object */ public function setListener($listener) { diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php index 8f9c689ed..7bf8f4138 100644 --- a/lib/Doctrine/Hydrate.php +++ b/lib/Doctrine/Hydrate.php @@ -129,7 +129,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable * @see Doctrine_Query::* constants */ protected $type = self::SELECT; - + /** + * @var array + */ protected $_cache; protected $_tableAliases = array(); @@ -177,13 +179,56 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable { return $this->getQuery(); } - public function setCache(Doctrine_Cache_Interface $cache) + /** + * useCache + * + * @param Doctrine_Cache_Interface|bool $driver cache driver + * @param integer $timeToLive how long the cache entry is valid + * @return Doctrine_Hydrate this object + */ + public function useCache($driver = true, $timeToLive = null) { - $this->_cache = $cache; + if ($driver !== null) { + if ($driver !== true) { + if ( ! ($driver instanceof Doctrine_Cache_Interface)) { + $msg = 'First argument should be instance of Doctrine_Cache_Interface or null.'; + + throw new Doctrine_Hydrate_Exception($msg); + } + } + } + $this->_cache = $driver; + + return $this->setTimeToLive($timeToLive); } - public function getCache() + /** + * setTimeToLive + * + * @param integer $timeToLive how long the cache entry is valid + * @return Doctrine_Hydrate this object + */ + public function setTimeToLive($timeToLive) { - return $this->_cache; + if ($timeToLive !== null) { + $timeToLive = (int) $timeToLive; + } + $this->_timeToLive = $timeToLive; + + return $this; + } + /** + * getCacheDriver + * returns the cache driver associated with this object + * + * @return Doctrine_Cache_Interface|boolean|null cache driver + */ + public function getCacheDriver() + { + if ($this->_cache instanceof Doctrine_Cache_Interface) { + return $this->_cache; + } else { + return $this->_conn->getCacheDriver(); + } } /** * serialize @@ -635,6 +680,13 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable return $found; } + /** + * getCachedForm + * returns the cached form of this query for given resultSet + * + * @param array $resultSet + * @return string serialized string representation of this query + */ public function getCachedForm(array $resultSet) { $map = ''; @@ -687,20 +739,22 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable public function execute($params = array(), $return = Doctrine::FETCH_RECORD) { if ($this->_cache) { + $cacheDriver = $this->getCacheDriver(); + $dql = $this->getDql(); // calculate hash for dql query - $hash = strlen($dql) . md5($dql . var_export($params, true)); + $hash = md5($dql . var_export($params, true)); - $cached = $this->_cache->fetch($hash); + $cached = $cacheDriver->fetch($hash); if ($cached === null) { // cache miss $stmt = $this->_execute($params, $return); - $array = $this->parseData2($stmt); + $array = $this->parseData2($stmt, Doctrine::FETCH_ARRAY); $cached = $this->getCachedForm($array); - $this->_cache->save($hash, $cached); + $cacheDriver->save($hash, $cached, $this->_timeToLive); } else { $cached = unserialize($cached); $this->_tableAliases = $cached[2]; diff --git a/lib/Doctrine/Manager.php b/lib/Doctrine/Manager.php index d09667dc1..7c90a20c6 100644 --- a/lib/Doctrine/Manager.php +++ b/lib/Doctrine/Manager.php @@ -121,9 +121,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera if ( ! $init) { $init = true; $attributes = array( - Doctrine::ATTR_DQL_PARSER_CACHE => null, - Doctrine::ATTR_DQL_CACHE => null, - Doctrine::ATTR_SQL_CACHE => null, + Doctrine::ATTR_CACHE => null, Doctrine::ATTR_LOAD_REFERENCES => true, Doctrine::ATTR_LISTENER => new Doctrine_EventListener(), Doctrine::ATTR_LOCKMODE => 1, diff --git a/tests/Query/CacheTestCase.php b/tests/Query/CacheTestCase.php index 446e55462..4ef1d5abd 100644 --- a/tests/Query/CacheTestCase.php +++ b/tests/Query/CacheTestCase.php @@ -38,19 +38,16 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase $q = new Doctrine_Query(); $cache = new Doctrine_Cache_Array(); - $q->setCache($cache); - $q->select('u.name')->from('User u'); + $q->useCache($cache)->select('u.name')->from('User u'); $coll = $q->execute(); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 8); + $this->assertEqual(count($coll), 8); $coll = $q->execute(); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 8); + $this->assertEqual(count($coll), 8); } public function testResultSetCacheSupportsQueriesWithJoins() @@ -58,40 +55,59 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase $q = new Doctrine_Query(); $cache = new Doctrine_Cache_Array(); - $q->setCache($cache); + $q->useCache($cache); $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p'); $coll = $q->execute(); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 8); + $this->assertEqual(count($coll), 8); $coll = $q->execute(); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 8); + $this->assertEqual(count($coll), 8); } - + public function testResultSetCacheSupportsPreparedStatements() { $q = new Doctrine_Query(); $cache = new Doctrine_Cache_Array(); - $q->setCache($cache); + $q->useCache($cache); $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p') ->where('u.id = ?'); $coll = $q->execute(array(5)); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 1); + $this->assertEqual(count($coll), 1); $coll = $q->execute(array(5)); $this->assertEqual($cache->count(), 1); - $this->assertTrue($coll instanceof Doctrine_Collection); - $this->assertEqual($coll->count(), 1); + $this->assertEqual(count($coll), 1); + } + public function testUseCacheSupportsBooleanTrueAsParameter() + { + $q = new Doctrine_Query(); + + $cache = new Doctrine_Cache_Array(); + $this->conn->setAttribute(Doctrine::ATTR_CACHE, $cache); + + $q->useCache(true); + $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p') + ->where('u.id = ?'); + + $coll = $q->execute(array(5)); + + $this->assertEqual($cache->count(), 1); + $this->assertEqual(count($coll), 1); + + $coll = $q->execute(array(5)); + + $this->assertEqual($cache->count(), 1); + $this->assertEqual(count($coll), 1); + + $this->conn->setAttribute(Doctrine::ATTR_CACHE, null); } } diff --git a/tests/run.php b/tests/run.php index 7b90074f1..9a0e728e8 100644 --- a/tests/run.php +++ b/tests/run.php @@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); $test->addTestCase(new Doctrine_Ticket330_TestCase()); */ -/** */ +/***/ // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); @@ -238,9 +238,7 @@ $test->addTestCase(new Doctrine_ColumnAlias_TestCase()); $test->addTestCase(new Doctrine_Query_OneToOneFetching_TestCase()); -$test->addTestCase(new Doctrine_Cache_Apc_TestCase()); -$test->addTestCase(new Doctrine_Cache_Memcache_TestCase()); -$test->addTestCase(new Doctrine_Cache_Sqlite_TestCase()); + $test->addTestCase(new Doctrine_Query_Check_TestCase()); @@ -279,7 +277,7 @@ $test->addTestCase(new Doctrine_NewCore_TestCase()); $test->addTestCase(new Doctrine_Record_State_TestCase()); -//$test->addTestCase(new Doctrine_Query_Cache_TestCase()); + $test->addTestCase(new Doctrine_Tokenizer_TestCase()); @@ -309,7 +307,12 @@ $test->addTestCase(new Doctrine_Query_PgsqlSubquery_TestCase()); $test->addTestCase(new Doctrine_Query_MysqlSubqueryHaving_TestCase()); $test->addTestCase(new Doctrine_Record_ZeroValues_TestCase()); - /***/ + +$test->addTestCase(new Doctrine_Query_Cache_TestCase()); + +$test->addTestCase(new Doctrine_Cache_Apc_TestCase()); +$test->addTestCase(new Doctrine_Cache_Memcache_TestCase()); +$test->addTestCase(new Doctrine_Cache_Sqlite_TestCase()); /** $test->addTestCase(new Doctrine_Template_TestCase()); */