Commit efb733d7 authored by romanb's avatar romanb

[2.0] Refactored cache drivers. Made use of ArrayCache as the metadata cache...

[2.0] Refactored cache drivers. Made use of ArrayCache as the metadata cache during a test suite run.
parent 886c9611
......@@ -16,10 +16,10 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
#namespace Doctrine\ORM\Cache;
namespace Doctrine\ORM\Cache;
/**
* APC cache driver.
......@@ -31,37 +31,28 @@
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Roman Borschel <roman@code-factory.org>
*/
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)
{
......
......@@ -19,30 +19,28 @@
* <http://www.phpdoctrine.org>.
*/
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 <kvesteri@cc.hut.fi>
*/
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()
{
......
......@@ -16,10 +16,10 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
#namespace Doctrine\ORM\Cache;
namespace Doctrine\ORM\Cache;
/**
* Interface for cache drivers.
......@@ -31,7 +31,7 @@
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Roman Borschel <roman@code-factory.org>
*/
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
......
......@@ -16,28 +16,27 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
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 <kvesteri@cc.hut.fi>
* @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)
{
......
......@@ -16,9 +16,11 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Cache;
/**
* Memcache cache driver.
*
......@@ -28,7 +30,7 @@
* @version $Revision: 4910 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
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)
{
......
......@@ -16,22 +16,24 @@
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.doctrine-project.org>.
*/
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)
{
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
}
}
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