diff --git a/core/core.services.yml b/core/core.services.yml index 5e63508..074c639 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -207,7 +207,7 @@ services: tags: - { name: cache.bin } factory: cache_factory:get - arguments: [default] + arguments: [default, 86400] cache.entity: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php index dae24dc..c408625 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php @@ -54,16 +54,14 @@ public function __construct($root, $site_path, CacheTagsChecksumInterface $check } /** - * Gets ApcuBackend for the specified cache bin. - * - * @param $bin - * The cache bin for which the object is created. - * - * @return \Drupal\Core\Cache\ApcuBackend - * The cache backend object for the specified cache bin. + * {@inheritdoc} */ - public function get($bin) { - return new $this->backendClass($bin, $this->sitePrefix, $this->checksumProvider); + public function get($bin, $minimum_permanent_ttl = CacheBackendInterface::CACHE_PERMANENT) { + $class = new $this->backendClass($bin, $this->sitePrefix, $this->checksumProvider); + if ($class instanceOf ConfigurablePermanentTtlInterface) { + $class->setMinimumPermanentTtl($minimum_permanent_ttl); + } + return $class; } } diff --git a/core/lib/Drupal/Core/Cache/CacheFactory.php b/core/lib/Drupal/Core/Cache/CacheFactory.php index a5acf7c..1fb2134 100644 --- a/core/lib/Drupal/Core/Cache/CacheFactory.php +++ b/core/lib/Drupal/Core/Cache/CacheFactory.php @@ -53,21 +53,9 @@ public function __construct(Settings $settings, array $default_bin_backends = ar } /** - * Instantiates a cache backend class for a given cache bin. - * - * By default, this returns an instance of the - * Drupal\Core\Cache\DatabaseBackend class. - * - * Classes implementing Drupal\Core\Cache\CacheBackendInterface can register - * themselves both as a default implementation and for specific bins. - * - * @param string $bin - * The cache bin for which a cache backend object should be returned. - * - * @return \Drupal\Core\Cache\CacheBackendInterface - * The cache backend object associated with the specified bin. + * {@inheritdoc} */ - public function get($bin) { + public function get($bin, $minimum_permanent_ttl = 0) { $cache_settings = $this->settings->get('cache'); if (isset($cache_settings['bins'][$bin])) { $service_name = $cache_settings['bins'][$bin]; @@ -81,7 +69,11 @@ public function get($bin) { else { $service_name = 'cache.backend.database'; } - return $this->container->get($service_name)->get($bin); + $class = $this->container->get($service_name)->get($bin); + if ($class instanceof ConfigurablePermanentTtlInterface) { + $class->setMinimumPermanentTtl($minimum_permanent_ttl); + } + return $class; } } diff --git a/core/lib/Drupal/Core/Cache/CacheFactoryInterface.php b/core/lib/Drupal/Core/Cache/CacheFactoryInterface.php index b149373..5ee039e 100644 --- a/core/lib/Drupal/Core/Cache/CacheFactoryInterface.php +++ b/core/lib/Drupal/Core/Cache/CacheFactoryInterface.php @@ -17,10 +17,12 @@ * * @param string $bin * The cache bin for which a cache backend object should be returned. + * @param int $minimum_permanent_ttl + * The minimum ttl for permanent cache items. * * @return \Drupal\Core\Cache\CacheBackendInterface * The cache backend object associated with the specified bin. */ - public function get($bin); + public function get($bin, $minimum_permanent_ttl = CacheBackendInterface::CACHE_PERMANENT); } diff --git a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php index ca38ca0..4fd8cce 100644 --- a/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ChainedFastBackendFactory.php @@ -68,21 +68,15 @@ public function __construct(Settings $settings = NULL, $consistent_service_name } /** - * Instantiates a chained, fast cache backend class for a given cache bin. - * - * @param string $bin - * The cache bin for which a cache backend object should be returned. - * - * @return \Drupal\Core\Cache\CacheBackendInterface - * The cache backend object associated with the specified bin. + * {@inheritdoc} */ - public function get($bin) { + public function get($bin, $minimum_permanent_ttl = CacheBackendInterface::CACHE_PERMANENT) { // Use the chained backend only if there is a fast backend available; // otherwise, just return the consistent backend directly. if (isset($this->fastServiceName)) { return new ChainedFastBackend( - $this->container->get($this->consistentServiceName)->get($bin), - $this->container->get($this->fastServiceName)->get($bin), + $this->container->get($this->consistentServiceName)->get($bin, $minimum_permanent_ttl), + $this->container->get($this->fastServiceName)->get($bin, $minimum_permanent_ttl), $bin ); } diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index db5c2f0..b8dd314 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -19,7 +19,7 @@ * * @ingroup cache */ -class DatabaseBackend implements CacheBackendInterface { +class DatabaseBackend implements CacheBackendInterface, ConfigurablePermanentTtlInterface { /** * @var string @@ -42,6 +42,11 @@ class DatabaseBackend implements CacheBackendInterface { protected $checksumProvider; /** + * The minimum permanent TTL. + */ + protected $minimumPermanentTtl = CacheBackendInterface::CACHE_PERMANENT; + + /** * Constructs a DatabaseBackend object. * * @param \Drupal\Core\Database\Connection $connection @@ -194,8 +199,10 @@ protected function doSetMultiple(array $items) { $values = array(); foreach ($items as $cid => $item) { + if (!isset($item['expire']) || $item['expire'] === CacheBackendInterface::CACHE_PERMANENT) { + $item['expire'] = $this->minimumPermanentTtl; + } $item += array( - 'expire' => CacheBackendInterface::CACHE_PERMANENT, 'tags' => array(), ); @@ -356,6 +363,13 @@ public function removeBin() { } /** + * {@inheritdoc} + */ + public function setMinimumPermanentTtl($ttl) { + $this->minimumPermanentTtl = $ttl; + } + + /** * Check if the cache bin exists and create it if not. */ protected function ensureBinExists() { diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 59b0b22..ee85b3a 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -39,16 +39,12 @@ function __construct(Connection $connection, CacheTagsChecksumInterface $checksu } /** - * Gets DatabaseBackend for the specified cache bin. - * - * @param $bin - * The cache bin for which the object is created. - * - * @return \Drupal\Core\Cache\DatabaseBackend - * The cache backend object for the specified cache bin. + * {@inheritdoc} */ - function get($bin) { - return new DatabaseBackend($this->connection, $this->checksumProvider, $bin); + function get($bin, $minimum_permanent_ttl = CacheBackendInterface::CACHE_PERMANENT) { + $class = new DatabaseBackend($this->connection, $this->checksumProvider, $bin); + $class->setMinimumPermanentTtl($minimum_permanent_ttl); + return $class; } } diff --git a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php index 53e5b07..d4f8fe2 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php @@ -19,7 +19,7 @@ class MemoryBackendFactory implements CacheFactoryInterface { /** * {@inheritdoc} */ - function get($bin) { + function get($bin, $minimum_permanent_ttl = CacheBackendInterface::CACHE_PERMANENT) { if (!isset($this->bins[$bin])) { $this->bins[$bin] = new MemoryBackend($bin); }