You are here

class CacheBackendWrapper in Devel 8.2

Same name and namespace in other branches
  1. 8.3 webprofiler/src/Cache/CacheBackendWrapper.php \Drupal\webprofiler\Cache\CacheBackendWrapper
  2. 8 webprofiler/src/Cache/CacheBackendWrapper.php \Drupal\webprofiler\Cache\CacheBackendWrapper
  3. 4.x webprofiler/src/Cache/CacheBackendWrapper.php \Drupal\webprofiler\Cache\CacheBackendWrapper

Wraps an existing cache backend to track calls to the cache backend.

Hierarchy

Expanded class hierarchy of CacheBackendWrapper

1 file declares its use of CacheBackendWrapper
CacheDataCollectorTest.php in webprofiler/tests/src/Unit/DataCollector/CacheDataCollectorTest.php

File

webprofiler/src/Cache/CacheBackendWrapper.php, line 13

Namespace

Drupal\webprofiler\Cache
View source
class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidatorInterface {

  /**
   * The data collector to register the calls.
   *
   * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
   */
  protected $cacheDataCollector;

  /**
   * The wrapped cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * The name of the wrapped cache bin.
   *
   * @var string
   */
  protected $bin;

  /**
   * Constructs a new CacheBackendWrapper.
   *
   * @param \Drupal\webprofiler\DataCollector\CacheDataCollector $cacheDataCollector
   *   The cache data collector to inform about cache get calls.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
   *   The wrapped cache backend.
   * @param string $bin
   *   The name of the wrapped cache bin.
   */
  public function __construct(CacheDataCollector $cacheDataCollector, CacheBackendInterface $cacheBackend, $bin) {
    $this->cacheDataCollector = $cacheDataCollector;
    $this->cacheBackend = $cacheBackend;
    $this->bin = $bin;
  }

  /**
   * {@inheritdoc}
   */
  public function get($cid, $allow_invalid = FALSE) {
    $cache = $this->cacheBackend
      ->get($cid, $allow_invalid);
    if ($cache) {
      $cacheCopy = new \stdClass();
      $cacheCopy->cid = $cache->cid;
      $cacheCopy->expire = $cache->expire;
      $cacheCopy->tags = $cache->tags;
      $this->cacheDataCollector
        ->registerCacheHit($this->bin, $cacheCopy);
    }
    else {
      $this->cacheDataCollector
        ->registerCacheMiss($this->bin, $cid);
    }
    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(&$cids, $allow_invalid = FALSE) {
    $cidsCopy = $cids;
    $cache = $this->cacheBackend
      ->getMultiple($cids, $allow_invalid);
    foreach ($cidsCopy as $cid) {
      if (in_array($cid, $cids)) {
        $this->cacheDataCollector
          ->registerCacheMiss($this->bin, $cid);
      }
      else {
        $cacheCopy = new \stdClass();
        $cacheCopy->cid = $cache[$cid]->cid;
        $cacheCopy->expire = $cache[$cid]->expire;
        $cacheCopy->tags = $cache[$cid]->tags;
        $this->cacheDataCollector
          ->registerCacheHit($this->bin, $cacheCopy);
      }
    }
    return $cache;
  }

  /**
   * {@inheritdoc}
   */
  public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
    return $this->cacheBackend
      ->set($cid, $data, $expire, $tags);
  }

  /**
   * {@inheritdoc}
   */
  public function setMultiple(array $items) {
    return $this->cacheBackend
      ->setMultiple($items);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($cid) {
    return $this->cacheBackend
      ->delete($cid);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMultiple(array $cids) {
    return $this->cacheBackend
      ->deleteMultiple($cids);
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {
    return $this->cacheBackend
      ->deleteAll();
  }

  /**
   * {@inheritdoc}
   */
  public function invalidate($cid) {
    return $this->cacheBackend
      ->invalidate($cid);
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateMultiple(array $cids) {
    return $this->cacheBackend
      ->invalidateMultiple($cids);
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateTags(array $tags) {
    if ($this->cacheBackend instanceof CacheTagsInvalidatorInterface) {
      $this->cacheBackend
        ->invalidateTags($tags);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function invalidateAll() {
    return $this->cacheBackend
      ->invalidateAll();
  }

  /**
   * {@inheritdoc}
   */
  public function garbageCollection() {
    return $this->cacheBackend
      ->garbageCollection();
  }

  /**
   * {@inheritdoc}
   */
  public function removeBin() {
    return $this->cacheBackend
      ->removeBin();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
CacheBackendWrapper::$bin protected property The name of the wrapped cache bin.
CacheBackendWrapper::$cacheBackend protected property The wrapped cache backend.
CacheBackendWrapper::$cacheDataCollector protected property The data collector to register the calls.
CacheBackendWrapper::delete public function Deletes an item from the cache. Overrides CacheBackendInterface::delete
CacheBackendWrapper::deleteAll public function Deletes all cache items in a bin. Overrides CacheBackendInterface::deleteAll
CacheBackendWrapper::deleteMultiple public function Deletes multiple items from the cache. Overrides CacheBackendInterface::deleteMultiple
CacheBackendWrapper::garbageCollection public function Performs garbage collection on a cache bin. Overrides CacheBackendInterface::garbageCollection
CacheBackendWrapper::get public function Returns data from the persistent cache. Overrides CacheBackendInterface::get
CacheBackendWrapper::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface::getMultiple
CacheBackendWrapper::invalidate public function Marks a cache item as invalid. Overrides CacheBackendInterface::invalidate
CacheBackendWrapper::invalidateAll public function Marks all cache items as invalid. Overrides CacheBackendInterface::invalidateAll
CacheBackendWrapper::invalidateMultiple public function Marks cache items as invalid. Overrides CacheBackendInterface::invalidateMultiple
CacheBackendWrapper::invalidateTags public function Marks cache items with any of the specified tags as invalid. Overrides CacheTagsInvalidatorInterface::invalidateTags
CacheBackendWrapper::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
CacheBackendWrapper::set public function Stores data in the persistent cache. Overrides CacheBackendInterface::set
CacheBackendWrapper::setMultiple public function Store multiple items in the persistent cache. Overrides CacheBackendInterface::setMultiple
CacheBackendWrapper::__construct public function Constructs a new CacheBackendWrapper.