class StaticCache in Tome 8
Determines if pages are statically cached.
@internal
Hierarchy
- class \Drupal\Core\Cache\DatabaseBackend implements CacheBackendInterface
- class \Drupal\tome_static\StaticCache implements StaticCacheInterface
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_staticView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheBackendInterface:: |
constant | Indicates that the item should never be removed unless explicitly deleted. | ||
DatabaseBackend:: |
protected | property | ||
DatabaseBackend:: |
protected | property | The cache tags checksum provider. | |
DatabaseBackend:: |
protected | property | The database connection. | |
DatabaseBackend:: |
protected | property | The maximum number of rows that this cache bin table is allowed to store. | |
DatabaseBackend:: |
protected | function | Act on an exception when cache might be stale. | |
DatabaseBackend:: |
constant | The default maximum number of rows that this cache bin table can store. | ||
DatabaseBackend:: |
public | function |
Deletes an item from the cache. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Deletes all cache items in a bin. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Deletes multiple items from the cache. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
protected | function | Stores multiple items in the persistent cache. | |
DatabaseBackend:: |
protected | function | Check if the cache bin exists and create it if not. | |
DatabaseBackend:: |
public | function |
Returns data from the persistent cache. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function | The maximum number of rows that this cache bin table is allowed to store. | |
DatabaseBackend:: |
public | function |
Returns data from the persistent cache when given an array of cache IDs. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Marks a cache item as invalid. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Marks all cache items as invalid. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Marks cache items as invalid. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
constant | -1 means infinite allows numbers of rows for the cache backend. | ||
DatabaseBackend:: |
protected | function | Normalizes a cache ID in order to comply with database limitations. | |
DatabaseBackend:: |
protected | function | Prepares a cached item. | |
DatabaseBackend:: |
public | function |
Remove a cache bin. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function | Defines the schema for the {cache_*} bin tables. | |
DatabaseBackend:: |
public | function |
Stores data in the persistent cache. Overrides CacheBackendInterface:: |
|
DatabaseBackend:: |
public | function |
Store multiple items in the persistent cache. Overrides CacheBackendInterface:: |
|
StaticCache:: |
public | function |
Filters paths that are uncached from a given array. Overrides StaticCacheInterface:: |
|
StaticCache:: |
public | function |
Performs garbage collection on a cache bin. Overrides DatabaseBackend:: |
|
StaticCache:: |
protected | function | Gets the Tome cache ID for this request. | |
StaticCache:: |
public | function |
Gets files that are in cache but invalid, and can be deleted. Overrides StaticCacheInterface:: |
|
StaticCache:: |
public | function |
Checks if the cache is empty. Overrides StaticCacheInterface:: |
|
StaticCache:: |
public | function |
Adds to the Tome cache. Overrides StaticCacheInterface:: |
|
StaticCache:: |
public | function |
Constructs a StaticCache object. Overrides DatabaseBackend:: |