Commit 91247936 authored by catch's avatar catch
Browse files

Issue #2662844 by gapple, yogeshmpawar, penyaskito, dawehner, catch,...

Issue #2662844 by gapple, yogeshmpawar, penyaskito, dawehner, catch, smustgrave, alexpott, chx: cache.backend.chainedfast misbehaves when both fast and consistent backends point to the same storage
parent 9857cab4
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -76,9 +76,14 @@ public function __construct(Settings $settings = NULL, $consistent_service_name
   *   The cache backend object associated with the specified bin.
   */
  public function get($bin) {
    // Use the chained backend only if there is a fast backend available;
    // otherwise, just return the consistent backend directly.
    if (isset($this->fastServiceName)) {
    // Use the chained backend only if there is a fast backend available and it
    // is not the same as the consistent backend; otherwise, just return the
    // consistent backend directly.
    if (
      isset($this->fastServiceName)
      &&
      $this->fastServiceName !== $this->consistentServiceName
    ) {
      return new ChainedFastBackend(
        $this->container->get($this->consistentServiceName)->get($bin),
        $this->container->get($this->fastServiceName)->get($bin),
+84 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Core\Cache;

use Drupal\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\Core\Cache\ChainedFastBackend;
use Drupal\Core\Cache\ChainedFastBackendFactory;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\Core\Cache\ChainedFastBackendFactory
 * @group Cache
 */
class ChainedFastBackendFactoryTest extends UnitTestCase {

  /**
   * Test if the same name is provided for consistent and fast services.
   */
  public function testIdenticalService() {
    $container = $this->createMock(ContainerInterface::class);
    $testCacheFactory = $this->createMock(CacheFactoryInterface::class);
    $testCacheBackend = $this->createMock(CacheBackendInterface::class);

    $container->expects($this->once())
      ->method('get')
      ->with('cache.backend.test')
      ->willReturn($testCacheFactory);

    $testCacheFactory->expects($this->once())
      ->method('get')
      ->with('test_bin')
      ->willReturn($testCacheBackend);

    $cacheFactory = new ChainedFastBackendFactory(NULL, 'cache.backend.test', 'cache.backend.test');
    $cacheFactory->setContainer($container);

    $cacheBackend = $cacheFactory->get('test_bin');

    // The test backend should be returned directly.
    $this->assertSame($testCacheBackend, $cacheBackend);
  }

  /**
   * Test if different names are provided for consistent and fast services.
   */
  public function testDifferentServices() {
    $container = $this->createMock(ContainerInterface::class);
    $testConsistentCacheFactory = $this->createMock(CacheFactoryInterface::class);
    $testFastCacheFactory = $this->createMock(CacheFactoryInterface::class);
    $testConsistentCacheBackend = $this->createMock(CacheBackendInterface::class);
    $testFastCacheBackend = $this->createMock(CacheBackendInterface::class);

    $container->expects($this->exactly(2))
      ->method('get')
      ->will(
        $this->returnCallback(function ($service) use ($testFastCacheFactory, $testConsistentCacheFactory) {
          return match ($service) {
            'cache.backend.test_consistent' => $testConsistentCacheFactory,
            'cache.backend.test_fast' => $testFastCacheFactory,
          };
        })
      );

    // The same bin should be retrieved from both backends.
    $testConsistentCacheFactory->expects($this->once())
      ->method('get')
      ->with('test_bin')
      ->willReturn($testConsistentCacheBackend);
    $testFastCacheFactory->expects($this->once())
      ->method('get')
      ->with('test_bin')
      ->willReturn($testFastCacheBackend);

    $cacheFactory = new ChainedFastBackendFactory(NULL, 'cache.backend.test_consistent', 'cache.backend.test_fast');
    $cacheFactory->setContainer($container);

    // A wrapping ChainedFastBackend should be returned.
    $cacheBackend = $cacheFactory->get('test_bin');
    $this->assertInstanceOf(ChainedFastBackend::class, $cacheBackend);
  }

}