You are here

class ChainedFastRawBackendFactory in Supercache 8

Same name and namespace in other branches
  1. 2.0.x src/Cache/ChainedFastRawBackendFactory.php \Drupal\supercache\Cache\ChainedFastRawBackendFactory

Defines the chained fast cache backend factory.

Hierarchy

Expanded class hierarchy of ChainedFastRawBackendFactory

2 files declare their use of ChainedFastRawBackendFactory
CacheCacheTagsChecksumTests.php in src/Tests/Cache/CacheCacheTagsChecksumTests.php
CacheServicesTrait.php in src/Tests/Generic/Cache/CacheServicesTrait.php
1 string reference to 'ChainedFastRawBackendFactory'
supercache.services.yml in ./supercache.services.yml
supercache.services.yml
1 service uses ChainedFastRawBackendFactory
cache.rawbackend.chainedfast in ./supercache.services.yml
Drupal\supercache\Cache\ChainedFastRawBackendFactory

File

src/Cache/ChainedFastRawBackendFactory.php, line 24
Contains \Drupal\supercache\Cache\ChainedFastBackendFactory.

Namespace

Drupal\supercache\Cache
View source
class ChainedFastRawBackendFactory implements CacheRawFactoryInterface, EventSubscriberInterface {
  use ContainerAwareTrait;

  /**
   * The service name of the consistent backend factory.
   *
   * @var string
   */
  protected $consistentServiceName;

  /**
   * The service name of the fast backend factory.
   *
   * @var string
   */
  protected $fastServiceName;

  /**
   * Cache binary instances.
   *
   * @var ChainedFastRawBackend[]
   */
  protected $caches;

  /**
   * Track wether the Kernel is terminated.
   *
   * @var bool
   */
  protected $kernel_terminated;

  /**
   * Constructs ChainedFastBackendFactory object.
   *
   * @param 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('rawcache') : array();
      $consistent_service_name = isset($cache_settings['default']) ? $cache_settings['default'] : 'cache.rawbackend.database';
    }

    // Default the fast backend to APCu if it's available.
    if (!isset($fast_service_name) && function_exists('apcu_fetch')) {
      $fast_service_name = 'cache.rawbackend.apcu';
    }
    $this->consistentServiceName = $consistent_service_name;
    $this->fastServiceName = $fast_service_name;
    $this->caches = [];
    $this->kernel_terminated = FALSE;
  }

  /**
   * 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 CacheRawBackendInterface
   *   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)) {
      return new ChainedFastRawBackend($this->container
        ->get($this->consistentServiceName)
        ->get($bin), $this->container
        ->get($this->fastServiceName)
        ->get($bin), !$this->kernel_terminated);
    }
    else {
      return $this->container
        ->get($this->consistentServiceName)
        ->get($bin);
    }
  }

  /**
   * Shutdown functions.
   *
   * Using __destruct() proved to be problematic
   * with some some cache backends such as couchbase
   * with custom transcoders or the Drupal.org
   * test bot.
   *
   */
  public function onKernelTerminate(PostResponseEvent $event) {
    foreach ($this->caches as $cache) {
      $cache
        ->onKernelTerminate();
    }
    $this->kernel_terminated = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::TERMINATE][] = array(
      'onKernelTerminate',
      -100,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChainedFastRawBackendFactory::$caches protected property Cache binary instances.
ChainedFastRawBackendFactory::$consistentServiceName protected property The service name of the consistent backend factory.
ChainedFastRawBackendFactory::$fastServiceName protected property The service name of the fast backend factory.
ChainedFastRawBackendFactory::$kernel_terminated protected property Track wether the Kernel is terminated.
ChainedFastRawBackendFactory::get public function Instantiates a chained, fast cache backend class for a given cache bin. Overrides CacheRawFactoryInterface::get
ChainedFastRawBackendFactory::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ChainedFastRawBackendFactory::onKernelTerminate public function Shutdown functions.
ChainedFastRawBackendFactory::__construct public function Constructs ChainedFastBackendFactory object.