diff --git a/core/lib/Drupal/Core/Cache/CacheCollector.php b/core/lib/Drupal/Core/Cache/CacheCollector.php index 7ecbd6d..afc6922 100644 --- a/core/lib/Drupal/Core/Cache/CacheCollector.php +++ b/core/lib/Drupal/Core/Cache/CacheCollector.php @@ -220,7 +220,6 @@ protected function updateCache($lock = TRUE) { } // Lock cache writes to help avoid stampedes. - // To implement locking for cache misses, override __construct(). $lock_name = $this->cid . ':' . __CLASS__; if (!$lock || $this->lock->acquire($lock_name)) { // Set and delete operations invalidate the cache item. Try to also load @@ -268,7 +267,12 @@ public function reset() { */ public function clear() { $this->reset(); - $this->cache->delete($this->cid); + if ($this->tags) { + $this->cache->deleteTags($this->tags); + } + else { + $this->cache->delete($this->cid); + } } /** diff --git a/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php b/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php index c400400..f66024e 100644 --- a/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php +++ b/core/modules/locale/tests/Drupal/locale/Tests/LocaleTranslationTest.php @@ -38,13 +38,15 @@ public static function getInfo() { */ protected function setUp() { $this->storage = $this->getMock('Drupal\locale\StringStorageInterface'); + $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); } /** * Tests for \Drupal\locale\LocaleTranslation::destruct() */ public function testDestruct() { - $translation = new LocaleTranslation($this->storage); + $translation = new LocaleTranslation($this->storage, $this->cache, $this->lock); // Prove that destruction works without errors when translations are empty. $this->assertAttributeEmpty('translations', $translation); $translation->destruct(); diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php index c3cc755..702c7c7 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php @@ -105,6 +105,12 @@ public function testSetAndGetNull() { $this->collector->set($key, $value); $this->assertTrue($this->collector->has($key)); $this->assertEquals($value, $this->collector->get($key)); + + // Ensure that getting a value that isn't set does not mark it as + // existent. + $non_existing_key = $this->randomName(7); + $this->collector->get($non_existing_key); + $this->assertFalse($this->collector->has($non_existing_key)); } /** @@ -152,7 +158,6 @@ public function testDelete() { * Tests updating the cache when no changes were made. */ public function testUpdateCacheNoChanges() { - $this->lock->expects($this->never()) ->method('acquire'); $this->cache->expects($this->never()) @@ -217,7 +222,7 @@ public function testUpdateCacheLockFail() { } /** - * Tests updating the cache when there is a + * Tests updating the cache when there is a conflict after cache invalidation. */ public function testUpdateCacheInvalidatedConflict() { $key = $this->randomName(); @@ -380,6 +385,36 @@ public function testUpdateCacheClear() { $this->cache->expects($this->once()) ->method('delete') ->with($this->cid); + $this->cache->expects($this->never()) + ->method('deleteTags'); + $this->collector->clear(); + $this->assertEquals($value, $this->collector->get($key)); + $this->assertEquals(2, $this->collector->getCacheMisses()); + } + + /** + * Tests a clear of the cache collector using tags. + */ + public function testUpdateCacheClearTags() { + $key = $this->randomName(); + $value = $this->randomName(); + $tags = array($this->randomName() => TRUE); + $this->collector = new CacheCollectorHelper($this->cid, $this->cache, $this->lock, $tags); + + // Set the data and request it. + $this->collector->setCacheMissData($key, $value); + $this->assertEquals($value, $this->collector->get($key)); + $this->assertEquals($value, $this->collector->get($key)); + + // Should have been added to the storage and only be requested once. + $this->assertEquals(1, $this->collector->getCacheMisses()); + + // Clear the collected cache using the tags, should call it again. + $this->cache->expects($this->never()) + ->method('delete'); + $this->cache->expects($this->once()) + ->method('deleteTags') + ->with($tags); $this->collector->clear(); $this->assertEquals($value, $this->collector->get($key)); $this->assertEquals(2, $this->collector->getCacheMisses());