You are here

class StaticCache in Tome 8

Determines if pages are statically cached.

@internal

Hierarchy

Expanded class hierarchy of StaticCache

1 file declares its use of StaticCache
SuperStaticCache.php in modules/tome_static/modules/tome_static_super_cache/src/SuperStaticCache.php
1 string reference to 'StaticCache'
tome_static.services.yml in modules/tome_static/tome_static.services.yml
modules/tome_static/tome_static.services.yml
1 service uses StaticCache
cache.tome_static in modules/tome_static/tome_static.services.yml
Drupal\tome_static\StaticCache

File

modules/tome_static/src/StaticCache.php, line 19

Namespace

Drupal\tome_static
View source
class StaticCache extends DatabaseBackend implements StaticCacheInterface {

  /**
   * Constructs a StaticCache object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
   *   The cache tags checksum provider.
   */
  public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider) {
    parent::__construct($connection, $checksum_provider, 'tome_static', self::MAXIMUM_NONE);
  }

  /**
   * {@inheritdoc}
   */
  public function filterUncachedPaths($base_url, array $original_paths) {
    $this
      ->ensureBinExists();
    $cid_map = [];
    foreach ($original_paths as $original_path) {
      $cid_map[$this
        ->getCacheId($base_url, $original_path)] = $original_path;
    }
    $never_cache = Settings::get('tome_static_cache_exclude', []);
    $cids = array_keys($cid_map);
    foreach ($this
      ->getMultiple($cids) as $cid => $cache) {
      $skip = FALSE;
      foreach ($never_cache as $pattern) {
        if ($cid_map[$cid] === $pattern || @preg_match($pattern, $cid_map[$cid])) {
          $skip = TRUE;
          break;
        }
      }
      if (!$skip && file_exists($cache->data)) {
        unset($cid_map[$cid]);
      }
    }
    return array_values($cid_map);
  }

  /**
   * {@inheritdoc}
   */
  public function isCacheEmpty() {
    $this
      ->ensureBinExists();
    $count = $this->connection
      ->select($this->bin)
      ->fields($this->bin, [
      'cid',
    ])
      ->countQuery()
      ->execute()
      ->fetchField();
    return empty($count);
  }

  /**
   * {@inheritdoc}
   */
  public function getExpiredFiles() {
    $this
      ->ensureBinExists();
    $cids = $this->connection
      ->select($this->bin)
      ->fields($this->bin, [
      'cid',
    ])
      ->execute()
      ->fetchCol();
    $files = [];
    foreach ($this
      ->getMultiple($cids, TRUE) as $cache) {
      if (!$cache->valid && file_exists($cache->data)) {
        $files[$cache->data] = $cache->data;
      }
    }
    parent::garbageCollection();
    return array_values($files);
  }

  /**
   * {@inheritdoc}
   */
  public function setCache(Request $request, Response $response, $original_path, $destination) {
    if (!$response instanceof CacheableResponseInterface) {
      return FALSE;
    }
    $request_time = $request->server
      ->get('REQUEST_TIME');
    if ($expires = $response
      ->getExpires()) {
      $date = $expires
        ->getTimestamp();
      $expire = $date > $request_time ? $date : Cache::PERMANENT;
    }
    else {
      $expire = Cache::PERMANENT;
    }
    if ($expire === Cache::PERMANENT || $expire > $request_time) {
      $tags = $response
        ->getCacheableMetadata()
        ->getCacheTags();
      $cid = $this
        ->getCacheId($request
        ->getSchemeAndHttpHost(), $original_path);
      $this
        ->set($cid, $destination, $expire, $tags);
      if ($request
        ->getUri() !== $original_path) {
        $cid = $this
          ->getCacheId($request
          ->getSchemeAndHttpHost(), $request
          ->getPathInfo());
        $this
          ->set($cid, $destination, $expire, $tags);
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function garbageCollection() {

    // No-op, we rely on expired rows in ::getExpiredFiles.
  }

  /**
   * Gets the Tome cache ID for this request.
   *
   * @param string $base_url
   *   The base scheme/host for this request.
   * @param string $original_path
   *   The original, placeholdered path.
   *
   * @return string
   *   The cache ID for this request.
   */
  protected function getCacheId($base_url, $original_path) {
    $cid_parts = [
      $base_url,
      rtrim($original_path, '/'),
    ];
    return strtolower(implode(':', $cid_parts));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheBackendInterface::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly deleted.
DatabaseBackend::$bin protected property
DatabaseBackend::$checksumProvider protected property The cache tags checksum provider.
DatabaseBackend::$connection protected property The database connection.
DatabaseBackend::$maxRows protected property The maximum number of rows that this cache bin table is allowed to store.
DatabaseBackend::catchException protected function Act on an exception when cache might be stale.
DatabaseBackend::DEFAULT_MAX_ROWS constant The default maximum number of rows that this cache bin table can store.
DatabaseBackend::delete public function Deletes an item from the cache. Overrides CacheBackendInterface::delete
DatabaseBackend::deleteAll public function Deletes all cache items in a bin. Overrides CacheBackendInterface::deleteAll
DatabaseBackend::deleteMultiple public function Deletes multiple items from the cache. Overrides CacheBackendInterface::deleteMultiple
DatabaseBackend::doSetMultiple protected function Stores multiple items in the persistent cache.
DatabaseBackend::ensureBinExists protected function Check if the cache bin exists and create it if not.
DatabaseBackend::get public function Returns data from the persistent cache. Overrides CacheBackendInterface::get
DatabaseBackend::getMaxRows public function The maximum number of rows that this cache bin table is allowed to store.
DatabaseBackend::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface::getMultiple
DatabaseBackend::invalidate public function Marks a cache item as invalid. Overrides CacheBackendInterface::invalidate
DatabaseBackend::invalidateAll public function Marks all cache items as invalid. Overrides CacheBackendInterface::invalidateAll
DatabaseBackend::invalidateMultiple public function Marks cache items as invalid. Overrides CacheBackendInterface::invalidateMultiple
DatabaseBackend::MAXIMUM_NONE constant -1 means infinite allows numbers of rows for the cache backend.
DatabaseBackend::normalizeCid protected function Normalizes a cache ID in order to comply with database limitations.
DatabaseBackend::prepareItem protected function Prepares a cached item.
DatabaseBackend::removeBin public function Remove a cache bin. Overrides CacheBackendInterface::removeBin
DatabaseBackend::schemaDefinition public function Defines the schema for the {cache_*} bin tables.
DatabaseBackend::set public function Stores data in the persistent cache. Overrides CacheBackendInterface::set
DatabaseBackend::setMultiple public function Store multiple items in the persistent cache. Overrides CacheBackendInterface::setMultiple
StaticCache::filterUncachedPaths public function Filters paths that are uncached from a given array. Overrides StaticCacheInterface::filterUncachedPaths
StaticCache::garbageCollection public function Performs garbage collection on a cache bin. Overrides DatabaseBackend::garbageCollection
StaticCache::getCacheId protected function Gets the Tome cache ID for this request.
StaticCache::getExpiredFiles public function Gets files that are in cache but invalid, and can be deleted. Overrides StaticCacheInterface::getExpiredFiles
StaticCache::isCacheEmpty public function Checks if the cache is empty. Overrides StaticCacheInterface::isCacheEmpty
StaticCache::setCache public function Adds to the Tome cache. Overrides StaticCacheInterface::setCache
StaticCache::__construct public function Constructs a StaticCache object. Overrides DatabaseBackend::__construct