Commit 6f4f3a88 authored by catch's avatar catch
Browse files

Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx,...

Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx, kristiaanvandeneynde: Ensure invalid items are not written to FastBackend in ChainedFast

(cherry picked from commit e6b0b85e)
parent c813f52a
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -166,9 +166,11 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) {
    if ($cids) {
      foreach ($this->consistentBackend->getMultiple($cids, $allow_invalid) as $item) {
        $cache[$item->cid] = $item;
        if (!$allow_invalid || $item->valid) {
          $this->fastBackend->set($item->cid, $item->data, $item->expire, $item->tags);
        }
      }
    }

    return $cache;
  }
+42 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\Core\Cache;

use Drupal\Component\Datetime\Time;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\ChainedFastBackend;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Tests\UnitTestCase;
@@ -61,6 +62,47 @@ public function testGetDoesNotHitConsistentBackend(): void {
    $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
  }

  /**
   * Tests a get() on consistent backend without saving on fast backend.
   */
  public function testSetInvalidDataFastBackend(): void {
    $cid = $this->randomString();
    $item = (object) [
      'cid' => $cid,
      'data' => serialize($this->randomObject()),
      'created' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
      'expire' => Cache::PERMANENT,
      'tags' => [],
      'valid' => FALSE,
    ];

    $consistent_cache = $this->createMock('Drupal\Core\Cache\CacheBackendInterface');

    $consistent_cache->expects($this->once())
      ->method('get')
      ->withAnyParameters()
      ->willReturn(FALSE);
    $consistent_cache->expects($this->once())
      ->method('getMultiple')
      ->withAnyParameters()
      ->willReturn([$item]);

    $fast_cache = new MemoryBackend(new Time());

    $chained_fast_backend = new ChainedFastBackend(
      $consistent_cache,
      $fast_cache,
      'foo'
    );

    // Perform a get using the allowing invalid data parameter.
    $this->assertEquals($item, $chained_fast_backend->get($cid, TRUE));

    // Perform a get directly on the fast cache to guarantee the invalid data
    // were not saved there.
    $this->assertEquals(NULL, $fast_cache->get($cid), 'Invalid data was not saved on the fast cache.');
  }

  /**
   * Tests a fast cache miss gets data from the consistent cache backend.
   */