class CacheDecorator

Enables static and persistent caching of discovered plugin definitions.

Hierarchy

Expanded class hierarchy of CacheDecorator

21 files declare their use of CacheDecorator
AggregatorPluginManager.php in drupal/core/modules/aggregator/lib/Drupal/aggregator/Plugin/AggregatorPluginManager.php
Contains \Drupal\aggregator\Plugin\AggregatorPluginManager.
ArchiverManager.php in drupal/core/lib/Drupal/Core/Archiver/ArchiverManager.php
BlockManager.php in drupal/core/modules/block/lib/Drupal/block/Plugin/Type/BlockManager.php
CacheDecoratorTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php
Contains \Drupal\system\Tests\Plugin\CacheDecoratorTest.
CachedMockBlockManager.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/CachedMockBlockManager.php
Contains Drupal\plugin_test\Plugin\CachedMockBlockManager.

... See full list

1 string reference to 'CacheDecorator'
CacheDecoratorTest::getInfo in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php

File

drupal/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php, line 17
Definition of Drupal\Core\Plugin\Discovery\CacheDecorator.

Namespace

Drupal\Core\Plugin\Discovery
View source
class CacheDecorator implements CachedDiscoveryInterface {

  /**
   * The cache key used to store the definition list.
   *
   * @var string
   */
  protected $cacheKey;

  /**
   * The cache bin used to store the definition list.
   *
   * @var string
   */
  protected $cacheBin;

  /**
   * The timestamp indicating when the definition list cache expires.
   *
   * @var int
   */
  protected $cacheExpire;

  /**
   * The cache tags associated with the definition list.
   *
   * @var array
   */
  protected $cacheTags;

  /**
   * The plugin definitions of the decorated discovery class.
   *
   * @var array
   */
  protected $definitions;

  /**
   * The Discovery object being decorated.
   *
   * @var Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $decorated;

  /**
   * Constructs a Drupal\Core\Plugin\Discovery\CacheDecorator object.
   *
   * It uses the DiscoveryInterface object it should decorate.
   *
   * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
   *   The object implementing DiscoveryInterface that is being decorated.
   * @param string $cache_key
   *   The cache identifier used for storage of the definition list.
   * @param string $cache_bin
   *   The cache bin used for storage and retrieval of the definition list.
   * @param int $cache_expire
   *   A Unix timestamp indicating that the definition list will be considered
   *   invalid after this time.
   * @param array $cache_tags
   *   The cache tags associated with the definition list.
   */
  public function __construct(DiscoveryInterface $decorated, $cache_key, $cache_bin = 'cache', $cache_expire = CacheBackendInterface::CACHE_PERMANENT, array $cache_tags = array()) {
    $this->decorated = $decorated;
    $this->cacheKey = $cache_key;
    $this->cacheBin = $cache_bin;
    $this->cacheExpire = $cache_expire;
    $this->cacheTags = $cache_tags;
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition().
   */
  public function getDefinition($plugin_id) {

    // Optimize for fast access to definitions if they are already in memory.
    if (isset($this->definitions)) {

      // Avoid using a ternary that would create a copy of the array.
      if (isset($this->definitions[$plugin_id])) {
        return $this->definitions[$plugin_id];
      }
      else {
        return;
      }
    }
    $definitions = $this
      ->getDefinitions();

    // Avoid using a ternary that would create a copy of the array.
    if (isset($definitions[$plugin_id])) {
      return $definitions[$plugin_id];
    }
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
   */
  public function getDefinitions() {

    // Optimize for fast access to definitions if they are already in memory.
    if (isset($this->definitions)) {
      return $this->definitions;
    }
    $definitions = $this
      ->getCachedDefinitions();
    if (!isset($definitions)) {
      $definitions = $this->decorated
        ->getDefinitions();
      $this
        ->setCachedDefinitions($definitions);
    }
    return $definitions;
  }

  /**
   * Returns the cached plugin definitions of the decorated discovery class.
   *
   * @return mixed
   *   On success this will return an array of plugin definitions. On failure
   *   this should return NULL, indicating to other methods that this has not
   *   yet been defined. Success with no values should return as an empty array
   *   and would actually be returned by the getDefinitions() method.
   */
  protected function getCachedDefinitions() {
    if (!isset($this->definitions) && isset($this->cacheKey) && ($cache = cache($this->cacheBin)
      ->get($this->cacheKey))) {
      $this->definitions = $cache->data;
    }
    return $this->definitions;
  }

  /**
   * Sets a cache of plugin definitions for the decorated discovery class.
   *
   * @param array $definitions
   *   List of definitions to store in cache.
   */
  protected function setCachedDefinitions($definitions) {
    if (isset($this->cacheKey)) {
      cache($this->cacheBin)
        ->set($this->cacheKey, $definitions, $this->cacheExpire, $this->cacheTags);
    }
    $this->definitions = $definitions;
  }

  /**
   * Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions().
   */
  public function clearCachedDefinitions() {

    // If there are any cache tags, clear cache based on those.
    if (!empty($this->cacheTags)) {
      cache($this->cacheBin)
        ->deleteTags($this->cacheTags);
    }
    else {
      if (isset($this->cacheKey)) {
        cache($this->cacheBin)
          ->delete($this->cacheKey);
      }
    }
    $this->definitions = NULL;
  }

  /**
   * Passes through all unknown calls onto the decorated object.
   */
  public function __call($method, $args) {
    return call_user_func_array(array(
      $this->decorated,
      $method,
    ), $args);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheDecorator::$cacheBin protected property The cache bin used to store the definition list.
CacheDecorator::$cacheExpire protected property The timestamp indicating when the definition list cache expires.
CacheDecorator::$cacheKey protected property The cache key used to store the definition list.
CacheDecorator::$cacheTags protected property The cache tags associated with the definition list.
CacheDecorator::$decorated protected property The Discovery object being decorated.
CacheDecorator::$definitions protected property The plugin definitions of the decorated discovery class.
CacheDecorator::clearCachedDefinitions public function Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions(). Overrides CachedDiscoveryInterface::clearCachedDefinitions
CacheDecorator::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
CacheDecorator::getDefinition public function Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). Overrides DiscoveryInterface::getDefinition
CacheDecorator::getDefinitions public function Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions(). Overrides DiscoveryInterface::getDefinitions
CacheDecorator::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
CacheDecorator::__call public function Passes through all unknown calls onto the decorated object.
CacheDecorator::__construct public function Constructs a Drupal\Core\Plugin\Discovery\CacheDecorator object.