From ccf27a386ca779c25f66e59ff675996d5f3c0b6e Mon Sep 17 00:00:00 2001 From: jwage Date: Sat, 24 Oct 2009 00:28:43 +0000 Subject: [PATCH] [2.0] Minor fixes to cache drivers and result set cache implementation --- lib/Doctrine/Common/Cache/AbstractCache.php | 32 ++++++------ lib/Doctrine/Common/Cache/ApcCache.php | 4 +- lib/Doctrine/ORM/AbstractQuery.php | 51 ++++++++++++------- .../Tests/Common/Cache/ArrayCacheTest.php | 2 +- .../Doctrine/Tests/Common/Cache/CacheTest.php | 14 ++--- .../Tests/ORM/Functional/QueryCacheTest.php | 2 +- .../Tests/ORM/Functional/ResultCacheTest.php | 23 +++++++-- 7 files changed, 81 insertions(+), 47 deletions(-) diff --git a/lib/Doctrine/Common/Cache/AbstractCache.php b/lib/Doctrine/Common/Cache/AbstractCache.php index c1fe5fad1..96399ef06 100644 --- a/lib/Doctrine/Common/Cache/AbstractCache.php +++ b/lib/Doctrine/Common/Cache/AbstractCache.php @@ -41,7 +41,7 @@ abstract class AbstractCache implements Cache private $_namespace = null; /** @var boolean Whether to manage cache keys or not. */ - private $_manageCacheKeys = false; + private $_manageCacheIds = false; /** * Sets whether cache keys should be managed by the cache driver @@ -54,20 +54,20 @@ abstract class AbstractCache implements Cache * * @param boolean $bool */ - public function setManageCacheKeys($bool) + public function setManageCacheIds($bool) { - $this->_manageCacheKeys = $bool; + $this->_manageCacheIds = $bool; } /** * Checks whether cache keys are managed by this cache driver. * * @return boolean - * @see setManageCacheKeys() + * @see setManageCacheIds() */ - public function getManageCacheKeys() + public function getManageCacheIds() { - return $this->_manageCacheKeys; + return $this->_manageCacheIds; } /** @@ -104,7 +104,7 @@ abstract class AbstractCache implements Cache { $id = $this->_getNamespacedId($id); if ($this->_doSave($id, $data, $lifeTime)) { - if ($this->_manageCacheKeys) { + if ($this->_manageCacheIds) { $this->_saveId($id); } @@ -125,7 +125,7 @@ abstract class AbstractCache implements Cache } if ($this->_doDelete($id)) { - if ($this->_manageCacheKeys) { + if ($this->_manageCacheIds) { $this->_deleteId($id); } @@ -141,7 +141,7 @@ abstract class AbstractCache implements Cache */ public function deleteAll() { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $ids = $this->getIds(); foreach ($ids as $id) { $this->delete($id); @@ -157,7 +157,7 @@ abstract class AbstractCache implements Cache */ public function deleteByRegex($regex) { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $deleted = array(); $ids = $this->getIds(); foreach ($ids as $id) { @@ -177,7 +177,7 @@ abstract class AbstractCache implements Cache */ public function deleteByPrefix($prefix) { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $deleted = array(); $ids = $this->getIds(); foreach ($ids as $id) { @@ -197,7 +197,7 @@ abstract class AbstractCache implements Cache */ public function deleteBySuffix($suffix) { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $deleted = array(); $ids = $this->getIds(); foreach ($ids as $id) { @@ -216,7 +216,7 @@ abstract class AbstractCache implements Cache */ public function count() { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $ids = $this->getIds(); return $ids ? count($ids) : 0; } @@ -228,7 +228,7 @@ abstract class AbstractCache implements Cache */ public function getIds() { - $this->_errorIfCacheKeysNotManaged(); + $this->_errorIfCacheIdsNotManaged(); $ids = $this->fetch($this->_cacheIdsIndexId); return $ids ? $ids : array(); } @@ -285,9 +285,9 @@ abstract class AbstractCache implements Cache /** * @throws BadMethodCallException If the cache driver does not manage cache keys. */ - private function _errorIfCacheKeysNotManaged() + private function _errorIfCacheIdsNotManaged() { - if ( ! $this->_manageCacheKeys) { + if ( ! $this->_manageCacheIds) { throw new \BadMethodCallException("Operation not supported if cache keys are not managed."); } } diff --git a/lib/Doctrine/Common/Cache/ApcCache.php b/lib/Doctrine/Common/Cache/ApcCache.php index d2a0f4b14..32674a558 100644 --- a/lib/Doctrine/Common/Cache/ApcCache.php +++ b/lib/Doctrine/Common/Cache/ApcCache.php @@ -47,7 +47,9 @@ class ApcCache extends AbstractCache */ protected function _doContains($id) { - return apc_fetch($id) === false ? false : true; + $found = false; + apc_fetch($id, $found); + return $found; } /** diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index a59e470ff..971f0f1bc 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -92,7 +92,14 @@ abstract class AbstractQuery * * @var CacheDriver */ - protected $_resultCache; + protected $_resultCacheDriver; + + /** + * Boolean flag for whether or not to cache the result sets of this query. + * + * @var boolean + */ + protected $_useResultCache; /** * The id to store the result cache entry under. @@ -211,13 +218,15 @@ abstract class AbstractQuery * @param Doctrine\Common\Cache\Cache $driver Cache driver * @return Doctrine\ORM\Query */ - public function setResultCache($resultCache = null) + public function setResultCacheDriver($resultCacheDriver = null) { - if ($resultCache !== null && ! ($resultCache instanceof \Doctrine\Common\Cache\Cache)) { - throw DoctrineException::invalidResultCacheObject($resultCache); + if ($resultCacheDriver !== null && ! ($resultCacheDriver instanceof \Doctrine\Common\Cache\Cache)) { + throw DoctrineException::invalidResultCacheObject($resultCacheDriver); + } + $this->_resultCacheDriver = $resultCacheDriver; + if ($resultCacheDriver) { + $this->_useResultCache = true; } - $this->_resultCache = $resultCache; - return $this; } /** @@ -227,18 +236,33 @@ abstract class AbstractQuery */ public function getResultCacheDriver() { - if ($this->_resultCache) { - return $this->_resultCache; + if ($this->_resultCacheDriver) { + return $this->_resultCacheDriver; } else { return $this->_em->getConfiguration()->getResultCacheImpl(); } } + /** + * Set whether or not to cache the result sets for this query + * + * @param boolean $bool + */ + public function useResultCache($bool, $timeToLive = null, $resultCacheId = null) + { + $this->_useResultCache = $bool; + if ($timeToLive) { + $this->setResultCacheLifetime($timeToLive); + } + if ($resultCacheId) { + $this->_resultCacheId = $resultCacheId; + } + } + /** * Defines how long the result cache will be active before expire. * * @param integer $timeToLive How long the cache entry is valid - * @return Doctrine\ORM\Query */ public function setResultCacheLifetime($timeToLive) { @@ -247,8 +271,6 @@ abstract class AbstractQuery } $this->_resultCacheTTL = $timeToLive; - - return $this; } /** @@ -270,8 +292,6 @@ abstract class AbstractQuery public function setExpireResultCache($expire = true) { $this->_expireResultCache = $expire; - - return $this; } /** @@ -289,12 +309,10 @@ abstract class AbstractQuery * * @param integer $hydrationMode Doctrine processing mode to be used during hydration process. * One of the Query::HYDRATE_* constants. - * @return Doctrine\ORM\Query */ public function setHydrationMode($hydrationMode) { $this->_hydrationMode = $hydrationMode; - return $this; } /** @@ -443,7 +461,7 @@ abstract class AbstractQuery $params = $this->getParameters($params); // Check result cache - if ($cacheDriver = $this->getResultCacheDriver()) { + if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) { $id = $this->_getResultCacheId($params); $cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id); @@ -481,7 +499,6 @@ abstract class AbstractQuery * generated for you. * * @param string $id - * @return void */ public function setResultCacheId($id) { diff --git a/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php b/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php index a1cacc9ce..18a750f7f 100644 --- a/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php +++ b/tests/Doctrine/Tests/Common/Cache/ArrayCacheTest.php @@ -11,7 +11,7 @@ class ArrayCacheTest extends \Doctrine\Tests\DoctrineTestCase public function testArrayCacheDriver() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); // Test save $cache->save('test_key', 'testing this out'); diff --git a/tests/Doctrine/Tests/Common/Cache/CacheTest.php b/tests/Doctrine/Tests/Common/Cache/CacheTest.php index 3f5dbce4b..088ead22e 100644 --- a/tests/Doctrine/Tests/Common/Cache/CacheTest.php +++ b/tests/Doctrine/Tests/Common/Cache/CacheTest.php @@ -11,7 +11,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testCount() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $this->assertEquals($cache->count(), 2); @@ -20,7 +20,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testDeleteAll() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->deleteAll(); @@ -31,7 +31,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testDeleteByRegex() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->deleteByRegex('/test_key[0-9]/'); @@ -42,7 +42,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testDeleteByPrefix() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->deleteByPrefix('test_key'); @@ -53,7 +53,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testDeleteBySuffix() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('1test_key', '1'); $cache->save('2test_key', '2'); $cache->deleteBySuffix('test_key'); @@ -64,7 +64,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testDeleteByWildcard() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->save('test_key1', '1'); $cache->save('test_key2', '2'); $cache->delete('test_key*'); @@ -75,7 +75,7 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase public function testNamespace() { $cache = new ArrayCache(); - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $cache->setNamespace('test_'); $cache->save('key1', 'test'); $this->assertTrue($cache->contains('key1')); diff --git a/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php index 84ac58a7e..0f0f3fe13 100644 --- a/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/QueryCacheTest.php @@ -33,7 +33,7 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $cache = new ArrayCache; - $cache->setManageCacheKeys(true); + $cache->setManageCacheIds(true); $query->setQueryCacheDriver($cache); $this->assertEquals(0, $cache->count()); diff --git a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php index 8e6c21bb3..4de48b0aa 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php @@ -31,8 +31,8 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $cache = new ArrayCache; - $cache->setManageCacheKeys(true); - $query->setResultCache($cache); + $cache->setManageCacheIds(true); + $query->setResultCacheDriver($cache); $this->assertEquals(0, $cache->count()); $users = $query->getResult(); @@ -44,7 +44,7 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); $query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); - $query2->setResultCache($cache); + $query2->setResultCacheDriver($cache); $users = $query2->getResult(); @@ -58,10 +58,25 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $cache = new ArrayCache; $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); - $query->setResultCache($cache); + $query->setResultCacheDriver($cache); $query->setResultCacheId('testing_result_cache_id'); $users = $query->getResult(); $this->assertTrue($cache->contains('testing_result_cache_id')); } + + public function testUseResultCache() + { + $cache = new \Doctrine\Common\Cache\ArrayCache(); + $this->_em->getConfiguration()->setResultCacheImpl($cache); + + $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); + $query->useResultCache(true); + $query->setResultCacheId('testing_result_cache_id'); + $users = $query->getResult(); + + $this->assertTrue($cache->contains('testing_result_cache_id')); + + $this->_em->getConfiguration()->setResultCacheImpl(null); + } } \ No newline at end of file