Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php
- 9 core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php
Namespace
Drupal\Core\Cache
File
core/lib/Drupal/Core/Cache/DatabaseCacheTagsChecksum.php
View source
<?php
namespace Drupal\Core\Cache;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
class DatabaseCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {
use CacheTagsChecksumTrait;
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
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) {
if (!$this
->ensureTableExists()) {
throw $e;
}
}
}
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 (!$this
->ensureTableExists()) {
throw $e;
}
}
return [];
}
protected function ensureTableExists() {
try {
$database_schema = $this->connection
->schema();
$schema_definition = $this
->schemaDefinition();
$database_schema
->createTable('cachetags', $schema_definition);
} catch (DatabaseException $e) {
} catch (\Exception $e) {
return FALSE;
}
return TRUE;
}
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;
}
public function getDatabaseConnection() {
return $this->connection;
}
}
Classes