diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 49e830b..8f9cb44 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -42,6 +42,13 @@ class DatabaseBackend implements CacheBackendInterface { protected $checksumProvider; /** + * Per-bin database backend configuration. + * + * @var array + */ + protected $configuration; + + /** * Constructs a DatabaseBackend object. * * @param \Drupal\Core\Database\Connection $connection @@ -50,14 +57,17 @@ class DatabaseBackend implements CacheBackendInterface { * The cache tags checksum provider. * @param string $bin * The cache bin for which the object is created. + * @param array $configuration + * The maximum cache expiration time. */ - public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, $bin) { + public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, $bin, $configuration = array()) { // All cache tables should be prefixed with 'cache_'. $bin = 'cache_' . $bin; $this->bin = $bin; $this->connection = $connection; $this->checksumProvider = $checksum_provider; + $this->configuration = $configuration; } /** @@ -150,6 +160,10 @@ protected function prepareItem($cache, $allow_invalid) { * Implements Drupal\Core\Cache\CacheBackendInterface::set(). */ 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']; + } Cache::validateTags($tags); $tags = array_unique($tags); // Sort the cache tags so that they are stored consistently in the database. diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 59b0b22..75e90be 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Cache; use Drupal\Core\Database\Connection; +use Drupal\Core\Site\Settings; class DatabaseBackendFactory implements CacheFactoryInterface { @@ -26,6 +27,13 @@ class DatabaseBackendFactory implements CacheFactoryInterface { protected $checksumProvider; /** + * Per-bin database backend configuration. + * + * @var array + */ + protected $configuration; + + /** * Constructs the DatabaseBackendFactory object. * * @param \Drupal\Core\Database\Connection $connection @@ -36,6 +44,7 @@ class DatabaseBackendFactory implements CacheFactoryInterface { function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider) { $this->connection = $connection; $this->checksumProvider = $checksum_provider; + $this->configuration = Settings::get('cache_database', []); } /** @@ -48,7 +57,8 @@ function __construct(Connection $connection, CacheTagsChecksumInterface $checksu * The cache backend object for the specified cache bin. */ function get($bin) { - return new DatabaseBackend($this->connection, $this->checksumProvider, $bin); + $configuration['max_expire'] = isset($this->configuration[$bin]) ? (int) $this->configuration[$bin] : null; + 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 934fe81..434d53d 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -53,4 +53,23 @@ public function testSetGet() { $this->assertIdentical($cached_value_short, $backend->get($cid_short)->data, "Backend contains the correct value for short, non-ASCII cache id."); } + public function testCacheBinExpiration(){ + $configuration = array('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); + $this->assertIdentical($cached->expire, (string) (REQUEST_TIME + 2800), 'Maximum cache expire time is correct.'); + + $configuration = array('max_expire' => 2799); + $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 + 2799), 'Maximum cache expire time is correct.'); + + $configuration = array('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.'); + } } diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php old mode 100644 new mode 100755 index 9ad5cae..17e8215 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -691,3 +691,11 @@ # if (file_exists(__DIR__ . '/settings.local.php')) { # include __DIR__ . '/settings.local.php'; # } + +/** + * Per-bin database backend configuration. + * + * Use this setting to set the maximum cache expiration time for a specific + * cache bin in the database. + */ +$settings['cache_database']['render'] = 604800;