Commit 1ad982a4 authored by hobodave's avatar hobodave

[2.0][DC-460] Refactored cache bulk deletion methods to use driver specific...

[2.0][DC-460] Refactored cache bulk deletion methods to use driver specific features to retrieve list of keys. Also, refactored tests
so that all methods are tested for all drivers.

Removed:

- Doctrine\Common\Cache\AbstractCache::count()
- Doctrine\Common\Cache\AbstractCache::deleteAll()

API Changes:

- Doctrine\ORM\AbstractQuery::getResultCacheId() now public

Bugs fixed:

- Doctrine\Common\Cache\AbstractCache::deleteByPrefix() was deleting _every_ key in cache

parent a8bcf0f7
...@@ -39,36 +39,6 @@ abstract class AbstractCache implements Cache ...@@ -39,36 +39,6 @@ abstract class AbstractCache implements Cache
/** @var string The namespace to prefix all cache ids with */ /** @var string The namespace to prefix all cache ids with */
private $_namespace = null; private $_namespace = null;
/** @var boolean Whether to manage cache keys or not. */
private $_manageCacheIds = false;
/**
* Sets whether cache keys should be managed by the cache driver
* separately from the cache entries. This allows more granular
* cache clearing through {@link deleteByPrefix}, {@link deleteByRegex},
* {@link deleteBySuffix} and some other operations such as {@link count}
* and {@link getIds}. Managing cache keys comes at the cost of a higher
* probability for cache slams due to the single cache key used for
* managing all other keys.
*
* @param boolean $bool
*/
public function setManageCacheIds($bool)
{
$this->_manageCacheIds = $bool;
}
/**
* Checks whether cache keys are managed by this cache driver.
*
* @return boolean
* @see setManageCacheIds()
*/
public function getManageCacheIds()
{
return $this->_manageCacheIds;
}
/** /**
* Set the namespace to prefix all cache ids with. * Set the namespace to prefix all cache ids with.
...@@ -103,14 +73,7 @@ abstract class AbstractCache implements Cache ...@@ -103,14 +73,7 @@ abstract class AbstractCache implements Cache
public function save($id, $data, $lifeTime = false) public function save($id, $data, $lifeTime = false)
{ {
$id = $this->_getNamespacedId($id); $id = $this->_getNamespacedId($id);
if ($this->_doSave($id, $data, $lifeTime)) { return $this->_doSave($id, $data, $lifeTime);
if ($this->_manageCacheIds) {
$this->_saveId($id);
}
return true;
}
return false;
} }
/** /**
...@@ -124,14 +87,7 @@ abstract class AbstractCache implements Cache ...@@ -124,14 +87,7 @@ abstract class AbstractCache implements Cache
return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/'); return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/');
} }
if ($this->_doDelete($id)) { return $this->_doDelete($id);
if ($this->_manageCacheIds) {
$this->_deleteId($id);
}
return true;
}
return false;
} }
/** /**
...@@ -141,7 +97,6 @@ abstract class AbstractCache implements Cache ...@@ -141,7 +97,6 @@ abstract class AbstractCache implements Cache
*/ */
public function deleteAll() public function deleteAll()
{ {
$this->_errorIfCacheIdsNotManaged();
$ids = $this->getIds(); $ids = $this->getIds();
foreach ($ids as $id) { foreach ($ids as $id) {
$this->delete($id); $this->delete($id);
...@@ -157,7 +112,6 @@ abstract class AbstractCache implements Cache ...@@ -157,7 +112,6 @@ abstract class AbstractCache implements Cache
*/ */
public function deleteByRegex($regex) public function deleteByRegex($regex)
{ {
$this->_errorIfCacheIdsNotManaged();
$deleted = array(); $deleted = array();
$ids = $this->getIds(); $ids = $this->getIds();
foreach ($ids as $id) { foreach ($ids as $id) {
...@@ -177,11 +131,10 @@ abstract class AbstractCache implements Cache ...@@ -177,11 +131,10 @@ abstract class AbstractCache implements Cache
*/ */
public function deleteByPrefix($prefix) public function deleteByPrefix($prefix)
{ {
$this->_errorIfCacheIdsNotManaged();
$deleted = array(); $deleted = array();
$ids = $this->getIds(); $ids = $this->getIds();
foreach ($ids as $id) { foreach ($ids as $id) {
if (strpos($id, $prefix) == 0) { if (strpos($id, $prefix) === 0) {
$this->delete($id); $this->delete($id);
$deleted[] = $id; $deleted[] = $id;
} }
...@@ -192,16 +145,15 @@ abstract class AbstractCache implements Cache ...@@ -192,16 +145,15 @@ abstract class AbstractCache implements Cache
/** /**
* Delete cache entries where the id has the passed suffix * Delete cache entries where the id has the passed suffix
* *
* @param string $suffix * @param string $suffix
* @return array $deleted Array of the deleted cache ids * @return array $deleted Array of the deleted cache ids
*/ */
public function deleteBySuffix($suffix) public function deleteBySuffix($suffix)
{ {
$this->_errorIfCacheIdsNotManaged();
$deleted = array(); $deleted = array();
$ids = $this->getIds(); $ids = $this->getIds();
foreach ($ids as $id) { foreach ($ids as $id) {
if (substr($id, -1 * strlen($suffix)) == $suffix) { if (substr($id, -1 * strlen($suffix)) === $suffix) {
$this->delete($id); $this->delete($id);
$deleted[] = $id; $deleted[] = $id;
} }
...@@ -209,30 +161,6 @@ abstract class AbstractCache implements Cache ...@@ -209,30 +161,6 @@ abstract class AbstractCache implements Cache
return $deleted; return $deleted;
} }
/**
* Count and return the number of cache entries.
*
* @return integer $count
*/
public function count()
{
$this->_errorIfCacheIdsNotManaged();
$ids = $this->getIds();
return $ids ? count($ids) : 0;
}
/**
* Get an array of all the cache ids stored
*
* @return array $ids
*/
public function getIds()
{
$this->_errorIfCacheIdsNotManaged();
$ids = $this->fetch($this->_cacheIdsIndexId);
return $ids ? $ids : array();
}
/** /**
* Prefix the passed id with the configured namespace value * Prefix the passed id with the configured namespace value
* *
...@@ -248,53 +176,9 @@ abstract class AbstractCache implements Cache ...@@ -248,53 +176,9 @@ abstract class AbstractCache implements Cache
} }
} }
/**
* Save a cache id to the index of cache ids
*
* @param string $id
* @return boolean TRUE if the id was successfully stored in the cache, FALSE otherwise.
*/
private function _saveId($id)
{
$ids = $this->getIds();
$ids[] = $id;
$cacheIdsIndexId = $this->_getNamespacedId($this->_cacheIdsIndexId);
return $this->_doSave($cacheIdsIndexId, $ids, null);
}
/**
* Delete a cache id from the index of cache ids
*
* @param string $id
* @return boolean TRUE if the entry was successfully removed from the cache, FALSE otherwise.
*/
private function _deleteId($id)
{
$ids = $this->getIds();
$key = array_search($id, $ids);
if ($key !== false) {
unset($ids[$key]);
$cacheIdsIndexId = $this->_getNamespacedId($this->_cacheIdsIndexId);
return $this->_doSave($cacheIdsIndexId, $ids, null);
}
return false;
}
/**
* @throws BadMethodCallException If the cache driver does not manage cache keys.
*/
private function _errorIfCacheIdsNotManaged()
{
if ( ! $this->_manageCacheIds) {
throw new \BadMethodCallException("Operation not supported if cache keys are not managed.");
}
}
/** /**
* Fetches an entry from the cache. * Fetches an entry from the cache.
* *
* @param string $id cache id The id of the cache entry to fetch. * @param string $id cache id The id of the cache entry to fetch.
* @return string The cached data or FALSE, if no cache entry exists for the given id. * @return string The cached data or FALSE, if no cache entry exists for the given id.
*/ */
...@@ -320,9 +204,16 @@ abstract class AbstractCache implements Cache ...@@ -320,9 +204,16 @@ abstract class AbstractCache implements Cache
/** /**
* Deletes a cache entry. * Deletes a cache entry.
* *
* @param string $id cache id * @param string $id cache id
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise. * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/ */
abstract protected function _doDelete($id); abstract protected function _doDelete($id);
/**
* Get an array of all the cache ids stored
*
* @return array $ids
*/
abstract public function getIds();
} }
\ No newline at end of file
...@@ -31,13 +31,28 @@ namespace Doctrine\Common\Cache; ...@@ -31,13 +31,28 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/ */
class ApcCache extends AbstractCache class ApcCache extends AbstractCache
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doFetch($id) public function getIds()
{
$ci = apc_cache_info('user');
$keys = array();
foreach ($ci['cache_list'] as $entry) {
$keys[] = $entry['info'];
}
return $keys;
}
/**
* {@inheritdoc}
*/
protected function _doFetch($id)
{ {
return apc_fetch($id); return apc_fetch($id);
} }
...@@ -45,7 +60,7 @@ class ApcCache extends AbstractCache ...@@ -45,7 +60,7 @@ class ApcCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doContains($id) protected function _doContains($id)
{ {
$found = false; $found = false;
apc_fetch($id, $found); apc_fetch($id, $found);
...@@ -63,7 +78,7 @@ class ApcCache extends AbstractCache ...@@ -63,7 +78,7 @@ class ApcCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doDelete($id) protected function _doDelete($id)
{ {
return apc_delete($id); return apc_delete($id);
} }
......
...@@ -31,19 +31,28 @@ namespace Doctrine\Common\Cache; ...@@ -31,19 +31,28 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/ */
class ArrayCache extends AbstractCache class ArrayCache extends AbstractCache
{ {
/** /**
* @var array $data * @var array $data
*/ */
private $data; private $data = array();
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doFetch($id) public function getIds()
{ {
return array_keys($this->data);
}
/**
* {@inheritdoc}
*/
protected function _doFetch($id)
{
if (isset($this->data[$id])) { if (isset($this->data[$id])) {
return $this->data[$id]; return $this->data[$id];
} }
......
...@@ -33,6 +33,7 @@ use \Memcache; ...@@ -33,6 +33,7 @@ use \Memcache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/ */
class MemcacheCache extends AbstractCache class MemcacheCache extends AbstractCache
{ {
...@@ -64,7 +65,28 @@ class MemcacheCache extends AbstractCache ...@@ -64,7 +65,28 @@ class MemcacheCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doFetch($id) public function getIds()
{
$keys = array();
$allSlabs = $this->_memcache->getExtendedStats('slabs');
foreach ($allSlabs as $server => $slabs) {
foreach (array_keys($slabs) as $slabId) {
$dump = $this->_memcache->getExtendedStats('cachedump', (int) $slabId);
foreach ($dump as $entries) {
if ($entries) {
$keys = array_merge($keys, array_keys($entries));
}
}
}
}
return $keys;
}
/**
* {@inheritdoc}
*/
protected function _doFetch($id)
{ {
return $this->_memcache->get($id); return $this->_memcache->get($id);
} }
...@@ -72,7 +94,7 @@ class MemcacheCache extends AbstractCache ...@@ -72,7 +94,7 @@ class MemcacheCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doContains($id) protected function _doContains($id)
{ {
return (bool) $this->_memcache->get($id); return (bool) $this->_memcache->get($id);
} }
...@@ -88,7 +110,7 @@ class MemcacheCache extends AbstractCache ...@@ -88,7 +110,7 @@ class MemcacheCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doDelete($id) protected function _doDelete($id)
{ {
return $this->_memcache->delete($id); return $this->_memcache->delete($id);
} }
......
...@@ -31,13 +31,32 @@ namespace Doctrine\Common\Cache; ...@@ -31,13 +31,32 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/ */
class XcacheCache extends AbstractCache class XcacheCache extends AbstractCache
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doFetch($id) public function getIds()
{
$this->_checkAuth();
$keys = array();
for ($i = 0, $count = xcache_count(XC_TYPE_VAR); $i < $count; $i++) {
$entries = xcache_list(XC_TYPE_VAR, $i);
if (is_array($entries['cache_list'])) {
foreach ($entries['cache_list'] as $entry) {
$keys[] = $entry['name'];
}
}
}
return $keys;
}
/**
* {@inheritdoc}
*/
protected function _doFetch($id)
{ {
return $this->_doContains($id) ? xcache_get($id) : false; return $this->_doContains($id) ? xcache_get($id) : false;
} }
...@@ -45,7 +64,7 @@ class XcacheCache extends AbstractCache ...@@ -45,7 +64,7 @@ class XcacheCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doContains($id) protected function _doContains($id)
{ {
return xcache_isset($id); return xcache_isset($id);
} }
...@@ -61,8 +80,22 @@ class XcacheCache extends AbstractCache ...@@ -61,8 +80,22 @@ class XcacheCache extends AbstractCache
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _doDelete($id) protected function _doDelete($id)
{
return xcache_unset($id);
}
/**
* Checks that xcache.admin.enable_auth is Off
*
* @throws \BadMethodCallException When xcache.admin.enable_auth is On
* @return void
*/
protected function _checkAuth()
{ {
return xcache_unset($id); if (ini_get('xcache.admin.enable_auth')) {
throw new \BadMethodCallException('To use all features of \Doctrine\Common\Cache\XcacheCache, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
}
} }
} }
\ No newline at end of file
...@@ -158,10 +158,10 @@ abstract class AbstractQuery ...@@ -158,10 +158,10 @@ abstract class AbstractQuery
} }
return $this->_params; return $this->_params;
} }
/** /**
* Gets a query parameter. * Gets a query parameter.
* *
* @param mixed $key The key (index or name) of the bound parameter. * @param mixed $key The key (index or name) of the bound parameter.
* @return mixed The value of the bound parameter. * @return mixed The value of the bound parameter.
*/ */
...@@ -178,7 +178,7 @@ abstract class AbstractQuery ...@@ -178,7 +178,7 @@ abstract class AbstractQuery
* @return string SQL query * @return string SQL query
*/ */
abstract public function getSql(); abstract public function getSql();
/** /**
* Sets a query parameter. * Sets a query parameter.
* *
...@@ -191,7 +191,7 @@ abstract class AbstractQuery ...@@ -191,7 +191,7 @@ abstract class AbstractQuery
$this->_params[$key] = $value; $this->_params[$key] = $value;
return $this; return $this;
} }
/** /**
* Sets a collection of query parameters. * Sets a collection of query parameters.
* *
...@@ -375,9 +375,9 @@ abstract class AbstractQuery ...@@ -375,9 +375,9 @@ abstract class AbstractQuery
/** /**
* Gets the single result of the query. * Gets the single result of the query.
* *
* Enforces the presence as well as the uniqueness of the result. * Enforces the presence as well as the uniqueness of the result.
* *
* If the result is not unique, a NonUniqueResultException is thrown. * If the result is not unique, a NonUniqueResultException is thrown.
* If there is no result, a NoResultException is thrown. * If there is no result, a NoResultException is thrown.
* *
...@@ -389,11 +389,11 @@ abstract class AbstractQuery ...@@ -389,11 +389,11 @@ abstract class AbstractQuery
public function getSingleResult($hydrationMode = null) public function getSingleResult($hydrationMode = null)
{ {
$result = $this->execute(array(), $hydrationMode); $result = $this->execute(array(), $hydrationMode);
if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) { if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
throw new NoResultException; throw new NoResultException;
} }
if (is_array($result)) { if (is_array($result)) {
if (count($result) > 1) { if (count($result) > 1) {
throw new NonUniqueResultException; throw new NonUniqueResultException;
...@@ -405,7 +405,7 @@ abstract class AbstractQuery ...@@ -405,7 +405,7 @@ abstract class AbstractQuery
} }
return $result->first(); return $result->first();
} }
return $result; return $result;
} }
...@@ -479,26 +479,26 @@ abstract class AbstractQuery ...@@ -479,26 +479,26 @@ abstract class AbstractQuery
if ($hydrationMode !== null) { if ($hydrationMode !== null) {
$this->_hydrationMode = $hydrationMode; $this->_hydrationMode = $hydrationMode;
} }
$params = $this->getParameters($params); $params = $this->getParameters($params);
if (isset($params[0])) { if (isset($params[0])) {
throw QueryException::invalidParameterPosition(0); throw QueryException::invalidParameterPosition(0);
} }
// Check result cache // Check result cache
if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) { if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) {
$id = $this->_getResultCacheId($params); $id = $this->getResultCacheId($params);
$cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id); $cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id);
if ($cached === false) { if ($cached === false) {
// Cache miss. // Cache miss.
$stmt = $this->_doExecute($params); $stmt = $this->_doExecute($params);
$result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll( $result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
$stmt, $this->_resultSetMapping, $this->_hints $stmt, $this->_resultSetMapping, $this->_hints
); );
$cacheDriver->save($id, $result, $this->_resultCacheTTL); $cacheDriver->save($id, $result, $this->_resultCacheTTL);
return $result; return $result;
...@@ -538,10 +538,10 @@ abstract class AbstractQuery ...@@ -538,10 +538,10 @@ abstract class AbstractQuery
* Will return the configured id if it exists otherwise a hash will be * Will return the configured id if it exists otherwise a hash will be
* automatically generated for you. * automatically generated for you.
* *
* @param array $params * @param array $params
* @return string $id * @return string $id
*/ */
protected function _getResultCacheId(array $params) public function getResultCacheId(array $params)
{ {
if ($this->_resultCacheId) { if ($this->_resultCacheId) {
return $this->_resultCacheId; return $this->_resultCacheId;
...@@ -552,11 +552,11 @@ abstract class AbstractQuery ...@@ -552,11 +552,11 @@ abstract class AbstractQuery
/** /**
* Prepares the given parameters for execution in an SQL statement. * Prepares the given parameters for execution in an SQL statement.
* *
* Note to inheritors: This method must return a numerically, continuously indexed array, * Note to inheritors: This method must return a numerically, continuously indexed array,
* starting with index 0 where the values (the parameter values) are in the order * starting with index 0 where the values (the parameter values) are in the order
* in which the parameters appear in the SQL query. * in which the parameters appear in the SQL query.
* *
* @return array The SQL parameter array. * @return array The SQL parameter array.
*/ */
abstract protected function _prepareParams(array $params); abstract protected function _prepareParams(array $params);
......
...@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\ApcCache; ...@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\ApcCache;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase class ApcCacheTest extends CacheTest
{ {
public function setUp() public function setUp()
{ {
...@@ -15,23 +15,8 @@ class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -15,23 +15,8 @@ class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase
} }
} }
public function testApcCacheDriver() protected function _getCacheDriver()
{ {
$cache = new ApcCache(); return new ApcCache();
// Test save
$cache->save('test_key', 'testing this out');
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
} }
} }
\ No newline at end of file
...@@ -6,32 +6,10 @@ use Doctrine\Common\Cache\ArrayCache; ...@@ -6,32 +6,10 @@ use Doctrine\Common\Cache\ArrayCache;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class ArrayCacheTest extends \Doctrine\Tests\DoctrineTestCase class ArrayCacheTest extends CacheTest
{ {
public function testArrayCacheDriver() protected function _getCacheDriver()
{ {
$cache = new ArrayCache(); return new ArrayCache();
$cache->setManageCacheIds(true);
// Test save
$cache->save('test_key', 'testing this out');
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test count
$this->assertEquals(1, $cache->count());
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
// Test delete all
$cache->deleteAll();
$this->assertEquals(0, $cache->count());
} }
} }
\ No newline at end of file
...@@ -2,80 +2,87 @@ ...@@ -2,80 +2,87 @@
namespace Doctrine\Tests\Common\Cache; namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\ArrayCache;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class CacheTest extends \Doctrine\Tests\DoctrineTestCase abstract class CacheTest extends \Doctrine\Tests\DoctrineTestCase
{ {
public function testCount() public function testBasics()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('test_key1', '1'); // Test save
$cache->save('test_key2', '2'); $cache->save('test_key', 'testing this out');
$this->assertEquals($cache->count(), 2);
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
} }
public function testDeleteAll() public function testDeleteAll()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('test_key1', '1'); $cache->save('test_key1', '1');
$cache->save('test_key2', '2'); $cache->save('test_key2', '2');
$cache->deleteAll(); $cache->deleteAll();
$this->assertEquals($cache->count(), 0); $this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
} }
public function testDeleteByRegex() public function testDeleteByRegex()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('test_key1', '1'); $cache->save('test_key1', '1');
$cache->save('test_key2', '2'); $cache->save('test_key2', '2');
$cache->deleteByRegex('/test_key[0-9]/'); $cache->deleteByRegex('/test_key[0-9]/');
$this->assertEquals($cache->count(), 0); $this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
} }
public function testDeleteByPrefix() public function testDeleteByPrefix()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('test_key1', '1'); $cache->save('test_key1', '1');
$cache->save('test_key2', '2'); $cache->save('test_key2', '2');
$cache->deleteByPrefix('test_key'); $cache->deleteByPrefix('test_key');
$this->assertEquals($cache->count(), 0); $this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
} }
public function testDeleteBySuffix() public function testDeleteBySuffix()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('1test_key', '1'); $cache->save('1test_key', '1');
$cache->save('2test_key', '2'); $cache->save('2test_key', '2');
$cache->deleteBySuffix('test_key'); $cache->deleteBySuffix('test_key');
$this->assertEquals($cache->count(), 0); $this->assertFalse($cache->contains('1test_key'));
$this->assertFalse($cache->contains('2test_key'));
} }
public function testDeleteByWildcard() public function testDeleteByWildcard()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->save('test_key1', '1'); $cache->save('test_key1', '1');
$cache->save('test_key2', '2'); $cache->save('test_key2', '2');
$cache->delete('test_key*'); $cache->delete('test_key*');
$this->assertEquals($cache->count(), 0); $this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
} }
public function testNamespace() public function testNamespace()
{ {
$cache = new ArrayCache(); $cache = $this->_getCacheDriver();
$cache->setManageCacheIds(true);
$cache->setNamespace('test_'); $cache->setNamespace('test_');
$cache->save('key1', 'test'); $cache->save('key1', 'test');
$this->assertTrue($cache->contains('key1')); $this->assertTrue($cache->contains('key1'));
...@@ -83,4 +90,6 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -83,4 +90,6 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase
$ids = $cache->getIds(); $ids = $cache->getIds();
$this->assertTrue(in_array('test_key1', $ids)); $this->assertTrue(in_array('test_key1', $ids));
} }
abstract protected function _getCacheDriver();
} }
\ No newline at end of file
...@@ -6,10 +6,10 @@ use Doctrine\Common\Cache\MemcacheCache; ...@@ -6,10 +6,10 @@ use Doctrine\Common\Cache\MemcacheCache;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase class MemcacheCacheTest extends CacheTest
{ {
private $_memcache; private $_memcache;
public function setUp() public function setUp()
{ {
if (extension_loaded('memcache')) { if (extension_loaded('memcache')) {
...@@ -23,23 +23,10 @@ class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -23,23 +23,10 @@ class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
} }
} }
public function testMemcacheCacheDriver() protected function _getCacheDriver()
{ {
$cache = new MemcacheCache(); $driver = new MemcacheCache();
$cache->setMemcache($this->_memcache); $driver->setMemcache($this->_memcache);
return $driver;
// Test save
$cache->save('test_key', 'testing this out');
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
} }
} }
\ No newline at end of file
...@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\XcacheCache; ...@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\XcacheCache;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase class XcacheCacheTest extends CacheTest
{ {
public function setUp() public function setUp()
{ {
...@@ -15,22 +15,8 @@ class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -15,22 +15,8 @@ class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
} }
} }
public function testXcacheCacheDriver() protected function _getCacheDriver()
{ {
$cache = new XcacheCache(); return new XcacheCache();
// Test save
$cache->save('test_key', 'testing this out');
// Test contains to test that save() worked
$this->assertTrue($cache->contains('test_key'));
// Test fetch
$this->assertEquals('testing this out', $cache->fetch('test_key'));
// Test delete
$cache->save('test_key2', 'test2');
$cache->delete('test_key2');
$this->assertFalse($cache->contains('test_key2'));
} }
} }
\ No newline at end of file
...@@ -22,7 +22,7 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -22,7 +22,7 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testQueryCache() public function testQueryCache()
{ {
$this->_em->getConfiguration()->setQueryCacheImpl(null); $this->_em->getConfiguration()->setQueryCacheImpl(null);
$user = new CmsUser; $user = new CmsUser;
$user->name = 'Roman'; $user->name = 'Roman';
$user->username = 'romanb'; $user->username = 'romanb';
...@@ -33,26 +33,22 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -33,26 +33,22 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$cache = new ArrayCache; $cache = new ArrayCache;
$cache->setManageCacheIds(true);
$query->setQueryCacheDriver($cache); $query->setQueryCacheDriver($cache);
$this->assertEquals(0, $cache->count());
$users = $query->getResult(); $users = $query->getResult();
$this->assertEquals(1, $cache->count()); $this->assertTrue($cache->contains(md5('select ux from Doctrine\Tests\Models\CMS\CmsUser uxDOCTRINE_QUERY_CACHE_SALT')));
$this->assertTrue($cache->contains(md5('select ux from Doctrine\Tests\Models\CMS\CmsUser uxDOCTRINE_QUERY_CACHE_SALT')));
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name); $this->assertEquals('Roman', $users[0]->name);
$this->_em->clear(); $this->_em->clear();
$query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$query2->setQueryCacheDriver($cache); $query2->setQueryCacheDriver($cache);
$users = $query2->getResult(); $users = $query2->getResult();
$this->assertEquals(1, $cache->count()); $this->assertTrue($cache->contains(md5('select ux from Doctrine\Tests\Models\CMS\CmsUser uxDOCTRINE_QUERY_CACHE_SALT')));
$this->assertTrue($cache->contains(md5('select ux from Doctrine\Tests\Models\CMS\CmsUser uxDOCTRINE_QUERY_CACHE_SALT')));
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name); $this->assertEquals('Roman', $users[0]->name);
} }
......
...@@ -31,24 +31,22 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -31,24 +31,22 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$cache = new ArrayCache; $cache = new ArrayCache;
$cache->setManageCacheIds(true);
$query->setResultCacheDriver($cache); $query->setResultCacheDriver($cache);
$this->assertEquals(0, $cache->count());
$users = $query->getResult(); $users = $query->getResult();
$this->assertEquals(1, $cache->count()); $this->assertTrue($cache->contains($query->getResultCacheId(array())));
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name); $this->assertEquals('Roman', $users[0]->name);
$this->_em->clear(); $this->_em->clear();
$query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); $query2 = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$query2->setResultCacheDriver($cache); $query2->setResultCacheDriver($cache);
$users = $query2->getResult(); $users = $query2->getResult();
$this->assertEquals(1, $cache->count()); $this->assertTrue($cache->contains($query->getResultCacheId(array())));
$this->assertEquals(1, count($users)); $this->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name); $this->assertEquals('Roman', $users[0]->name);
} }
......
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