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
......@@ -40,36 +40,6 @@ abstract class AbstractCache implements Cache
/** @var string The namespace to prefix all cache ids with */
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.
*
......@@ -103,14 +73,7 @@ abstract class AbstractCache implements Cache
public function save($id, $data, $lifeTime = false)
{
$id = $this->_getNamespacedId($id);
if ($this->_doSave($id, $data, $lifeTime)) {
if ($this->_manageCacheIds) {
$this->_saveId($id);
}
return true;
}
return false;
return $this->_doSave($id, $data, $lifeTime);
}
/**
......@@ -124,14 +87,7 @@ abstract class AbstractCache implements Cache
return $this->deleteByRegex('/' . str_replace('*', '.*', $id) . '/');
}
if ($this->_doDelete($id)) {
if ($this->_manageCacheIds) {
$this->_deleteId($id);
}
return true;
}
return false;
return $this->_doDelete($id);
}
/**
......@@ -141,7 +97,6 @@ abstract class AbstractCache implements Cache
*/
public function deleteAll()
{
$this->_errorIfCacheIdsNotManaged();
$ids = $this->getIds();
foreach ($ids as $id) {
$this->delete($id);
......@@ -157,7 +112,6 @@ abstract class AbstractCache implements Cache
*/
public function deleteByRegex($regex)
{
$this->_errorIfCacheIdsNotManaged();
$deleted = array();
$ids = $this->getIds();
foreach ($ids as $id) {
......@@ -177,11 +131,10 @@ abstract class AbstractCache implements Cache
*/
public function deleteByPrefix($prefix)
{
$this->_errorIfCacheIdsNotManaged();
$deleted = array();
$ids = $this->getIds();
foreach ($ids as $id) {
if (strpos($id, $prefix) == 0) {
if (strpos($id, $prefix) === 0) {
$this->delete($id);
$deleted[] = $id;
}
......@@ -197,11 +150,10 @@ abstract class AbstractCache implements Cache
*/
public function deleteBySuffix($suffix)
{
$this->_errorIfCacheIdsNotManaged();
$deleted = array();
$ids = $this->getIds();
foreach ($ids as $id) {
if (substr($id, -1 * strlen($suffix)) == $suffix) {
if (substr($id, -1 * strlen($suffix)) === $suffix) {
$this->delete($id);
$deleted[] = $id;
}
......@@ -209,30 +161,6 @@ abstract class AbstractCache implements Cache
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
*
......@@ -248,50 +176,6 @@ 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.
*
......@@ -325,4 +209,11 @@ abstract class AbstractCache implements Cache
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
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,9 +31,24 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/
class ApcCache extends AbstractCache
{
/**
* {@inheritdoc}
*/
public function getIds()
{
$ci = apc_cache_info('user');
$keys = array();
foreach ($ci['cache_list'] as $entry) {
$keys[] = $entry['info'];
}
return $keys;
}
/**
* {@inheritdoc}
*/
......
......@@ -31,13 +31,22 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/
class ArrayCache extends AbstractCache
{
/**
* @var array $data
*/
private $data;
private $data = array();
/**
* {@inheritdoc}
*/
public function getIds()
{
return array_keys($this->data);
}
/**
* {@inheritdoc}
......
......@@ -33,6 +33,7 @@ use \Memcache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/
class MemcacheCache extends AbstractCache
{
......@@ -61,6 +62,27 @@ class MemcacheCache extends AbstractCache
return $this->_memcache;
}
/**
* {@inheritdoc}
*/
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}
*/
......
......@@ -31,9 +31,28 @@ namespace Doctrine\Common\Cache;
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author David Abdemoulaie <dave@hobodave.com>
*/
class XcacheCache extends AbstractCache
{
/**
* {@inheritdoc}
*/
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}
*/
......@@ -65,4 +84,18 @@ class XcacheCache extends AbstractCache
{
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()
{
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
......@@ -488,7 +488,7 @@ abstract class AbstractQuery
// Check result cache
if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) {
$id = $this->_getResultCacheId($params);
$id = $this->getResultCacheId($params);
$cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id);
if ($cached === false) {
......@@ -541,7 +541,7 @@ abstract class AbstractQuery
* @param array $params
* @return string $id
*/
protected function _getResultCacheId(array $params)
public function getResultCacheId(array $params)
{
if ($this->_resultCacheId) {
return $this->_resultCacheId;
......
......@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\ApcCache;
require_once __DIR__ . '/../../TestInit.php';
class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase
class ApcCacheTest extends CacheTest
{
public function setUp()
{
......@@ -15,23 +15,8 @@ class ApcCacheTest extends \Doctrine\Tests\DoctrineTestCase
}
}
public function testApcCacheDriver()
protected function _getCacheDriver()
{
$cache = 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'));
return new ApcCache();
}
}
\ No newline at end of file
......@@ -6,32 +6,10 @@ use Doctrine\Common\Cache\ArrayCache;
require_once __DIR__ . '/../../TestInit.php';
class ArrayCacheTest extends \Doctrine\Tests\DoctrineTestCase
class ArrayCacheTest extends CacheTest
{
public function testArrayCacheDriver()
protected function _getCacheDriver()
{
$cache = 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());
return new ArrayCache();
}
}
\ No newline at end of file
......@@ -2,80 +2,87 @@
namespace Doctrine\Tests\Common\Cache;
use Doctrine\Common\Cache\ArrayCache;
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->setManageCacheIds(true);
$cache->save('test_key1', '1');
$cache->save('test_key2', '2');
$this->assertEquals($cache->count(), 2);
$cache = $this->_getCacheDriver();
// 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'));
}
public function testDeleteAll()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->save('test_key1', '1');
$cache->save('test_key2', '2');
$cache->deleteAll();
$this->assertEquals($cache->count(), 0);
$this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
}
public function testDeleteByRegex()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->save('test_key1', '1');
$cache->save('test_key2', '2');
$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()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->save('test_key1', '1');
$cache->save('test_key2', '2');
$cache->deleteByPrefix('test_key');
$this->assertEquals($cache->count(), 0);
$this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
}
public function testDeleteBySuffix()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->save('1test_key', '1');
$cache->save('2test_key', '2');
$cache->deleteBySuffix('test_key');
$this->assertEquals($cache->count(), 0);
$this->assertFalse($cache->contains('1test_key'));
$this->assertFalse($cache->contains('2test_key'));
}
public function testDeleteByWildcard()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->save('test_key1', '1');
$cache->save('test_key2', '2');
$cache->delete('test_key*');
$this->assertEquals($cache->count(), 0);
$this->assertFalse($cache->contains('test_key1'));
$this->assertFalse($cache->contains('test_key2'));
}
public function testNamespace()
{
$cache = new ArrayCache();
$cache->setManageCacheIds(true);
$cache = $this->_getCacheDriver();
$cache->setNamespace('test_');
$cache->save('key1', 'test');
$this->assertTrue($cache->contains('key1'));
......@@ -83,4 +90,6 @@ class CacheTest extends \Doctrine\Tests\DoctrineTestCase
$ids = $cache->getIds();
$this->assertTrue(in_array('test_key1', $ids));
}
abstract protected function _getCacheDriver();
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\MemcacheCache;
require_once __DIR__ . '/../../TestInit.php';
class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
class MemcacheCacheTest extends CacheTest
{
private $_memcache;
......@@ -23,23 +23,10 @@ class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
}
}
public function testMemcacheCacheDriver()
protected function _getCacheDriver()
{
$cache = new MemcacheCache();
$cache->setMemcache($this->_memcache);
// 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'));
$driver = new MemcacheCache();
$driver->setMemcache($this->_memcache);
return $driver;
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ use Doctrine\Common\Cache\XcacheCache;
require_once __DIR__ . '/../../TestInit.php';
class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
class XcacheCacheTest extends CacheTest
{
public function setUp()
{
......@@ -15,22 +15,8 @@ class XcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
}
}
public function testXcacheCacheDriver()
protected function _getCacheDriver()
{
$cache = 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'));
return new XcacheCache();
}
}
\ No newline at end of file
......@@ -33,13 +33,10 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$cache = new ArrayCache;
$cache->setManageCacheIds(true);
$query->setQueryCacheDriver($cache);
$this->assertEquals(0, $cache->count());
$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->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name);
......@@ -51,7 +48,6 @@ class QueryCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$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->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name);
......
......@@ -31,13 +31,11 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux');
$cache = new ArrayCache;
$cache->setManageCacheIds(true);
$query->setResultCacheDriver($cache);
$this->assertEquals(0, $cache->count());
$users = $query->getResult();
$this->assertEquals(1, $cache->count());
$this->assertTrue($cache->contains($query->getResultCacheId(array())));
$this->assertEquals(1, count($users));
$this->assertEquals('Roman', $users[0]->name);
......@@ -48,7 +46,7 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase
$users = $query2->getResult();
$this->assertEquals(1, $cache->count());
$this->assertTrue($cache->contains($query->getResultCacheId(array())));
$this->assertEquals(1, count($users));
$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