diff --git a/includes/cache.inc b/includes/cache.inc index 8666874..00b9ce3 100644 --- a/includes/cache.inc +++ b/includes/cache.inc @@ -46,7 +46,18 @@ function _cache_get_object($bin) { * The cache or FALSE on failure. */ function cache_get($cid, $bin = 'cache') { - return _cache_get_object($bin)->get($cid); + // Check if the object exists in cache. + if ($cached = _cache_get_object($bin)->get($cid)) { + // Check if it is permanently or temporarily cached (no expiry), or has + // not expired yet. + if ($cached->expire == CACHE_PERMANENT + || $cached->expire == CACHE_TEMPORARY + || $cached->expire > REQUEST_TIME) { + return $cached; + } + } + // Either no cache object hit, or the returned object was expired. + return FALSE; } /** diff --git a/modules/simpletest/tests/cache.test b/modules/simpletest/tests/cache.test index 43d1fa1..60a357f 100644 --- a/modules/simpletest/tests/cache.test +++ b/modules/simpletest/tests/cache.test @@ -151,6 +151,23 @@ class CacheSavingCase extends CacheTestCase { $this->assertTrue(isset($cache->data) && $cache->data == $test_object, t('Object is saved and restored properly.')); } + /** + * Test that expired items are handled properly. + */ + function testExpired() { + $test_string = 'Drupal rocks!'; + + // Set an item in the cache to expire in the future + cache_set('expired_test_string1', $test_string, 'cache', time() + 3600); + // Item should be found in cache. + $this->assertCacheExists(t('Cached item with future expiry date returned by cache_get'), $test_string, 'expired_test_string1', 'cache'); + + // Set an item that expires in the past. + cache_set('expired_test_string2', $test_string, 'cache', time() - 3600); + // Cache item should now be expired + $this->assertFalse(cache_get('expired_test_string2'), t('Expired cache item not returned by cache_get')); + } + /* * Check or a variable is stored and restored properly. **/