class FileParsingCacheCollectorBase

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Utility/FileParsingCacheCollectorBase.php \Drupal\Core\Utility\FileParsingCacheCollectorBase

Caches file parsing in a cache collector.

Hierarchy

Expanded class hierarchy of FileParsingCacheCollectorBase

File

core/lib/Drupal/Core/Utility/FileParsingCacheCollectorBase.php, line 15

Namespace

Drupal\Core\Utility
View source
abstract class FileParsingCacheCollectorBase extends CacheCollector {
  public function __construct($cid, CacheBackendInterface $cache, LockBackendInterface $lock, protected TimeInterface $time, array $tags = []) {
    parent::__construct($cid, $cache, $lock, $tags);
  }
  
  /**
   * {@inheritdoc}
   */
  public function get($key) : array {
    $this->lazyLoadCache();
    if (isset($this->storage[$key]) && file_exists($key)) {
      if ($this->storage[$key]['mtime'] === filemtime($key)) {
        return $this->storage[$key]['parsed'];
      }
    }
    return $this->resolveCacheMiss($key);
  }
  
  /**
   * {@inheritdoc}
   */
  public function resolveCacheMiss($key) : array {
    if (file_exists($key)) {
      $mtime = filemtime($key);
      $this->storage[$key] = [
        'mtime' => $mtime,
        'parsed' => $this->parseFile(file_get_contents($key)) ?? [],
      ];
      $this->persist($key);
      return $this->storage[$key]['parsed'];
    }
    if (isset($this->storage[$key])) {
      $this->delete($key);
    }
    return [];
  }
  
  /**
   * {@inheritdoc}
   */
  public function updateCache($lock = TRUE) : void {
    // Look for files cached longer than three months ago and remove them from
    // the cache item if they no longer exist.
    $three_months_ago = $this->time
      ->getCurrentTime() - 90 * 86400;
    foreach ($this->storage as $key => $item) {
      if ($item['mtime'] <= $three_months_ago && !file_exists($key)) {
        $this->delete($key);
      }
    }
    parent::updateCache($lock);
  }
  
  /**
   * Parses a file given a filename and returns the result.
   *
   * @param string $file
   *   The file path.
   *
   * @return mixed
   *   The result of the file parsing.
   */
  abstract protected function parseFile(string $file) : mixed;

}

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 1
CacheCollector::getCid protected function Gets the cache ID. 3
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 2
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::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface::set 4
FileParsingCacheCollectorBase::get public function Gets value from the cache. Overrides CacheCollector::get
FileParsingCacheCollectorBase::parseFile abstract protected function Parses a file given a filename and returns the result. 1
FileParsingCacheCollectorBase::resolveCacheMiss public function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
FileParsingCacheCollectorBase::updateCache public function Writes a value to the persistent cache immediately. Overrides CacheCollector::updateCache
FileParsingCacheCollectorBase::__construct public function Constructs a CacheCollector object. Overrides CacheCollector::__construct

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