class ChainedFastBackendFactory

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php \Drupal\Core\Cache\ChainedFastBackendFactory
  2. 8.9.x core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php \Drupal\Core\Cache\ChainedFastBackendFactory
  3. 10 core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php \Drupal\Core\Cache\ChainedFastBackendFactory

Defines the chained fast cache backend factory.

Hierarchy

Expanded class hierarchy of ChainedFastBackendFactory

See also

\Drupal\Core\Cache\ChainedFastBackend

1 file declares its use of ChainedFastBackendFactory
ChainedFastBackendFactoryTest.php in core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendFactoryTest.php

File

core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php, line 14

Namespace

Drupal\Core\Cache
View source
class ChainedFastBackendFactory implements CacheFactoryInterface {
    
    /**
     * The service name of the consistent backend factory.
     *
     * @var string
     */
    protected $consistentServiceName;
    
    /**
     * The service name of the fast backend factory.
     *
     * @var string
     */
    protected $fastServiceName;
    
    /**
     * The service container.
     */
    protected ContainerInterface $container;
    
    /**
     * Sets the service container.
     */
    public function setContainer(ContainerInterface $container) : void {
        $this->container = $container;
    }
    
    /**
     * Constructs ChainedFastBackendFactory object.
     *
     * @param \Drupal\Core\Site\Settings|null $settings
     *   (optional) The settings object.
     * @param string|null $consistent_service_name
     *   (optional) The service name of the consistent backend factory. Defaults
     *   to:
     *   - $settings->get('cache')['default'] (if specified)
     *   - 'cache.backend.database' (if the above isn't specified)
     * @param string|null $fast_service_name
     *   (optional) The service name of the fast backend factory. Defaults to:
     *   - 'cache.backend.apcu' (if the PHP process has APCu enabled)
     *   - NULL (if the PHP process doesn't have APCu enabled)
     */
    public function __construct(?Settings $settings = NULL, $consistent_service_name = NULL, $fast_service_name = NULL) {
        // Default the consistent backend to the site's default backend.
        if (!isset($consistent_service_name)) {
            $cache_settings = isset($settings) ? $settings->get('cache') : [];
            $consistent_service_name = $cache_settings['default'] ?? 'cache.backend.database';
        }
        // Default the fast backend to APCu if it's available.
        if (!isset($fast_service_name) && function_exists('apcu_fetch')) {
            $fast_service_name = 'cache.backend.apcu';
        }
        $this->consistentServiceName = $consistent_service_name;
        // Do not use the fast chained backend during installation. In those cases,
        // we expect many cache invalidations and writes, the fast chained cache
        // backend performs badly in such a scenario.
        if (!InstallerKernel::installationAttempted()) {
            $this->fastServiceName = $fast_service_name;
        }
    }
    
    /**
     * Instantiates a chained, fast cache backend class for a given cache bin.
     *
     * @param string $bin
     *   The cache bin for which a cache backend object should be returned.
     *
     * @return \Drupal\Core\Cache\CacheBackendInterface
     *   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 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), $bin);
        }
        else {
            return $this->container
                ->get($this->consistentServiceName)
                ->get($bin);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ChainedFastBackendFactory::$consistentServiceName protected property The service name of the consistent backend factory.
ChainedFastBackendFactory::$container protected property The service container.
ChainedFastBackendFactory::$fastServiceName protected property The service name of the fast backend factory.
ChainedFastBackendFactory::get public function Instantiates a chained, fast cache backend class for a given cache bin. Overrides CacheFactoryInterface::get
ChainedFastBackendFactory::setContainer public function Sets the service container.
ChainedFastBackendFactory::__construct public function Constructs ChainedFastBackendFactory object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.