class CacheCollectorHelper

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

Helper class to test the cache collector.

Hierarchy

Expanded class hierarchy of CacheCollectorHelper

1 file declares its use of CacheCollectorHelper
CacheCollectorTest.php in core/tests/Drupal/KernelTests/Core/Cache/CacheCollectorTest.php

File

core/tests/Drupal/Tests/Core/Cache/CacheCollectorHelper.php, line 12

Namespace

Drupal\Tests\Core\Cache
View source
class CacheCollectorHelper extends CacheCollector {
    
    /**
     * Contains data to return on a cache miss.
     * @var array
     */
    protected $cacheMissData = [];
    
    /**
     * Number of calls to \Drupal\Core\Cache\CacheCollector::resolveCacheMiss().
     *
     * @var int
     */
    protected $cacheMisses = 0;
    
    /**
     * {@inheritdoc}
     */
    public function set($key, $value) {
        parent::set($key, $value);
        $this->persist($key);
    }
    
    /**
     * {@inheritdoc}
     */
    public function resolveCacheMiss($key) {
        $this->cacheMisses++;
        if (isset($this->cacheMissData[$key])) {
            $this->storage[$key] = $this->cacheMissData[$key];
            $this->persist($key);
            return $this->cacheMissData[$key];
        }
    }
    
    /**
     * Sets data to return from a cache miss resolve.
     *
     * @param string $key
     *   The key being looked for.
     * @param mixed $value
     *   The value to return.
     */
    public function setCacheMissData($key, $value) {
        $this->cacheMissData[$key] = $value;
    }
    
    /**
     * Returns the number of cache misses.
     *
     * @return int
     *   Number of calls to the resolve cache miss method.
     */
    public function getCacheMisses() {
        return $this->cacheMisses;
    }
    
    /**
     * Setter for the cacheCreated property for use in unit tests.
     *
     * @param int $cache_created
     *   A unix timestamp.
     */
    public function setCacheCreated(int $cache_created) : void {
        $this->cacheCreated = $cache_created;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheCollector::$cache protected property The cache backend that should be used. 1
CacheCollector::$cacheCreated protected property Stores the cache creation time.
CacheCollector::$cacheInvalidated protected property Flag that indicates of the cache has been invalidated.
CacheCollector::$cacheLoaded protected property Indicates if the collected cache was already loaded.
CacheCollector::$cid protected property The cache id that is used for the cache entry.
CacheCollector::$keysToPersist protected property An array of keys to add to the cache on service termination.
CacheCollector::$keysToRemove protected property An array of keys to remove from the cache on service termination.
CacheCollector::$lock protected property The lock backend that should be used. 1
CacheCollector::$storage protected property Storage for the data itself.
CacheCollector::$tags protected property A list of tags that are used for the cache entry.
CacheCollector::clear public function Clears the collected cache entry. Overrides CacheCollectorInterface::clear 1
CacheCollector::delete public function Deletes the element. Overrides CacheCollectorInterface::delete 1
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::get public function Gets value from the cache. Overrides CacheCollectorInterface::get 3
CacheCollector::getCid protected function Gets the cache ID. 3
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 1
CacheCollector::invalidateCache protected function Invalidate the cache.
CacheCollector::lazyLoadCache protected function Loads the cache if not already done. 1
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Resets the local cache. Overrides CacheCollectorInterface::reset 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1
CacheCollector::__construct public function Constructs a CacheCollector object. 7
CacheCollectorHelper::$cacheMissData protected property Contains data to return on a cache miss.
CacheCollectorHelper::$cacheMisses protected property Number of calls to \Drupal\Core\Cache\CacheCollector::resolveCacheMiss().
CacheCollectorHelper::getCacheMisses public function Returns the number of cache misses.
CacheCollectorHelper::resolveCacheMiss public function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
CacheCollectorHelper::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollector::set
CacheCollectorHelper::setCacheCreated public function Setter for the cacheCreated property for use in unit tests.
CacheCollectorHelper::setCacheMissData public function Sets data to return from a cache miss resolve.

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