diff --git a/includes/cache-install.inc b/includes/cache-install.inc index 3ab41410d16ad8ec3fe07d72a1cca5034a5ccb23..3be7588eba793ce10ca4b2408edc3964f1057256 100644 --- a/includes/cache-install.inc +++ b/includes/cache-install.inc @@ -28,5 +28,8 @@ function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) { function clear($cid = NULL, $wildcard = FALSE) { } - + + function isEmpty() { + return TRUE; + } } diff --git a/includes/cache.inc b/includes/cache.inc index e87e2f889d94c12f760e43eac58db9a461101222..6069ce004166d17369fe9a7f067499d1f74f8730 100644 --- a/includes/cache.inc +++ b/includes/cache.inc @@ -167,6 +167,21 @@ function cache_clear_all($cid = NULL, $bin = NULL, $wildcard = FALSE) { return _cache_get_object($bin)->clear($cid, $wildcard); } +/** + * Check if a cache bin is empty. + * + * A cache bin is considered empty if it does not contain any valid data for any + * cache ID. + * + * @param $bin + * The cache bin to check. + * @return + * TRUE if the cache bin specified is empty. + */ +function cache_is_empty($bin) { + return _cache_get_object($bin)->isEmpty(); +} + /** * Interface for cache implementations. * @@ -260,6 +275,17 @@ function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL); * match. If '*' is given as $cid, the bin $bin will be emptied. */ function clear($cid = NULL, $wildcard = FALSE); + + /** + * Check if a cache bin is empty. + * + * A cache bin is considered empty if it does not contain any valid data for + * any cache ID. + * + * @return + * TRUE if the cache bin specified is empty. + */ + function isEmpty(); } /** @@ -449,4 +475,14 @@ function clear($cid = NULL, $wildcard = FALSE) { } } } + + function isEmpty() { + $this->garbageCollection(); + $query = db_select($this->bin); + $query->addExpression('1'); + $result = $query->range(0, 1) + ->execute() + ->fetchField(); + return empty($result); + } } diff --git a/modules/simpletest/tests/cache.test b/modules/simpletest/tests/cache.test index 8de74b04e2f7a0843946e1e903231c7bb7ba58f0..0fc9fe298f2da278708722c26e2115f102538d73 100644 --- a/modules/simpletest/tests/cache.test +++ b/modules/simpletest/tests/cache.test @@ -312,3 +312,40 @@ class CacheClearCase extends CacheTestCase { t('All cache entries removed when the array exceeded the cache clear threshold.')); } } + +/** + * Test cache_is_empty() function. + */ +class CacheIsEmptyCase extends CacheTestCase { + public static function getInfo() { + return array( + 'name' => 'Cache emptiness test', + 'description' => 'Check if a cache bin is empty after performing clear operations.', + 'group' => 'Cache' + ); + } + + function setUp() { + $this->default_bin = 'cache_page'; + $this->default_value = $this->randomName(10); + + parent::setUp(); + } + + /** + * Test clearing using a cid. + */ + function testIsEmpty() { + // Clear the cache bin. + cache_clear_all('*', $this->default_bin); + $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty')); + // Add some data to the cache bin. + cache_set($this->default_cid, $this->default_value, $this->default_bin); + $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid); + $this->assertFalse(cache_is_empty($this->default_bin), t('The cache bin is not empty')); + // Remove the cached data. + cache_clear_all($this->default_cid, $this->default_bin); + $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid); + $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty')); + } +}