core/lib/Drupal/Core/Cache/DatabaseBackend.php | 9 +++++++-- core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php | 16 +++++++++++++++- core/lib/Drupal/Core/DrupalKernel.php | 3 ++- core/modules/system/system.install | 12 +++++++++--- .../KernelTests/Core/Cache/ChainedFastBackendTest.php | 2 +- .../KernelTests/Core/Cache/DatabaseBackendTest.php | 2 +- .../tests/Drupal/KernelTests/Core/Command/DbDumpTest.php | 3 ++- 7 files changed, 37 insertions(+), 10 deletions(-) diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index fd6b38c..877b4db 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -17,9 +17,14 @@ class DatabaseBackend implements CacheBackendInterface { /** + * -1 means "no maximum". + */ + const MAXIMUM_NONE = -1; + + /** * The maximum number of rows that this cache bin table is allowed to store. * - * -1 means no maximum. + * * @see ::MAXIMUM_NONE * * @var int */ @@ -339,7 +344,7 @@ public function invalidateAll() { public function garbageCollection() { try { // Bounded size cache bin, using FIFO. - if ($this->maxRows !== -1) { + if ($this->maxRows !== static::MAXIMUM_NONE) { $query = $this->connection->select($this->bin); $query->addExpression('MAX(bounded_id)', 'bounded_id'); $max_bounded_id = $query->execute()->fetchField(); diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index c610a77..db8bf25 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -54,6 +54,20 @@ public function __construct(Connection $connection, CacheTagsChecksumInterface $ * The cache backend object for the specified cache bin. */ public function get($bin) { + $max_rows = $this->getMaxRowsForBin($bin); + return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $max_rows); + } + + /** + * Gets the max rows for the specified cache bin. + * + * @param string $bin + * The cache bin for which the object is created. + * + * @return int + * The maximum number of rows for the given bin. Defaults to 10000. + */ + protected function getMaxRowsForBin($bin) { $max_rows_settings = $this->settings->get('database_cache_max_rows'); // First, look for a cache bin specific setting. if (isset($max_rows_settings['bins'][$bin])) { @@ -67,7 +81,7 @@ public function get($bin) { // Fall back to the default max rows if nothing else is configured. $max_rows = 10000; } - return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $max_rows); + return $max_rows; } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index dbd1aa9..8333f40 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -7,6 +7,7 @@ use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Cache\DatabaseBackend; use Drupal\Core\Config\BootstrapConfigStorageFactory; use Drupal\Core\Config\NullStorage; use Drupal\Core\Database\Database; @@ -77,7 +78,7 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface { ], 'cache.container' => [ 'class' => 'Drupal\Core\Cache\DatabaseBackend', - 'arguments' => ['@database', '@cache_tags_provider.container', 'container', -1], + 'arguments' => ['@database', '@cache_tags_provider.container', 'container', DatabaseBackend::MAXIMUM_NONE], ], 'cache_tags_provider.container' => [ 'class' => 'Drupal\Core\Cache\DatabaseCacheTagsChecksum', diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 684fb77..ea3e7c0 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -10,6 +10,8 @@ use Drupal\Component\FileSystem\FileSystem; use Drupal\Component\Utility\OpCodeCache; use Drupal\Component\Utility\Unicode; +use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\DatabaseBackend; use Drupal\Core\Path\AliasStorage; use Drupal\Core\Url; use Drupal\Core\Database\Database; @@ -1989,9 +1991,13 @@ function system_update_8401() { * Delete all cache_* tables. They are recreated on demand with the new schema. */ function system_update_8402() { - foreach (\Drupal\Core\Cache\Cache::getBins() as $bin => $cache_backend) { - if ($cache_backend instanceof \Drupal\Core\Cache\DatabaseBackend) { - db_drop_table("cache_$bin"); + foreach (Cache::getBins() as $bin => $cache_backend) { + if ($cache_backend instanceof DatabaseBackend) { + $table_name = "cache_$bin"; + $schema = Database::getConnection()->schema(); + if ($schema->tableExists($table_name)) { + $schema->dropTable($table_name); + } } } } diff --git a/core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php b/core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php index 3021b04..a93b674 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/ChainedFastBackendTest.php @@ -20,7 +20,7 @@ class ChainedFastBackendTest extends GenericCacheBackendUnitTestBase { * A new ChainedFastBackend object. */ protected function createCacheBackend($bin) { - $consistent_backend = new DatabaseBackend(\Drupal::service('database'), \Drupal::service('cache_tags.invalidator.checksum'), $bin); + $consistent_backend = new DatabaseBackend(\Drupal::service('database'), \Drupal::service('cache_tags.invalidator.checksum'), $bin, 100); $fast_backend = new PhpBackend($bin, \Drupal::service('cache_tags.invalidator.checksum')); $backend = new ChainedFastBackend($consistent_backend, $fast_backend, $bin); // Explicitly register the cache bin as it can not work through the diff --git a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php index 9857b3e..4516af6 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php @@ -16,7 +16,7 @@ class DatabaseBackendTest extends GenericCacheBackendUnitTestBase { * * @var int */ - public static $maxRows = 100; + protected static $maxRows = 100; /** * Modules to enable. diff --git a/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php b/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php index 8129410..13b29c3 100644 --- a/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php +++ b/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php @@ -70,7 +70,8 @@ public function register(ContainerBuilder $container) { parent::register($container); $container->register('cache_factory', 'Drupal\Core\Cache\DatabaseBackendFactory') ->addArgument(new Reference('database')) - ->addArgument(new Reference('cache_tags.invalidator.checksum')); + ->addArgument(new Reference('cache_tags.invalidator.checksum')) + ->addArgument(new Reference('settings')); } /**