class MemoryCounterBackend

Defines a memory cache implementation that counts set and get calls.

This can be used to mock a cache backend where one needs to know how many times a cache entry was set or requested.

@todo On the longrun this backend should be replaced by phpunit mock objects.

Hierarchy

Expanded class hierarchy of MemoryCounterBackend

1 file declares its use of MemoryCounterBackend
ViewsDataTest.php in drupal/core/modules/views/lib/Drupal/views/Tests/ViewsDataTest.php
Definition of Drupal\views\Tests\ViewsDataTest.

File

drupal/core/lib/Drupal/Core/Cache/MemoryCounterBackend.php, line 19
Contains \Drupal\Core\Cache\MemoryCounterBackend.

Namespace

Drupal\Core\Cache
View source
class MemoryCounterBackend extends MemoryBackend {

  /**
   * Stores a list of cache cid calls keyed by function name.
   *
   * @var array
   */
  protected $counter = array();

  /**
   * Implements \Drupal\Core\Cache\CacheBackendInterface::get().
   */
  public function get($cid, $allow_invalid = FALSE) {
    $this
      ->increaseCounter(__FUNCTION__, $cid);
    return parent::get($cid, $allow_invalid);
  }

  /**
   * Implements \Drupal\Core\Cache\CacheBackendInterface::set().
   */
  public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANENT, array $tags = array()) {
    $this
      ->increaseCounter(__FUNCTION__, $cid);
    parent::set($cid, $data, $expire, $tags);
  }

  /**
   * Implements \Drupal\Core\Cache\CacheBackendInterface::delete().
   */
  public function delete($cid) {
    $this
      ->increaseCounter(__FUNCTION__, $cid);
    parent::delete($cid);
  }

  /**
   * Increase the counter for a function with a certain cid.
   *
   * @param string $function
   *   The called function.
   *
   * @param string $cid
   *   The cache ID of the cache entry to increase the counter.
   */
  protected function increaseCounter($function, $cid) {
    if (!isset($this->counter[$function][$cid])) {
      $this->counter[$function][$cid] = 1;
    }
    else {
      $this->counter[$function][$cid]++;
    }
  }

  /**
   * Returns the call counter for the get, set and delete methods.
   *
   * @param string $method
   *   (optional) The name of the method to return the call counter for.
   * @param string $cid
   *   (optional) The name of the cache id to return the call counter for.
   *
   * @return int|array
   *   An integer if both method and cid is given, an array otherwise.
   */
  public function getCounter($method = NULL, $cid = NULL) {
    if ($method && $cid) {
      return isset($this->counter[$method][$cid]) ? $this->counter[$method][$cid] : 0;
    }
    elseif ($method) {
      return isset($this->counter[$method]) ? $this->counter[$method] : array();
    }
    else {
      return $this->counter;
    }
  }

  /**
   * Resets the call counter.
   */
  public function resetCounter() {
    $this->counter = array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
MemoryBackend::$cache protected property Array to store cache objects.
MemoryBackend::deleteAll public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll(). Overrides CacheBackendInterface::deleteAll
MemoryBackend::deleteMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple(). Overrides CacheBackendInterface::deleteMultiple
MemoryBackend::deleteTags public function Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags(). Overrides CacheBackendInterface::deleteTags
MemoryBackend::flattenTags protected function 'Flattens' a tags array into an array of strings.
MemoryBackend::garbageCollection public function Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection() Overrides CacheBackendInterface::garbageCollection
MemoryBackend::getMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple(). Overrides CacheBackendInterface::getMultiple
MemoryBackend::invalidate public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidate(). Overrides CacheBackendInterface::invalidate
MemoryBackend::invalidateAll public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). Overrides CacheBackendInterface::invalidateAll
MemoryBackend::invalidateMultiple public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple(). Overrides CacheBackendInterface::invalidateMultiple
MemoryBackend::invalidateTags public function Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). Overrides CacheBackendInterface::invalidateTags
MemoryBackend::isEmpty public function Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty(). Overrides CacheBackendInterface::isEmpty
MemoryBackend::prepareItem protected function Prepares a cached item.
MemoryBackend::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
MemoryBackend::__construct public function Constructs a MemoryBackend object.
MemoryCounterBackend::$counter protected property Stores a list of cache cid calls keyed by function name.
MemoryCounterBackend::delete public function Implements \Drupal\Core\Cache\CacheBackendInterface::delete(). Overrides MemoryBackend::delete
MemoryCounterBackend::get public function Implements \Drupal\Core\Cache\CacheBackendInterface::get(). Overrides MemoryBackend::get
MemoryCounterBackend::getCounter public function Returns the call counter for the get, set and delete methods.
MemoryCounterBackend::increaseCounter protected function Increase the counter for a function with a certain cid.
MemoryCounterBackend::resetCounter public function Resets the call counter.
MemoryCounterBackend::set public function Implements \Drupal\Core\Cache\CacheBackendInterface::set(). Overrides MemoryBackend::set