class DatabaseCacheTagsChecksum
Same name and namespace in other branches
- 10 core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php \Drupal\Core\Cache\DatabaseCacheTagsChecksum
- 9 core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php \Drupal\Core\Cache\DatabaseCacheTagsChecksum
- 8.9.x core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php \Drupal\Core\Cache\DatabaseCacheTagsChecksum
Cache tags invalidations checksum implementation that uses the database.
Hierarchy
- class \Drupal\Core\Cache\DatabaseCacheTagsChecksum implements \Drupal\Core\Cache\CacheTagsChecksumInterface, \Drupal\Core\Cache\CacheTagsInvalidatorInterface, \Drupal\Core\Cache\CacheTagsChecksumPreloadInterface, \Drupal\Core\Cache\CacheTagsPurgeInterface uses \Drupal\Core\Cache\CacheTagsChecksumTrait
Expanded class hierarchy of DatabaseCacheTagsChecksum
1 string reference to 'DatabaseCacheTagsChecksum'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses DatabaseCacheTagsChecksum
File
-
core/
lib/ Drupal/ Core/ Cache/ DatabaseCacheTagsChecksum.php, line 11
Namespace
Drupal\Core\CacheView source
class DatabaseCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface, CacheTagsChecksumPreloadInterface, CacheTagsPurgeInterface {
use CacheTagsChecksumTrait;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a DatabaseCacheTagsChecksum object.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
protected function doInvalidateTags(array $tags) {
try {
foreach ($tags as $tag) {
$this->connection
->merge('cachetags')
->insertFields([
'invalidations' => 1,
])
->expression('invalidations', '[invalidations] + 1')
->key('tag', $tag)
->execute();
}
} catch (\Exception $e) {
// Create the cache table, which will be empty. This fixes cases during
// core install where cache tags are invalidated before the table is
// created.
if (!$this->ensureTableExists()) {
throw $e;
}
}
}
/**
* {@inheritdoc}
*/
protected function getTagInvalidationCounts(array $tags) {
try {
return $this->connection
->query('SELECT [tag], [invalidations] FROM {cachetags} WHERE [tag] IN ( :tags[] )', [
':tags[]' => $tags,
])
->fetchAllKeyed();
} catch (\Exception $e) {
// If the table does not exist yet, create.
if (!$this->ensureTableExists()) {
throw $e;
}
}
return [];
}
/**
* {@inheritdoc}
*/
public function purge() : void {
try {
$this->connection
->truncate('cachetags')
->execute();
} catch (\Throwable $e) {
// If the table does not exist yet, there is nothing to purge.
if (!$this->ensureTableExists()) {
throw $e;
}
}
$this->reset();
}
/**
* Check if the cache tags table exists and create it if not.
*/
protected function ensureTableExists() {
try {
$database_schema = $this->connection
->schema();
$schema_definition = $this->schemaDefinition();
$database_schema->createTable('cachetags', $schema_definition);
} catch (DatabaseException) {
} catch (\Exception) {
return FALSE;
}
return TRUE;
}
/**
* Defines the schema for the {cachetags} table.
*
* @internal
*/
public function schemaDefinition() {
$schema = [
'description' => 'Cache table for tracking cache tag invalidations.',
'fields' => [
'tag' => [
'description' => 'Namespace-prefixed tag string.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'invalidations' => [
'description' => 'Number incremented when the tag is invalidated.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => [
'tag',
],
];
return $schema;
}
/**
* {@inheritdoc}
*/
public function getDatabaseConnection() {
return $this->connection;
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|
| CacheTagsChecksumInterface::INVALID_CHECKSUM_WHILE_IN_TRANSACTION | constant | The invalid checksum returned if a database transaction is in progress. | ||
| CacheTagsChecksumTrait::$delayedTags | protected | property | The set of cache tags whose invalidation is delayed. | |
| CacheTagsChecksumTrait::$invalidatedTags | protected | property | A list of tags that have already been invalidated in this request. | |
| CacheTagsChecksumTrait::$preloadTags | protected | property | Registered cache tags to preload. | |
| CacheTagsChecksumTrait::$tagCache | protected | property | Contains already loaded tag invalidation counts from the storage. | |
| CacheTagsChecksumTrait::calculateChecksum | protected | function | Calculates the current checksum for a given set of tags. | |
| CacheTagsChecksumTrait::getCurrentChecksum | public | function | ||
| CacheTagsChecksumTrait::invalidateTags | public | function | ||
| CacheTagsChecksumTrait::isValid | public | function | Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::isValid() | |
| CacheTagsChecksumTrait::registerCacheTagsForPreload | public | function | Implements \Drupal\Core\Cache\CacheTagsChecksumPreloadInterface::registerCacheTagsForPreload() | |
| CacheTagsChecksumTrait::reset | public | function | Implements \Drupal\Core\Cache\CacheTagsChecksumInterface::reset() | |
| CacheTagsChecksumTrait::rootTransactionEndCallback | public | function | Callback to be invoked just after a database transaction gets committed. | |
| DatabaseCacheTagsChecksum::$connection | protected | property | The database connection. | |
| DatabaseCacheTagsChecksum::doInvalidateTags | protected | function | Marks cache items with any of the specified tags as invalid. | Overrides CacheTagsChecksumTrait::doInvalidateTags |
| DatabaseCacheTagsChecksum::ensureTableExists | protected | function | Check if the cache tags table exists and create it if not. | |
| DatabaseCacheTagsChecksum::getDatabaseConnection | public | function | Returns the database connection. | Overrides CacheTagsChecksumTrait::getDatabaseConnection |
| DatabaseCacheTagsChecksum::getTagInvalidationCounts | protected | function | Fetches invalidation counts for cache tags. | Overrides CacheTagsChecksumTrait::getTagInvalidationCounts |
| DatabaseCacheTagsChecksum::purge | public | function | Purge cache tag invalidations. | Overrides CacheTagsPurgeInterface::purge |
| DatabaseCacheTagsChecksum::schemaDefinition | public | function | Defines the schema for the {cachetags} table. | |
| DatabaseCacheTagsChecksum::__construct | public | function | Constructs a DatabaseCacheTagsChecksum object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.