diff --git a/core/core.services.yml b/core/core.services.yml index 3208606..385ead7 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -14,6 +14,7 @@ parameters: max-age: 0 contexts: ['session', 'user'] tags: [] + cache.configuration.override_permanent_ttl: 86400 factory.keyvalue: default: keyvalue.database http.response.debug_cacheability_headers: false @@ -207,31 +208,31 @@ services: tags: - { name: cache.bin } factory: cache_factory:get - arguments: [default, ['override_permanent_ttl', 86400]] + arguments: [default, ['override_permanent_ttl', '%cache.configuration.override_permanent_ttl%']] cache.entity: class: Drupal\Core\Cache\CacheBackendInterface tags: - { name: cache.bin } factory: cache_factory:get - arguments: [entity, ['override_permanent_ttl', 86400]] + arguments: [entity, ['override_permanent_ttl', '%cache.configuration.override_permanent_ttl%']] cache.menu: class: Drupal\Core\Cache\CacheBackendInterface tags: - { name: cache.bin } factory: cache_factory:get - arguments: [menu, ['override_permanent_ttl', 86400]] + arguments: [menu, ['override_permanent_ttl', '%cache.configuration.override_permanent_ttl%']] cache.render: class: Drupal\Core\Cache\CacheBackendInterface tags: - { name: cache.bin } factory: cache_factory:get - arguments: [render, ['override_permanent_ttl', 86400]] + arguments: [render, ['override_permanent_ttl', '%cache.configuration.override_permanent_ttl%']] cache.data: class: Drupal\Core\Cache\CacheBackendInterface tags: - { name: cache.bin } factory: cache_factory:get - arguments: [data, ['override_permanent_ttl', 86400]] + arguments: [data, ['override_permanent_ttl', '%cache.configuration.override_permanent_ttl%']] cache.discovery: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php index 06d642b..6bdda49 100644 --- a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php @@ -71,23 +71,12 @@ public function __construct(Settings $settings = NULL, $consistent_service_name * {@inheritdoc} */ public function get($bin, array $options = []) { - $consistent = $this->container->get($this->consistentServiceName); - if ($consistent instanceof ConfigurableCacheBackendFactoryInterface) { - $consistent_backend = $consistent->get($bin, $options); - } - else { - $consistent_backend = $consistent->get($bin); - } + + $consistent_backend = $this->getCacheBackendWithOptions($bin, $this->consistentServiceName, $options); // Use the chained backend only if there is a fast backend available; // otherwise, just return the consistent backend directly. if (isset($this->fastServiceName)) { - $fast = $this->container->get($this->fastServiceName); - if ($fast instanceof ConfigurableCacheBackendFactoryInterface) { - $fast_backend = $fast->get($bin, $options); - } - else { - $fast_backend = $fast->get($bin); - } + $fast_backend = $this->getCacheBackendWithOptions($bin, $this->fastServiceName, $options); return new ChainedFastBackend( $consistent_backend, $fast_backend, @@ -99,4 +88,27 @@ public function get($bin, array $options = []) { } } + /** + * Gets a cache backend with options if it supports it. + * + * @param string $bin + * The cache bin. + * @param string $name + * The service name. + * @param array $options + * The options array to pass in. + * + * @return \Drupal\Core\Cache\CacheBackendInterface + * The cache backend. + */ + protected function getCacheBackendWithOptions($bin, $name, $options) { + $factory = $this->container->get($name); + if ($factory instanceof ConfigurableCacheBackendFactoryInterface) { + return $factory->get($bin, $options); + } + else { + return $factory->get($bin); + } + } + } diff --git a/core/lib/Drupal/Core/Cache/ConfigurableCacheFactoryInterface.php b/core/lib/Drupal/Core/Cache/ConfigurableCacheFactoryInterface.php index e9b07a4..a96a087 100644 --- a/core/lib/Drupal/Core/Cache/ConfigurableCacheFactoryInterface.php +++ b/core/lib/Drupal/Core/Cache/ConfigurableCacheFactoryInterface.php @@ -5,7 +5,7 @@ /** * An interface defining configurable cache factory classes. */ -interface ConfigurableCacheFactoryInterface { +interface ConfigurableCacheFactoryInterface extends CacheFactoryInterface { /** * Gets a cache backend class for a given cache bin. diff --git a/core/lib/Drupal/Core/Cache/ConfigurablePermanentTtlInterface.php b/core/lib/Drupal/Core/Cache/ConfigurablePermanentTtlInterface.php index ed33603..bd2fe5d 100644 --- a/core/lib/Drupal/Core/Cache/ConfigurablePermanentTtlInterface.php +++ b/core/lib/Drupal/Core/Cache/ConfigurablePermanentTtlInterface.php @@ -10,17 +10,18 @@ * to allow 'jitter' to be applied so that items set immediately after a cache * clear do not all expire at the same time. Cache backends should assign jitter * higher rather than lower to avoid the risk of cache stampedes, since the - * configured ttl could potentially be set very low for some use cases. + * configured TTL could potentially be set very low for some use cases. * * @ingroup cache */ interface ConfigurablePermanentTtlInterface { /** - * Set the configured minimum permanent TTL. + * Set the permanent TTL. * * @param int $ttl + * The TTL in seconds. */ - function setOverridePermanentTtl($ttl); + function setPermanentTtl($ttl); } diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 9b50cc5..4368581 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -49,9 +49,9 @@ class DatabaseBackend implements CacheBackendInterface, ConfigurablePermanentTtl protected $checksumProvider; /** - * The overridden permanent TTL. + * The permanent TTL. */ - protected $overridePermanentTtl = CacheBackendInterface::CACHE_PERMANENT; + protected $permanentTtl = CacheBackendInterface::CACHE_PERMANENT; /** * Constructs a DatabaseBackend object. @@ -207,7 +207,7 @@ protected function doSetMultiple(array $items) { foreach ($items as $cid => $item) { if (!isset($item['expire']) || $item['expire'] === CacheBackendInterface::CACHE_PERMANENT) { - if ($this->overridePermanentTtl === CacheBackendInterface::CACHE_PERMANENT) { + if ($this->permanentTtl === CacheBackendInterface::CACHE_PERMANENT) { $item['expire'] = CacheBackendInterface::CACHE_PERMANENT; } else { @@ -259,12 +259,13 @@ protected function doSetMultiple(array $items) { } /** - * Calculate permanent expiry. + * Calculates permanent expiry. * - * @return an expires timestamp based on the override TTL with added 'jitter'. + * @return int + * An expires timestamp based on the TTL with added 'jitter'. */ protected function calculatePermanentExpire() { - $minimum = $this->overridePermanentTtl; + $minimum = $this->permanentTtl; return rand($minimum, $minimum * self::JITTER_MULTIPLIER) + REQUEST_TIME; } @@ -387,8 +388,8 @@ public function removeBin() { /** * {@inheritdoc} */ - public function setOverridePermanentTtl($ttl) { - $this->overridePermanentTtl = $ttl; + public function setPermanentTtl($ttl) { + $this->permanentTtl = $ttl; } /** diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php index cf6767f..35839ed 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -55,11 +55,11 @@ public function testSetGet() { } /** - * Test overridePermanentTtl. + * Test permanent TTL. */ - function testOverridePermanentTtl() { + function testPermanentTtl() { $backend = $this->getCacheBackend(); - $backend->setOverridePermanentTtl(2); + $backend->setPermanentTtl(2); $cid = 'test'; $value = 'test'; $backend->set($cid, $value);