core/lib/Drupal/Core/Cache/DatabaseBackend.php | 2 +- .../KernelTests/Core/Cache/DatabaseBackendTest.php | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index f5d6f89..fac8245 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -24,7 +24,7 @@ class DatabaseBackend implements CacheBackendInterface { * * @var int */ - protected static $boundedSizeCount = 1000; + public static $boundedSizeCount = 1000; /** * @var string diff --git a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php index de8bbda..61c3b8d 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php @@ -48,4 +48,47 @@ public function testSetGet() { $this->assertIdentical($cached_value_short, $backend->get($cid_short)->data, "Backend contains the correct value for short, non-ASCII cache id."); } + /** + * Tests the row count limiting of cache bin database tables. + */ + public function testGarbageCollection() { + $backend = $this->getCacheBackend(); + $max_rows = DatabaseBackend::$boundedSizeCount; + + $this->assertSame(0, (int) $this->getNumRows()); + + // Fill to just the limit. + for ($i = 0; $i < $max_rows; $i++) { + $backend->set("test$i", $i); + } + $this->assertSame($max_rows, $this->getNumRows()); + + // Garbage collection has no effect. + $backend->garbageCollection(); + $this->assertSame($max_rows, $this->getNumRows()); + + // Go one row beyond the limit. + $backend->set('test' . ($max_rows + 1), $max_rows + 1); + $this->assertSame($max_rows + 1, $this->getNumRows()); + + // Garbage collection removes one row: the oldest. + $backend->garbageCollection(); + $this->assertSame($max_rows, $this->getNumRows()); + $this->assertSame(FALSE, $backend->get('test0')); + } + + /** + * Gets the number of rows in the test cache bin database table. + * + * @return int + * The number of rows in the test cache bin database table. + */ + protected function getNumRows() { + $table = 'cache_' . $this->testBin; + $connection = $this->container->get('database'); + $query = $connection->select($table); + $query->addExpression('COUNT(cid)', 'cid'); + return (int) $query->execute()->fetchField(); + } + }