Commit 0624cbd8 authored by zYne's avatar zYne

--no commit message

--no commit message
parent 6bedbdf3
...@@ -190,9 +190,7 @@ final class Doctrine ...@@ -190,9 +190,7 @@ final class Doctrine
const ATTR_NS_GAP_SIZE = 131; const ATTR_NS_GAP_SIZE = 131;
const ATTR_NS_GAP_DECREASE_EXP = 132; const ATTR_NS_GAP_DECREASE_EXP = 132;
const ATTR_DQL_PARSER_CACHE = 150; const ATTR_CACHE = 150;
const ATTR_DQL_CACHE = 151;
const ATTR_SQL_CACHE = 152;
const ATTR_LOAD_REFERENCES = 153; const ATTR_LOAD_REFERENCES = 153;
/** /**
......
...@@ -110,13 +110,11 @@ abstract class Doctrine_Configurable ...@@ -110,13 +110,11 @@ abstract class Doctrine_Configurable
throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'"); throw new Doctrine_Exception("Couldn't set collection key attribute. No such column '$value'");
} }
break; break;
case Doctrine::ATTR_DQL_CACHE: case Doctrine::ATTR_CACHE:
case Doctrine::ATTR_DQL_PARSER_CACHE:
case Doctrine::ATTR_SQL_CACHE:
if ($value !== null) { if ($value !== null) {
if ( ! ($value instanceof Doctrine_Cache_Interface)) { if ( ! ($value instanceof Doctrine_Cache_Interface)) {
throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface'); throw new Doctrine_Exception('Cache driver should implement Doctrine_Cache_Interface');
} }
} }
break; break;
case Doctrine::ATTR_VLD: case Doctrine::ATTR_VLD:
...@@ -158,6 +156,19 @@ abstract class Doctrine_Configurable ...@@ -158,6 +156,19 @@ abstract class Doctrine_Configurable
$this->attributes[$attribute] = $value; $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 * @param Doctrine_EventListener $listener
* @return void * @return void
...@@ -204,9 +215,7 @@ abstract class Doctrine_Configurable ...@@ -204,9 +215,7 @@ abstract class Doctrine_Configurable
* setListener * setListener
* *
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Connection_Informix|Doctrine_Connection_Mssql|Doctrine_Connection_Oracle| * @return Doctrine_Configurable this object
* Doctrine_Connection_Db2|Doctrine_Connection_Firebird|Doctrine_Connection_Common|
* Doctrine_Manager|Doctrine_Connection|Doctrine_Table
*/ */
public function setListener($listener) public function setListener($listener)
{ {
......
...@@ -129,7 +129,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable ...@@ -129,7 +129,9 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
* @see Doctrine_Query::* constants * @see Doctrine_Query::* constants
*/ */
protected $type = self::SELECT; protected $type = self::SELECT;
/**
* @var array
*/
protected $_cache; protected $_cache;
protected $_tableAliases = array(); protected $_tableAliases = array();
...@@ -177,13 +179,56 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable ...@@ -177,13 +179,56 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
{ {
return $this->getQuery(); 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)
{
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);
}
/**
* setTimeToLive
*
* @param integer $timeToLive how long the cache entry is valid
* @return Doctrine_Hydrate this object
*/
public function setTimeToLive($timeToLive)
{ {
$this->_cache = $cache; if ($timeToLive !== null) {
$timeToLive = (int) $timeToLive;
}
$this->_timeToLive = $timeToLive;
return $this;
} }
public function getCache() /**
* getCacheDriver
* returns the cache driver associated with this object
*
* @return Doctrine_Cache_Interface|boolean|null cache driver
*/
public function getCacheDriver()
{ {
return $this->_cache; if ($this->_cache instanceof Doctrine_Cache_Interface) {
return $this->_cache;
} else {
return $this->_conn->getCacheDriver();
}
} }
/** /**
* serialize * serialize
...@@ -635,6 +680,13 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable ...@@ -635,6 +680,13 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
return $found; 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) public function getCachedForm(array $resultSet)
{ {
$map = ''; $map = '';
...@@ -687,20 +739,22 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable ...@@ -687,20 +739,22 @@ class Doctrine_Hydrate extends Doctrine_Object implements Serializable
public function execute($params = array(), $return = Doctrine::FETCH_RECORD) public function execute($params = array(), $return = Doctrine::FETCH_RECORD)
{ {
if ($this->_cache) { if ($this->_cache) {
$cacheDriver = $this->getCacheDriver();
$dql = $this->getDql(); $dql = $this->getDql();
// calculate hash for dql query // 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) { if ($cached === null) {
// cache miss // cache miss
$stmt = $this->_execute($params, $return); $stmt = $this->_execute($params, $return);
$array = $this->parseData2($stmt); $array = $this->parseData2($stmt, Doctrine::FETCH_ARRAY);
$cached = $this->getCachedForm($array); $cached = $this->getCachedForm($array);
$this->_cache->save($hash, $cached); $cacheDriver->save($hash, $cached, $this->_timeToLive);
} else { } else {
$cached = unserialize($cached); $cached = unserialize($cached);
$this->_tableAliases = $cached[2]; $this->_tableAliases = $cached[2];
......
...@@ -121,9 +121,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -121,9 +121,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
if ( ! $init) { if ( ! $init) {
$init = true; $init = true;
$attributes = array( $attributes = array(
Doctrine::ATTR_DQL_PARSER_CACHE => null, Doctrine::ATTR_CACHE => null,
Doctrine::ATTR_DQL_CACHE => null,
Doctrine::ATTR_SQL_CACHE => null,
Doctrine::ATTR_LOAD_REFERENCES => true, Doctrine::ATTR_LOAD_REFERENCES => true,
Doctrine::ATTR_LISTENER => new Doctrine_EventListener(), Doctrine::ATTR_LISTENER => new Doctrine_EventListener(),
Doctrine::ATTR_LOCKMODE => 1, Doctrine::ATTR_LOCKMODE => 1,
......
...@@ -38,19 +38,16 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase ...@@ -38,19 +38,16 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$cache = new Doctrine_Cache_Array(); $cache = new Doctrine_Cache_Array();
$q->setCache($cache); $q->useCache($cache)->select('u.name')->from('User u');
$q->select('u.name')->from('User u');
$coll = $q->execute(); $coll = $q->execute();
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 8);
$this->assertEqual($coll->count(), 8);
$coll = $q->execute(); $coll = $q->execute();
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 8);
$this->assertEqual($coll->count(), 8);
} }
public function testResultSetCacheSupportsQueriesWithJoins() public function testResultSetCacheSupportsQueriesWithJoins()
...@@ -58,40 +55,59 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase ...@@ -58,40 +55,59 @@ class Doctrine_Query_Cache_TestCase extends Doctrine_UnitTestCase
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$cache = new Doctrine_Cache_Array(); $cache = new Doctrine_Cache_Array();
$q->setCache($cache); $q->useCache($cache);
$q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p'); $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p');
$coll = $q->execute(); $coll = $q->execute();
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 8);
$this->assertEqual($coll->count(), 8);
$coll = $q->execute(); $coll = $q->execute();
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 8);
$this->assertEqual($coll->count(), 8);
} }
public function testResultSetCacheSupportsPreparedStatements() public function testResultSetCacheSupportsPreparedStatements()
{ {
$q = new Doctrine_Query(); $q = new Doctrine_Query();
$cache = new Doctrine_Cache_Array(); $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->assertEqual(count($coll), 1);
$coll = $q->execute(array(5));
$this->assertEqual($cache->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') $q->select('u.name')->from('User u')->leftJoin('u.Phonenumber p')
->where('u.id = ?'); ->where('u.id = ?');
$coll = $q->execute(array(5)); $coll = $q->execute(array(5));
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 1);
$this->assertEqual($coll->count(), 1);
$coll = $q->execute(array(5)); $coll = $q->execute(array(5));
$this->assertEqual($cache->count(), 1); $this->assertEqual($cache->count(), 1);
$this->assertTrue($coll instanceof Doctrine_Collection); $this->assertEqual(count($coll), 1);
$this->assertEqual($coll->count(), 1);
$this->conn->setAttribute(Doctrine::ATTR_CACHE, null);
} }
} }
...@@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); ...@@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests');
$test->addTestCase(new Doctrine_Ticket330_TestCase()); $test->addTestCase(new Doctrine_Ticket330_TestCase());
*/ */
/** */ /***/
// Connection drivers (not yet fully tested) // Connection drivers (not yet fully tested)
$test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase());
$test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase());
...@@ -238,9 +238,7 @@ $test->addTestCase(new Doctrine_ColumnAlias_TestCase()); ...@@ -238,9 +238,7 @@ $test->addTestCase(new Doctrine_ColumnAlias_TestCase());
$test->addTestCase(new Doctrine_Query_OneToOneFetching_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()); $test->addTestCase(new Doctrine_Query_Check_TestCase());
...@@ -279,7 +277,7 @@ $test->addTestCase(new Doctrine_NewCore_TestCase()); ...@@ -279,7 +277,7 @@ $test->addTestCase(new Doctrine_NewCore_TestCase());
$test->addTestCase(new Doctrine_Record_State_TestCase()); $test->addTestCase(new Doctrine_Record_State_TestCase());
//$test->addTestCase(new Doctrine_Query_Cache_TestCase());
$test->addTestCase(new Doctrine_Tokenizer_TestCase()); $test->addTestCase(new Doctrine_Tokenizer_TestCase());
...@@ -309,7 +307,12 @@ $test->addTestCase(new Doctrine_Query_PgsqlSubquery_TestCase()); ...@@ -309,7 +307,12 @@ $test->addTestCase(new Doctrine_Query_PgsqlSubquery_TestCase());
$test->addTestCase(new Doctrine_Query_MysqlSubqueryHaving_TestCase()); $test->addTestCase(new Doctrine_Query_MysqlSubqueryHaving_TestCase());
$test->addTestCase(new Doctrine_Record_ZeroValues_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()); $test->addTestCase(new Doctrine_Template_TestCase());
*/ */
......
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