diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 2bc1675..aa1ebdc 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -203,20 +203,19 @@ public function setMultiple(array $items) { protected function doSetMultiple(array $items) { $values = []; - // Check if maximum expiration settings for cache bin is defined. If set, - // enforce an upper limit for the expiration to allow for garbage - // collection. - $max_expire = isset($this->configuration['max_lifetime']) ? $this->configuration['max_lifetime'] + REQUEST_TIME : Cache::PERMANENT; + // Check if a 'max_lifetime' setting is set for the current cache bin. + // If set, the lifetime of a cache item is limited to the specified + // 'max_lifetime', thus preventing unused cache items to linger forever. + $expire = isset($this->configuration['max_lifetime']) ? $this->configuration['max_lifetime'] + REQUEST_TIME : Cache::PERMANENT; foreach ($items as $cid => $item) { $item += array( - 'expire' => $max_expire, + 'expire' => $expire, 'tags' => array(), ); - // If maximum expiration is set, enforce an upper limit on the expiry. - if ($max_expire !== Cache::PERMANENT && ($item['expire'] === Cache::PERMANENT || $item['expire'] > $max_expire)) { - $item['expire'] = $max_expire; + if ($expire !== Cache::PERMANENT && ($item['expire'] === Cache::PERMANENT || $item['expire'] > $expire)) { + $item['expire'] = $expire; } Cache::validateTags($item['tags']); diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 7b55e51..c0256f9 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -44,9 +44,9 @@ class DatabaseBackendFactory implements CacheFactoryInterface { function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider) { $this->connection = $connection; $this->checksumProvider = $checksum_provider; - // Load settings and if not set, default to maximum expiration of the render + // Load settings and if not set, default to maximum lifetime of the render // cache bin in one week. - $this->configuration = Settings::get('cache_database', ['render' => ['max_expire' => 86400 * 7]]); + $this->configuration = Settings::get('cache_database', ['render' => ['max_lifetime' => 86400 * 7]]); } /** diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php index 307a853..c0ca619 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -54,11 +54,12 @@ public function testSetGet() { } public function testCacheBinExpiration() { - $configuration = ['max_expire' => 2800]; + $configuration = ['max_lifetime' => 2800]; $backend = new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'render', $configuration); $backend->set('test_cache1', 'foo'); $cached = $backend->get('test_cache1'); + debug($cached->expire); $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2800), 'Maximum cache expire time is correct.'); $backend->set('test_cache2', 'foo', REQUEST_TIME + 2799); @@ -71,7 +72,7 @@ public function testCacheBinExpiration() { } public function testCacheBinExpirationSetMultiple() { - $configuration = ['max_expire' => 2800]; + $configuration = ['max_lifetime' => 2800]; $backend = new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'render', $configuration); $backend->setMultiple([ @@ -99,8 +100,7 @@ public function testCacheBinExpirationSetMultiple() { } public function testCacheBinNoExpiration() { - // Empty configuration would force DatabaseBackend to select permanent - // lifetime for cache objects. + // No configuration (the default) allows cache items to live forever. $configuration = []; $backend = new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'render', $configuration); @@ -114,7 +114,7 @@ public function testCacheBinNoExpiration() { ], ]); - // Test set as well. + // Test set() as well. $backend->set('test_cache3', 'foo', REQUEST_TIME + 2801); $cached = $backend->get('test_cache1'); diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php index 4ba1366..c1c0adf 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -678,6 +678,21 @@ * example.org, with all subdomains included. */ + /** + * Per-bin database backend configuration. + * + * Configure settings for database backend. Available settings are: + * - max_expire: Specify the maximum expiry for an individual cache item in the + * selected bin. Some cache bins can get very big and persistent cache entries + * are never removed. Setting this value will allow them to be garbage + * collected after the configured time, and will need to be rebuilt if the + * item is requested again. + */ + $settings['cache_database']['render'] = [ + 'max_lifetime' => 86400 * 7, + ]; + + /** * Load local development override configuration, if available. * @@ -691,17 +706,3 @@ # if (file_exists(__DIR__ . '/settings.local.php')) { # include __DIR__ . '/settings.local.php'; # } - -/** - * Per-bin database backend configuration. - * - * Configure settings for database backend. Available settings are: - * - max_expire: Specify the maximum expiry for an individual cache item in the - * selected bin. Some cache bins can get very big and persistent cache entries - * are never removed. Setting this value will allow them to be garbage - * collected after the configured time, and will need to be rebuilt if the - * item is requested again. - */ -$settings['cache_database']['render'] = [ - 'max_lifetime' => 86400 * 7, -];