From 6c9436abbf5df740539215354b2eaa1b6893d2cf Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Sun, 28 Jun 2009 01:00:42 +0000 Subject: #333171 by catch: Provide cache_get_multiple() and allow clearing of multiple cache IDs at once (with tests). --- modules/simpletest/tests/cache.test | 89 ++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) (limited to 'modules/simpletest') diff --git a/modules/simpletest/tests/cache.test b/modules/simpletest/tests/cache.test index 638747bfd..8ba647ba0 100644 --- a/modules/simpletest/tests/cache.test +++ b/modules/simpletest/tests/cache.test @@ -162,6 +162,56 @@ class CacheSavingCase extends CacheTestCase { } } +/** + * Test cache_get_multiple(). + */ +class CacheGetMultipleUnitTest extends CacheTestCase { + + public static function getInfo() { + return array( + 'name' => t('Fetching multiple cache items'), + 'description' => t('Confirm that multiple records are fetched correctly.'), + 'group' => t('Cache'), + ); + } + + function setUp() { + $this->default_bin = 'cache_page'; + parent::setUp(); + } + + /** + * Test cache_get_multiple(). + */ + function testCacheMultiple() { + $item1 = $this->randomName(10); + $item2 = $this->randomName(10); + cache_set('item1', $item1, $this->default_bin); + cache_set('item2', $item2, $this->default_bin); + $this->assertTrue($this->checkCacheExists('item1', $item1), t('Item 1 is cached.')); + $this->assertTrue($this->checkCacheExists('item2', $item2), t('Item 2 is cached.')); + + // Fetch both records from the database with cache_get_multiple(). + $item_ids = array('item1', 'item2'); + $items = cache_get_multiple($item_ids, $this->default_bin); + $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.')); + $this->assertEqual($items['item2']->data, $item2, t('Item was returned from cache successfully.')); + + // Remove one item from the cache. + cache_clear_all('item2', $this->default_bin); + + // Confirm that only one item is returned by cache_get_multiple(). + $item_ids = array('item1', 'item2'); + $items = cache_get_multiple($item_ids, $this->default_bin); + $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.')); + $this->assertFalse(isset($items['item2']), t('Item was not returned from the cache.')); + $this->assertTrue(count($items) == 1, t('Only valid cache entries returned.')); + } +} + +/** + * Test cache clearing methods. + */ class CacheClearCase extends CacheTestCase { public static function getInfo() { return array( @@ -224,4 +274,41 @@ class CacheClearCase extends CacheTestCase { || $this->checkCacheExists('test_cid_clear2', $this->default_value), t('Two caches removed after clearing cid substring with wildcard true.')); } -} \ No newline at end of file + + /** + * Test clearing using an array. + */ + function testClearArray() { + // Create three cache entries. + cache_set('test_cid_clear1', $this->default_value, $this->default_bin); + cache_set('test_cid_clear2', $this->default_value, $this->default_bin); + cache_set('test_cid_clear3', $this->default_value, $this->default_bin); + $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) + && $this->checkCacheExists('test_cid_clear2', $this->default_value) + && $this->checkCacheExists('test_cid_clear3', $this->default_value), + t('Three cache entries were created.')); + + // Clear two entries using an array. + cache_clear_all(array('test_cid_clear1', 'test_cid_clear2'), $this->default_bin); + $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) + || $this->checkCacheExists('test_cid_clear2', $this->default_value), + t('Two cache entries removed after clearing with an array.')); + + $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value), + t('Entry was not cleared from the cache')); + + // Set the cache clear threshold to 2 to confirm that the full bin is cleared + // when the threshold is exceeded. + variable_set('cache_clear_threshold', 2); + cache_set('test_cid_clear1', $this->default_value, $this->default_bin); + cache_set('test_cid_clear2', $this->default_value, $this->default_bin); + $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) + && $this->checkCacheExists('test_cid_clear2', $this->default_value), + t('Two cache entries were created.')); + cache_clear_all(array('test_cid_clear1', 'test_cid_clear2', 'test_cid_clear3'), $this->default_bin); + $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) + || $this->checkCacheExists('test_cid_clear2', $this->default_value) + || $this->checkCacheExists('test_cid_clear3', $this->default_value), + t('All cache entries removed when the array exceeded the cache clear threshold.')); + } +} -- cgit v1.2.3