diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index e7d4191..d0d8278 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -58,7 +58,7 @@ class DatabaseBackend implements CacheBackendInterface { * @param string $bin * The cache bin for which the object is created. * @param array $configuration - * The maximum cache expiration time. + * Per-bin database backend configuration. */ public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, $bin, array $configuration = []) { // All cache tables should be prefixed with 'cache_'. @@ -161,8 +161,8 @@ protected function prepareItem($cache, $allow_invalid) { */ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) { // Check if maximum expiration settings for cache bin is defined - if (isset($this->configuration['max_expire']) && ($expire == Cache::PERMANENT || $expire > $this->configuration['max_expire'])) { - $expire = REQUEST_TIME + $this->configuration['max_expire']; + if (isset($this->configuration['max_expire']) && ($expire == Cache::PERMANENT || $expire > $this->configuration['max_expire'] + REQUEST_TIME)) { + $expire = $this->configuration['max_expire'] + REQUEST_TIME; } Cache::validateTags($tags); $tags = array_unique($tags); diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 75e90be..fd46b66 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -44,7 +44,9 @@ class DatabaseBackendFactory implements CacheFactoryInterface { function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider) { $this->connection = $connection; $this->checksumProvider = $checksum_provider; - $this->configuration = Settings::get('cache_database', []); + // Set cache_database settings from settings.php and if none is set use + // maximum expire for render bin for one week. + $this->configuration = Settings::get('cache_database', ['render' => ['max_expire' => 86400 * 7]]); } /** @@ -57,7 +59,7 @@ function __construct(Connection $connection, CacheTagsChecksumInterface $checksu * The cache backend object for the specified cache bin. */ function get($bin) { - $configuration['max_expire'] = isset($this->configuration[$bin]) ? (int) $this->configuration[$bin] : null; + $configuration = isset($this->configuration[$bin]) ? $this->configuration[$bin] : []; return new DatabaseBackend($this->connection, $this->checksumProvider, $bin, $configuration); } diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php index 5330ffe..a5f8ff7 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -57,25 +57,19 @@ public function testCacheBinExpiration(){ $configuration = ['max_expire' => 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', true); + $cached = $backend->get('test_cache1'); $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2800), 'Maximum cache expire time is correct.'); - $configuration = ['max_expire' => 2799]; + $configuration = ['max_expire' => 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', true); + $backend->set('test_cache2', 'foo', REQUEST_TIME + 2799); + $cached = $backend->get('test_cache2'); $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2799), 'Maximum cache expire time is correct.'); - $configuration = ['max_expire' => 2801]; - $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', true); - $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2801), 'Maximum cache expire time is correct.'); - $configuration = ['max_expire' => 2800]; $backend = new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'render', $configuration); - $backend->set('test_cache1', 'foo', 3200); - $cached = $backend->get('test_cache1', true); - $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2800), 'Maximum cache expire time is correct.'); + $backend->set('test_cache3', 'foo', REQUEST_TIME + 2801); + $cached = $backend->get('test_cache3'); + $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2800), 'Maximum cache expire time is not exceeded.'); } } diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php index 7e0ee12..b905a11 100644 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -698,4 +698,4 @@ * Use this setting to set the maximum cache expiration time for a specific * cache bin in the database. */ -$settings['cache_database']['render'] = 604800; +$settings['cache_database']['render']['max_expire'] = 86400 * 7;