class CacheBackendDecorator

Same name in other branches
  1. 11.x core/modules/system/tests/modules/performance_test/src/Cache/CacheBackendDecorator.php \Drupal\performance_test\Cache\CacheBackendDecorator

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

Hierarchy

Expanded class hierarchy of CacheBackendDecorator

File

core/modules/system/tests/modules/performance_test/src/Cache/CacheBackendDecorator.php, line 15

Namespace

Drupal\performance_test\Cache
View source
class CacheBackendDecorator implements CacheBackendInterface, CacheTagsInvalidatorInterface {
    public function __construct(PerformanceDataCollector $performanceDataCollector, CacheBackendInterface $cacheBackend, string $bin) {
    }
    
    /**
     * Logs a cache operation.
     *
     * @param string|array $cids
     *   The cache IDs.
     * @param float $start
     *   The start microtime.
     * @param float $stop
     *   The stop microtime.
     * @param string $operation
     *   The type of operation being logged.
     *
     * @return void
     */
    protected function logCacheOperation(string|array $cids, float $start, float $stop, string $operation) : void {
        $this->performanceDataCollector
            ->addCacheOperation([
            'operation' => $operation,
            'cids' => implode(', ', (array) $cids),
            'bin' => $this->bin,
            'start' => $start,
            'stop' => $stop,
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function get($cid, $allow_invalid = FALSE) : object|bool {
        $start = microtime(TRUE);
        $cache = $this->cacheBackend
            ->get($cid, $allow_invalid);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cid, $start, $stop, 'get');
        return $cache;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getMultiple(&$cids, $allow_invalid = FALSE) : array {
        $cids_copy = $cids;
        $start = microtime(TRUE);
        $cache = $this->cacheBackend
            ->getMultiple($cids, $allow_invalid);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cids_copy, $start, $stop, 'getMultiple');
        return $cache;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->set($cid, $data, $expire, $tags);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cid, $start, $stop, 'set');
    }
    
    /**
     * {@inheritdoc}
     */
    public function setMultiple(array $items) {
        $cids = array_keys($items);
        $start = microtime(TRUE);
        $this->cacheBackend
            ->setMultiple($items);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cids, $start, $stop, 'setMultiple');
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($cid) {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->delete($cid);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cid, $start, $stop, 'delete');
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteMultiple(array $cids) {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->deleteMultiple($cids);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cids, $start, $stop, 'deleteMultiple');
    }
    
    /**
     * {@inheritdoc}
     */
    public function deleteAll() {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->deleteAll();
        $stop = microtime(TRUE);
        $this->logCacheOperation([], $start, $stop, 'deleteAll');
    }
    
    /**
     * {@inheritdoc}
     */
    public function invalidate($cid) {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->invalidate($cid);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cid, $start, $stop, 'invalidate');
    }
    
    /**
     * {@inheritdoc}
     */
    public function invalidateMultiple(array $cids) {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->invalidateMultiple($cids);
        $stop = microtime(TRUE);
        $this->logCacheOperation($cids, $start, $stop, 'invalidateMultiple');
    }
    
    /**
     * {@inheritdoc}
     */
    public function invalidateTags(array $tags) {
        if ($this->cacheBackend instanceof CacheTagsInvalidatorInterface) {
            $this->cacheBackend
                ->invalidateTags($tags);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function invalidateAll() {
        $start = microtime(TRUE);
        $this->cacheBackend
            ->invalidateAll();
        $stop = microtime(TRUE);
        $this->logCacheOperation([], $start, $stop, 'invalidateAll');
    }
    
    /**
     * {@inheritdoc}
     */
    public function garbageCollection() {
        $this->cacheBackend
            ->garbageCollection();
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeBin() {
        $this->cacheBackend
            ->removeBin();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CacheBackendDecorator::delete public function Deletes an item from the cache. Overrides CacheBackendInterface::delete
CacheBackendDecorator::deleteAll public function Deletes all cache items in a bin. Overrides CacheBackendInterface::deleteAll
CacheBackendDecorator::deleteMultiple public function Deletes multiple items from the cache. Overrides CacheBackendInterface::deleteMultiple
CacheBackendDecorator::garbageCollection public function Performs garbage collection on a cache bin. Overrides CacheBackendInterface::garbageCollection
CacheBackendDecorator::get public function Returns data from the persistent cache. Overrides CacheBackendInterface::get
CacheBackendDecorator::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface::getMultiple
CacheBackendDecorator::invalidate public function Marks a cache item as invalid. Overrides CacheBackendInterface::invalidate
CacheBackendDecorator::invalidateAll public function Marks all cache items as invalid. Overrides CacheBackendInterface::invalidateAll
CacheBackendDecorator::invalidateMultiple public function Marks cache items as invalid. Overrides CacheBackendInterface::invalidateMultiple
CacheBackendDecorator::invalidateTags public function
CacheBackendDecorator::logCacheOperation protected function Logs a cache operation.
CacheBackendDecorator::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
CacheBackendDecorator::set public function Stores data in the persistent cache. Overrides CacheBackendInterface::set
CacheBackendDecorator::setMultiple public function Store multiple items in the persistent cache. Overrides CacheBackendInterface::setMultiple
CacheBackendDecorator::__construct public function
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.

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