From efb733d7dfda724beb28d44cba83f86b9ffd7999 Mon Sep 17 00:00:00 2001 From: romanb Date: Wed, 4 Feb 2009 18:03:05 +0000 Subject: [PATCH] [2.0] Refactored cache drivers. Made use of ArrayCache as the metadata cache during a test suite run. --- .../ORM/Cache/{Apc.php => ApcCache.php} | 39 +++++------------ .../ORM/Cache/{Array.php => ArrayCache.php} | 43 ++++++------------- .../ORM/Cache/{Interface.php => Cache.php} | 17 +++----- .../ORM/Cache/{Db.php => DbCache.php} | 42 ++++++------------ .../Cache/{Memcache.php => MemcacheCache.php} | 37 +++++----------- .../ORM/Cache/{Xcache.php => XcacheCache.php} | 37 +++++----------- .../ORM/Mapping/ClassMetadataFactory.php | 9 ++-- .../Doctrine/Tests/OrmFunctionalTestCase.php | 14 +++++- tests/Doctrine/Tests/OrmTestCase.php | 12 ++++++ 9 files changed, 94 insertions(+), 156 deletions(-) rename lib/Doctrine/ORM/Cache/{Apc.php => ApcCache.php} (55%) rename lib/Doctrine/ORM/Cache/{Array.php => ArrayCache.php} (57%) rename lib/Doctrine/ORM/Cache/{Interface.php => Cache.php} (82%) rename lib/Doctrine/ORM/Cache/{Db.php => DbCache.php} (75%) rename lib/Doctrine/ORM/Cache/{Memcache.php => MemcacheCache.php} (64%) rename lib/Doctrine/ORM/Cache/{Xcache.php => XcacheCache.php} (61%) diff --git a/lib/Doctrine/ORM/Cache/Apc.php b/lib/Doctrine/ORM/Cache/ApcCache.php similarity index 55% rename from lib/Doctrine/ORM/Cache/Apc.php rename to lib/Doctrine/ORM/Cache/ApcCache.php index 77ac72097..6192d3f34 100644 --- a/lib/Doctrine/ORM/Cache/Apc.php +++ b/lib/Doctrine/ORM/Cache/ApcCache.php @@ -16,10 +16,10 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ -#namespace Doctrine\ORM\Cache; +namespace Doctrine\ORM\Cache; /** * APC cache driver. @@ -31,37 +31,28 @@ * @author Konsta Vesterinen * @author Roman Borschel */ -class Doctrine_ORM_Cache_ApcCache implements Doctrine_ORM_Cache_Cache +class ApcCache implements Cache { /** - * constructor - * - * @param array $options associative array of cache driver options + * {@inheritdoc} */ public function __construct() { if ( ! extension_loaded('apc')) { - throw new Doctrine_Cache_Exception('The apc extension must be loaded for using this backend !'); + throw new DoctrineException('The apc extension must be loaded in order to use the ApcCache.'); } } /** - * Test if a cache is available for the given id and (if yes) return it (false else). - * - * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested - * @return string cached datas (or false) + * {@inheritdoc} */ - public function fetch($id, $testCacheValidity = true) + public function fetch($id) { return apc_fetch($id); } /** - * Test if a cache is available or not (for the given id) - * - * @param string $id cache id - * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + * {@inheritdoc} */ public function contains($id) { @@ -69,14 +60,7 @@ class Doctrine_ORM_Cache_ApcCache implements Doctrine_ORM_Cache_Cache } /** - * Save some string datas into a cache record - * - * Note : $data is always saved as a string - * - * @param string $data data to cache - * @param string $id cache id - * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) - * @return boolean true if no problem + * {@inheritdoc} */ public function save($id, $data, $lifeTime = false) { @@ -84,10 +68,7 @@ class Doctrine_ORM_Cache_ApcCache implements Doctrine_ORM_Cache_Cache } /** - * Remove a cache record - * - * @param string $id cache id - * @return boolean true if no problem + * {@inheritdoc} */ public function delete($id) { diff --git a/lib/Doctrine/ORM/Cache/Array.php b/lib/Doctrine/ORM/Cache/ArrayCache.php similarity index 57% rename from lib/Doctrine/ORM/Cache/Array.php rename to lib/Doctrine/ORM/Cache/ArrayCache.php index cd82169af..54c80ed14 100644 --- a/lib/Doctrine/ORM/Cache/Array.php +++ b/lib/Doctrine/ORM/Cache/ArrayCache.php @@ -19,31 +19,29 @@ * . */ +namespace Doctrine\ORM\Cache; + /** * Array cache driver. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.phpdoctrine.org + * @link www.doctrine-project.org * @since 1.0 * @version $Revision: 4910 $ * @author Konsta Vesterinen */ -class Doctrine_ORM_Cache_ArrayCache implements Doctrine_ORM_Cache_Cache +class ArrayCache implements Cache { /** - * @var array $data an array of cached data + * @var array $data */ - protected $data; + private $data; /** - * Test if a cache is available for the given id and (if yes) return it (false else). - * - * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested - * @return string cached datas (or false) + * {@inheritdoc} */ - public function fetch($id, $testCacheValidity = true) - { + public function fetch($id) + { if (isset($this->data[$id])) { return $this->data[$id]; } @@ -51,10 +49,7 @@ class Doctrine_ORM_Cache_ArrayCache implements Doctrine_ORM_Cache_Cache } /** - * Test if a cache is available or not (for the given id) - * - * @param string $id cache id - * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + * {@inheritdoc} */ public function contains($id) { @@ -62,14 +57,7 @@ class Doctrine_ORM_Cache_ArrayCache implements Doctrine_ORM_Cache_Cache } /** - * Save some string datas into a cache record - * - * Note : $data is always saved as a string - * - * @param string $data data to cache - * @param string $id cache id - * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) - * @return boolean true if no problem + * {@inheritdoc} */ public function save($id, $data, $lifeTime = false) { @@ -77,10 +65,7 @@ class Doctrine_ORM_Cache_ArrayCache implements Doctrine_ORM_Cache_Cache } /** - * Remove a cache record - * - * @param string $id cache id - * @return boolean true if no problem + * {@inheritdoc} */ public function delete($id) { @@ -88,9 +73,7 @@ class Doctrine_ORM_Cache_ArrayCache implements Doctrine_ORM_Cache_Cache } /** - * Remove all cache record - * - * @return boolean true if no problem + * {@inheritdoc} */ public function deleteAll() { diff --git a/lib/Doctrine/ORM/Cache/Interface.php b/lib/Doctrine/ORM/Cache/Cache.php similarity index 82% rename from lib/Doctrine/ORM/Cache/Interface.php rename to lib/Doctrine/ORM/Cache/Cache.php index 564150889..f23565447 100644 --- a/lib/Doctrine/ORM/Cache/Interface.php +++ b/lib/Doctrine/ORM/Cache/Cache.php @@ -16,10 +16,10 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ -#namespace Doctrine\ORM\Cache; +namespace Doctrine\ORM\Cache; /** * Interface for cache drivers. @@ -31,7 +31,7 @@ * @author Konsta Vesterinen * @author Roman Borschel */ -interface Doctrine_ORM_Cache_Cache +interface Cache { /** * Test if a cache entry is available for the given id and (if yes) return it (false else). @@ -39,10 +39,9 @@ interface Doctrine_ORM_Cache_Cache * Note : return value is always "string" (unserialization is done by the core not by the backend) * * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested * @return string cached datas (or false) */ - public function fetch($id, $testCacheValidity = true); + public function fetch($id); /** * Test if a cache is available or not (for the given id) @@ -53,16 +52,14 @@ interface Doctrine_ORM_Cache_Cache public function contains($id); /** - * Save some string datas into a cache record + * Puts data into the cache. * - * Note : $data is always saved as a string - * - * @param string $data data to cache * @param string $id cache id + * @param string $data data to cache * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) * @return boolean true if no problem */ - public function save($data, $id, $lifeTime = false); + public function save($id, $data, $lifeTime = false); /** * Remove a cache record diff --git a/lib/Doctrine/ORM/Cache/Db.php b/lib/Doctrine/ORM/Cache/DbCache.php similarity index 75% rename from lib/Doctrine/ORM/Cache/Db.php rename to lib/Doctrine/ORM/Cache/DbCache.php index 47b495a91..f25b9a053 100644 --- a/lib/Doctrine/ORM/Cache/Db.php +++ b/lib/Doctrine/ORM/Cache/DbCache.php @@ -16,28 +16,27 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ +namespace Doctrine\ORM\Cache; + /** * Doctrine_Cache_Db * - * @package Doctrine - * @subpackage Cache * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.phpdoctrine.org + * @link www.doctrine-project.org * @since 1.0 * @version $Revision: 3931 $ * @author Konsta Vesterinen + * @todo Needs some maintenance. Any takers? */ -class Doctrine_ORM_Cache_DbCache implements Doctrine_ORM_Cache_Cache, Countable +class DbCache implements Cache, \Countable { private $_options = array(); /** - * constructor - * - * @param array $_options an array of options + * {@inheritdoc} */ public function __construct($options) { @@ -68,13 +67,9 @@ class Doctrine_ORM_Cache_DbCache implements Doctrine_ORM_Cache_Cache, Countable } /** - * Test if a cache is available for the given id and (if yes) return it (false else). - * - * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested - * @return string cached datas (or false) + * {@inheritdoc} */ - public function fetch($id, $testCacheValidity = true) + public function fetch($id) { $sql = 'SELECT data, expire FROM ' . $this->_options['tableName'] . ' WHERE id = ?'; @@ -93,10 +88,7 @@ class Doctrine_ORM_Cache_DbCache implements Doctrine_ORM_Cache_Cache, Countable } /** - * Test if a cache is available or not (for the given id) - * - * @param string $id cache id - * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + * {@inheritdoc} */ public function contains($id) { @@ -107,14 +99,7 @@ class Doctrine_ORM_Cache_DbCache implements Doctrine_ORM_Cache_Cache, Countable } /** - * Save some string datas into a cache record - * - * Note : $data is always saved as a string - * - * @param string $data data to cache - * @param string $id cache id - * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) - * @return boolean true if no problem + * {@inheritdoc} */ public function save($data, $id, $lifeTime = false) { @@ -133,10 +118,7 @@ class Doctrine_ORM_Cache_DbCache implements Doctrine_ORM_Cache_Cache, Countable } /** - * Remove a cache record - * - * @param string $id cache id - * @return boolean true if no problem + * {@inheritdoc} */ public function delete($id) { diff --git a/lib/Doctrine/ORM/Cache/Memcache.php b/lib/Doctrine/ORM/Cache/MemcacheCache.php similarity index 64% rename from lib/Doctrine/ORM/Cache/Memcache.php rename to lib/Doctrine/ORM/Cache/MemcacheCache.php index ec163d7d6..be622a858 100644 --- a/lib/Doctrine/ORM/Cache/Memcache.php +++ b/lib/Doctrine/ORM/Cache/MemcacheCache.php @@ -16,9 +16,11 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ +namespace Doctrine\ORM\Cache; + /** * Memcache cache driver. * @@ -28,7 +30,7 @@ * @version $Revision: 4910 $ * @author Konsta Vesterinen */ -class Doctrine_ORM_Cache_MemcacheCache implements Doctrine_ORM_Cache_Cache +class MemcacheCache implements Cache { /** * @var Memcache $_memcache memcache object @@ -36,9 +38,7 @@ class Doctrine_ORM_Cache_MemcacheCache implements Doctrine_ORM_Cache_Cache private $_memcache; /** - * constructor - * - * @param array $options associative array of cache driver options + * {@inheritdoc} */ public function __construct() { @@ -68,22 +68,15 @@ class Doctrine_ORM_Cache_MemcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Test if a cache is available for the given id and (if yes) return it (false else). - * - * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested - * @return string cached datas (or false) + * {@inheritdoc} */ - public function fetch($id, $testCacheValidity = true) + public function fetch($id) { return $this->_memcache->get($id); } /** - * Test if a cache is available or not (for the given id) - * - * @param string $id cache id - * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + * {@inheritdoc} */ public function contains($id) { @@ -91,14 +84,7 @@ class Doctrine_ORM_Cache_MemcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Save some string datas into a cache record - * - * Note : $data is always saved as a string - * - * @param string $data data to cache - * @param string $id cache id - * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) - * @return boolean true if no problem + * {@inheritdoc} */ public function save($id, $data, $lifeTime = false) { @@ -106,10 +92,7 @@ class Doctrine_ORM_Cache_MemcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Remove a cache record - * - * @param string $id cache id - * @return boolean true if no problem + * {@inheritdoc} */ public function delete($id) { diff --git a/lib/Doctrine/ORM/Cache/Xcache.php b/lib/Doctrine/ORM/Cache/XcacheCache.php similarity index 61% rename from lib/Doctrine/ORM/Cache/Xcache.php rename to lib/Doctrine/ORM/Cache/XcacheCache.php index 1cc801169..dfd457b3b 100644 --- a/lib/Doctrine/ORM/Cache/Xcache.php +++ b/lib/Doctrine/ORM/Cache/XcacheCache.php @@ -16,22 +16,24 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ +namespace Doctrine\ORM\Cache; + /** * Xcache cache driver. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.phpdoctrine.org + * @link www.doctrine-project.org * @since 1.0 * @version $Revision: $ * @author Dmitry Bakaleinik (dima@snaiper.net) */ -class Doctrine_ORM_Cache_XcacheCache implements Doctrine_ORM_Cache_Cache +class XcacheCache implements Cache { /** - * constructor + * {@inheritdoc} */ public function __construct() { @@ -41,22 +43,15 @@ class Doctrine_ORM_Cache_XcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Test if a cache entry is available for the given id and (if yes) return it (false else). - * - * @param string $id cache id - * @param boolean $testCacheValidity if set to false, the cache validity won't be tested - * @return string cached datas (or false) + * {@inheritdoc} */ - public function fetch($id, $testCacheValidity = true) + public function fetch($id) { return $this->contains($id) ? xcache_get($id) : false; } /** - * Test if a cache is available or not (for the given id) - * - * @param string $id cache id - * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record + * {@inheritdoc} */ public function contains($id) { @@ -64,14 +59,7 @@ class Doctrine_ORM_Cache_XcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Save some string datas into a cache record - * - * Note : $data is always saved as a string - * - * @param string $data data to cache - * @param string $id cache id - * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) - * @return boolean true if no problem + * {@inheritdoc} */ public function save($id, $data, $lifeTime = false) { @@ -79,10 +67,7 @@ class Doctrine_ORM_Cache_XcacheCache implements Doctrine_ORM_Cache_Cache } /** - * Remove a cache record - * - * @param string $id cache id - * @return boolean true if no problem + * {@inheritdoc} */ public function delete($id) { diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 311dffed8..995ca5084 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -39,7 +39,9 @@ class ClassMetadataFactory { /** The targeted database platform. */ private $_targetPlatform; + /** The used metadata driver. */ private $_driver; + /** The used cache driver. */ private $_cacheDriver; /** @@ -82,12 +84,13 @@ class ClassMetadataFactory public function getMetadataFor($className) { if ( ! isset($this->_loadedMetadata[$className])) { + $cacheKey = "$className\$CLASSMETADATA"; if ($this->_cacheDriver) { - if ($this->_cacheDriver->contains("$className\$CLASSMETADATA")) { - $this->_loadedMetadata[$className] = $this->_cacheDriver->fetch("$className\$CLASSMETADATA"); + if ($this->_cacheDriver->contains($cacheKey)) { + $this->_loadedMetadata[$className] = $this->_cacheDriver->fetch($cacheKey); } else { $this->_loadMetadata($className); - $this->_cacheDriver->save($this->_loadedMetadata[$className], "$className\$CLASSMETADATA", null); + $this->_cacheDriver->save($cacheKey, $this->_loadedMetadata[$className], null); } } else { $this->_loadMetadata($className); diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 7338f3336..33fbf7303 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -5,9 +5,14 @@ namespace Doctrine\Tests; /** * Base testcase class for all orm testcases. * + * @since 2.0 */ class OrmFunctionalTestCase extends OrmTestCase { + /* The metadata cache shared between all functional tests. */ + private static $_metadataCacheImpl = null; + + /** The EntityManager for this testcase. */ protected $_em; /** @@ -97,7 +102,7 @@ class OrmFunctionalTestCase extends OrmTestCase } /** - * Sweeps the database tables of all used fixtures. + * Sweeps the database tables of all used fixtures and clears the EntityManager. */ protected function tearDown() { @@ -120,7 +125,14 @@ class OrmFunctionalTestCase extends OrmTestCase } protected function _getEntityManager($config = null, $eventManager = null) { + // NOTE: Functional tests use their own shared metadata cache, because + // the actual database platform used during execution has effect on some + // metadata mapping behaviors (like the choice of the ID generation). + if (is_null(self::$_metadataCacheImpl)) { + self::$_metadataCacheImpl = new \Doctrine\ORM\Cache\ArrayCache; + } $config = new \Doctrine\ORM\Configuration(); + $config->setMetadataCacheImpl(self::$_metadataCacheImpl); $eventManager = new \Doctrine\Common\EventManager(); $conn = $this->sharedFixture['conn']; return \Doctrine\ORM\EntityManager::create($conn, 'em', $config, $eventManager); diff --git a/tests/Doctrine/Tests/OrmTestCase.php b/tests/Doctrine/Tests/OrmTestCase.php index e870c6c6f..d78f29050 100644 --- a/tests/Doctrine/Tests/OrmTestCase.php +++ b/tests/Doctrine/Tests/OrmTestCase.php @@ -7,6 +7,9 @@ namespace Doctrine\Tests; */ class OrmTestCase extends DoctrineTestCase { + /** The metadata cache that is shared between all ORM tests (except functional tests). */ + private static $_metadataCacheImpl = null; + /** * Creates an EntityManager for testing purposes. * @@ -14,6 +17,7 @@ class OrmTestCase extends DoctrineTestCase */ protected function _getTestEntityManager($conf = null, $eventManager = null) { $config = new \Doctrine\ORM\Configuration(); + $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl()); $eventManager = new \Doctrine\Common\EventManager(); $connectionOptions = array( 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock', @@ -23,4 +27,12 @@ class OrmTestCase extends DoctrineTestCase ); return \Doctrine\ORM\EntityManager::create($connectionOptions, 'mockEM', $config, $eventManager); } + + private static function getSharedMetadataCacheImpl() + { + if (is_null(self::$_metadataCacheImpl)) { + self::$_metadataCacheImpl = new \Doctrine\ORM\Cache\ArrayCache; + } + return self::$_metadataCacheImpl; + } }