diff --git a/core/core.services.yml b/core/core.services.yml index 3e2feb6..c06c3b6 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -32,6 +32,15 @@ services: class: Drupal\Core\Cache\TimeZoneCacheContext tags: - { name: cache.context} + cache_tags: + class: Drupal\Core\Cache\CacheTagHandler + tags: + - { name: service_collector } + cache_tag_storage: + class: Drupal\Core\Cache\DatabaseCacheTagStorage + arguments: ['@database'] + tags: + - { name: cache_tags } cache.backend.chainedfast: class: Drupal\Core\Cache\ChainedFastBackendFactory arguments: ['@settings'] @@ -39,12 +48,16 @@ services: - [setContainer, ['@service_container']] cache.backend.database: class: Drupal\Core\Cache\DatabaseBackendFactory - arguments: ['@database'] + arguments: ['@database', '@cache_tag_storage'] cache.backend.apcu: class: Drupal\Core\Cache\ApcuBackendFactory arguments: ['@app.root'] + tags: + - { name: cache_tags } cache.backend.php: class: Drupal\Core\Cache\PhpBackendFactory + tags: + - { name: cache_tags } cache.bootstrap: class: Drupal\Core\Cache\CacheBackendInterface tags: diff --git a/core/lib/Drupal/Core/Cache/ApcuBackend.php b/core/lib/Drupal/Core/Cache/ApcuBackend.php index 50fb0fd..ec612eb 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackend.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php @@ -10,7 +10,7 @@ /** * Stores cache items in the Alternative PHP Cache User Cache (APCu). */ -class ApcuBackend implements CacheBackendInterface { +class ApcuBackend implements CacheBackendInterface, CacheTagInvalidationInterface { /** * The name of the cache bin to use. diff --git a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php index ffe5993..dd77ed1 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackendFactory.php @@ -9,7 +9,7 @@ use \Drupal\Component\Utility\Crypt; -class ApcuBackendFactory implements CacheFactoryInterface { +class ApcuBackendFactory implements CacheFactoryInterface, CacheTagInvalidationInterface { /** * The site prefix string. @@ -41,4 +41,43 @@ public function get($bin) { return new ApcuBackend($bin, $this->sitePrefix); } + /** + * Deletes items with any of the specified tags. + * + * If the cache items are being deleted because they are no longer "fresh", + * you may consider using invalidateTags() instead. This allows callers to + * retrieve the invalid items by calling get() with $allow_invalid set to TRUE. + * In some cases an invalid item may be acceptable rather than having to + * rebuild the cache. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::invalidateTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::delete() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll() + */ + public function deleteTags(array $tags) { + // TODO: Implement deleteTags() method. + } + + /** + * Marks cache items with any of the specified tags as invalid. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::deleteTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() + */ + public function invalidateTags(array $tags) { + // TODO: Implement invalidateTags() method. + } } diff --git a/core/lib/Drupal/Core/Cache/Cache.php b/core/lib/Drupal/Core/Cache/Cache.php index ddf444e..c88171e 100644 --- a/core/lib/Drupal/Core/Cache/Cache.php +++ b/core/lib/Drupal/Core/Cache/Cache.php @@ -96,41 +96,21 @@ public static function buildTags($prefix, array $suffixes) { /** * Deletes items from all bins with any of the specified tags. * - * Many sites have more than one active cache backend, and each backend may - * use a different strategy for storing tags against cache items, and - * deleting cache items associated with a given tag. - * - * When deleting a given list of tags, we iterate over each cache backend, and - * and call deleteTags() on each. - * * @param string[] $tags * The list of tags to delete cache items for. */ public static function deleteTags(array $tags) { - static::validateTags($tags); - foreach (static::getBins() as $cache_backend) { - $cache_backend->deleteTags($tags); - } + \Drupal::service('cache_tags')->deleteTags($tags); } /** * Marks cache items from all bins with any of the specified tags as invalid. * - * Many sites have more than one active cache backend, and each backend my use - * a different strategy for storing tags against cache items, and invalidating - * cache items associated with a given tag. - * - * When invalidating a given list of tags, we iterate over each cache backend, - * and call invalidateTags() on each. - * * @param string[] $tags * The list of tags to invalidate cache items for. */ public static function invalidateTags(array $tags) { - static::validateTags($tags); - foreach (static::getBins() as $cache_backend) { - $cache_backend->invalidateTags($tags); - } + \Drupal::service('cache_tags')->invalidateTags($tags); } /** diff --git a/core/lib/Drupal/Core/Cache/CacheBackendInterface.php b/core/lib/Drupal/Core/Cache/CacheBackendInterface.php index 39c8c26..4de8e23 100644 --- a/core/lib/Drupal/Core/Cache/CacheBackendInterface.php +++ b/core/lib/Drupal/Core/Cache/CacheBackendInterface.php @@ -161,27 +161,6 @@ public function delete($cid); public function deleteMultiple(array $cids); /** - * Deletes items with any of the specified tags. - * - * If the cache items are being deleted because they are no longer "fresh", - * you may consider using invalidateTags() instead. This allows callers to - * retrieve the invalid items by calling get() with $allow_invalid set to TRUE. - * In some cases an invalid item may be acceptable rather than having to - * rebuild the cache. - * - * @param array $tags - * Associative array of tags, in the same format that is passed to - * CacheBackendInterface::set(). - * - * @see \Drupal\Core\Cache\CacheBackendInterface::set() - * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateTags() - * @see \Drupal\Core\Cache\CacheBackendInterface::delete() - * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple() - * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll() - */ - public function deleteTags(array $tags); - - /** * Deletes all cache items in a bin. * * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() @@ -224,21 +203,6 @@ public function invalidate($cid); public function invalidateMultiple(array $cids); /** - * Marks cache items with any of the specified tags as invalid. - * - * @param array $tags - * Associative array of tags, in the same format that is passed to - * CacheBackendInterface::set(). - * - * @see \Drupal\Core\Cache\CacheBackendInterface::set() - * @see \Drupal\Core\Cache\CacheBackendInterface::deleteTags() - * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate() - * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple() - * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() - */ - public function invalidateTags(array $tags); - - /** * Marks all cache items as invalid. * * Invalid items may be returned in later calls to get(), if the $allow_invalid diff --git a/core/lib/Drupal/Core/Cache/CacheTagHandler.php b/core/lib/Drupal/Core/Cache/CacheTagHandler.php new file mode 100644 index 0000000..d439954 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/CacheTagHandler.php @@ -0,0 +1,38 @@ +invalidators as $invalidator) { + $invalidator->invalidateTags($tags); + } + } + + public function deleteTags(array $tags) { + foreach ($this->invalidators as $invalidator) { + $invalidator->deleteTags($tags); + } + } + + public function addHandler(CacheTagInvalidationInterface $service) { + $this->invalidators[] = $service; + } + +} diff --git a/core/lib/Drupal/Core/Cache/CacheTagHandlerInterface.php b/core/lib/Drupal/Core/Cache/CacheTagHandlerInterface.php new file mode 100644 index 0000000..288e04b --- /dev/null +++ b/core/lib/Drupal/Core/Cache/CacheTagHandlerInterface.php @@ -0,0 +1,31 @@ +bin = $bin; $this->connection = $connection; + $this->cacheTagStorage = $cacheTagStorage; } /** @@ -174,8 +184,8 @@ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array * Actually set the cache. */ protected function doSet($cid, $data, $expire, $tags) { - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::deletedTags', array()); - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); + $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::deletedTags', array()); + $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::invalidatedTags', array()); // Remove tags that were already deleted or invalidated during this request // from the static caches so that another deletion or invalidation can // occur. @@ -215,8 +225,8 @@ protected function doSet($cid, $data, $expire, $tags) { * {@inheritdoc} */ public function setMultiple(array $items) { - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::deletedTags', array()); - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); + $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::deletedTags', array()); + $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::invalidatedTags', array()); // Use a transaction so that the database can write the changes in a single // commit. @@ -319,28 +329,7 @@ public function deleteMultiple(array $cids) { /** * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags(). */ - public function deleteTags(array $tags) { - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheBackendInterface::tagCache', array()); - $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::deletedTags', array()); - foreach ($tags as $tag) { - // Only delete tags once per request unless they are written again. - if (isset($deleted_tags[$tag])) { - continue; - } - $deleted_tags[$tag] = TRUE; - unset($tag_cache[$tag]); - try { - $this->connection->merge('cachetags') - ->insertFields(array('deletions' => 1)) - ->expression('deletions', 'deletions + 1') - ->key('tag', $tag) - ->execute(); - } - catch (\Exception $e) { - $this->catchException($e, 'cachetags'); - } - } - } + public function deleteTags(array $tags) {} /** * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll(). @@ -386,30 +375,9 @@ public function invalidateMultiple(array $cids) { } /** - * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). + * Implements Drupal\Core\Cache\CacheTagHandlerInterface::invalidateTags(). */ - public function invalidateTags(array $tags) { - try { - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheBackendInterface::tagCache', array()); - $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseBackend::invalidatedTags', array()); - foreach ($tags as $tag) { - // Only invalidate tags once per request unless they are written again. - if (isset($invalidated_tags[$tag])) { - continue; - } - $invalidated_tags[$tag] = TRUE; - unset($tag_cache[$tag]); - $this->connection->merge('cachetags') - ->insertFields(array('invalidations' => 1)) - ->expression('invalidations', 'invalidations + 1') - ->key('tag', $tag) - ->execute(); - } - } - catch (\Exception $e) { - $this->catchException($e, 'cachetags'); - } - } + public function invalidateTags(array $tags) {} /** * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). @@ -452,7 +420,7 @@ public function garbageCollection() { * Sum of all invalidations. */ protected function checksumTags(array $tags) { - $tag_cache = &drupal_static('Drupal\Core\Cache\CacheBackendInterface::tagCache', array()); + $tag_cache = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::tagCache', array()); $checksum = array( 'invalidations' => 0, @@ -516,14 +484,16 @@ protected function ensureBinExists() { /** * Act on an exception when cache might be stale. * - * If the {cachetags} table does not yet exist, that's fine but if the table + * If the table does not yet exist, that's fine, but if the table * exists and yet the query failed, then the cache is stale and the * exception needs to propagate. * * @param $e * The exception. * @param string|null $table_name - * The table name, defaults to $this->bin. Can be cachetags. + * The table name. Defaults to $this->bin. + * + * @throws \Exception */ protected function catchException(\Exception $e, $table_name = NULL) { if ($this->connection->schema()->tableExists($table_name ?: $this->bin)) { diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 1d70164..2772bab 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -23,8 +23,9 @@ class DatabaseBackendFactory implements CacheFactoryInterface { * * @param \Drupal\Core\Database\Connection $connection */ - function __construct(Connection $connection) { + function __construct(Connection $connection, CacheTagInvalidationStorageInterface $cacheTagStorage) { $this->connection = $connection; + $this->cacheTagStorage = $cacheTagStorage; } /** @@ -37,7 +38,7 @@ function __construct(Connection $connection) { * The cache backend object for the specified cache bin. */ function get($bin) { - return new DatabaseBackend($this->connection, $bin); + return new DatabaseBackend($this->connection, $this->cacheTagStorage, $bin); } } diff --git a/core/lib/Drupal/Core/Cache/DatabaseCacheTagStorage.php b/core/lib/Drupal/Core/Cache/DatabaseCacheTagStorage.php new file mode 100644 index 0000000..0a9259c --- /dev/null +++ b/core/lib/Drupal/Core/Cache/DatabaseCacheTagStorage.php @@ -0,0 +1,123 @@ +connection = $connection; + } + + /** + * Stores cache tag invalidations. + * + * @param string[] $tags + * The list of tags for which to store invalidations. + */ + public function invalidateTags(array $tags) { + try { + $tag_cache = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::tagCache', array()); + $invalidated_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::invalidatedTags', array()); + foreach ($tags as $tag) { + // Only invalidate tags once per request unless they are written again. + if (isset($invalidated_tags[$tag])) { + continue; + } + $invalidated_tags[$tag] = TRUE; + unset($tag_cache[$tag]); + $this->connection->merge('cachetags') + ->insertFields(array('invalidations' => 1)) + ->expression('invalidations', 'invalidations + 1') + ->key('tag', $tag) + ->execute(); + } + } + catch (\Exception $e) { + $this->catchException($e, 'cachetags'); + } + } + + /** + * Stores cache tag deletions. + * + * @param string[] $tags + * The list of tags for which to store deletions. + */ + public function deleteTags(array $tags) { + $tag_cache = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::tagCache', array()); + $deleted_tags = &drupal_static('Drupal\Core\Cache\DatabaseCacheTagStorage::deletedTags', array()); + foreach ($tags as $tag) { + // Only delete tags once per request unless they are written again. + if (isset($deleted_tags[$tag])) { + continue; + } + $deleted_tags[$tag] = TRUE; + unset($tag_cache[$tag]); + try { + $this->connection->merge('cachetags') + ->insertFields(array('deletions' => 1)) + ->expression('deletions', 'deletions + 1') + ->key('tag', $tag) + ->execute(); + } + catch (\Exception $e) { + $this->catchException($e); + } + } + } + + /** + * {@inheritdoc} + */ + public function getTagInvalidations() { + + } + + /** + * {@inheritdoc} + */ + public function getTagDeletions() { + + } + + /** + * Act on an exception when cache might be stale. + * + * If the {cachetags} table does not yet exist, that's fine but if the table + * exists and yet the query failed, then the cache is stale and the + * exception needs to propagate. + * + * @param \Exception $e + * The exception. + * + * @throws \Exception + */ + protected function catchException(\Exception $e) { + if ($this->connection->schema()->tableExists('cachetags')) { + throw $e; + } + } +} diff --git a/core/lib/Drupal/Core/Cache/MemoryBackend.php b/core/lib/Drupal/Core/Cache/MemoryBackend.php index 23fddc5..e03660f 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackend.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackend.php @@ -17,7 +17,7 @@ * * @ingroup cache */ -class MemoryBackend implements CacheBackendInterface { +class MemoryBackend implements CacheBackendInterface, CacheTagInvalidationInterface { /** * Array to store cache objects. diff --git a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php index c9e288d..7317d39 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -class MemoryBackendFactory implements CacheFactoryInterface { +class MemoryBackendFactory implements CacheFactoryInterface, CacheTagInvalidationInterface { /** * {@inheritdoc} @@ -16,4 +16,43 @@ function get($bin) { return new MemoryBackend($bin); } + /** + * Deletes items with any of the specified tags. + * + * If the cache items are being deleted because they are no longer "fresh", + * you may consider using invalidateTags() instead. This allows callers to + * retrieve the invalid items by calling get() with $allow_invalid set to TRUE. + * In some cases an invalid item may be acceptable rather than having to + * rebuild the cache. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::invalidateTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::delete() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll() + */ + public function deleteTags(array $tags) { + // TODO: Implement deleteTags() method. + } + + /** + * Marks cache items with any of the specified tags as invalid. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::deleteTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() + */ + public function invalidateTags(array $tags) { + // TODO: Implement invalidateTags() method. + } } diff --git a/core/lib/Drupal/Core/Cache/NullBackend.php b/core/lib/Drupal/Core/Cache/NullBackend.php index d27bc13..c54e1a8 100644 --- a/core/lib/Drupal/Core/Cache/NullBackend.php +++ b/core/lib/Drupal/Core/Cache/NullBackend.php @@ -20,7 +20,7 @@ * * @ingroup cache */ -class NullBackend implements CacheBackendInterface { +class NullBackend implements CacheBackendInterface, CacheTagInvalidationInterface { /** * Constructs a NullBackend object. @@ -70,7 +70,7 @@ public function deleteMultiple(array $cids) {} public function deleteAll() {} /** - * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags(). + * Implements Drupal\Core\Cache\CacheTagInvalidationInterface::deleteTags(). */ public function deleteTags(array $tags) {} @@ -85,7 +85,7 @@ public function invalidate($cid) {} public function invalidateMultiple(array $cids) {} /** - * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). + * Implements Drupal\Core\Cache\CacheTagInvalidationInterface::invalidateTags(). */ public function invalidateTags(array $tags) {} diff --git a/core/lib/Drupal/Core/Cache/NullBackendFactory.php b/core/lib/Drupal/Core/Cache/NullBackendFactory.php index 0101a8e..b1d9365 100644 --- a/core/lib/Drupal/Core/Cache/NullBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/NullBackendFactory.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -class NullBackendFactory implements CacheFactoryInterface { +class NullBackendFactory implements CacheFactoryInterface, CacheTagInvalidationInterface { /** * {@inheritdoc} @@ -16,4 +16,43 @@ function get($bin) { return new NullBackend($bin); } + /** + * Deletes items with any of the specified tags. + * + * If the cache items are being deleted because they are no longer "fresh", + * you may consider using invalidateTags() instead. This allows callers to + * retrieve the invalid items by calling get() with $allow_invalid set to TRUE. + * In some cases an invalid item may be acceptable rather than having to + * rebuild the cache. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::invalidateTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::delete() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll() + */ + public function deleteTags(array $tags) { + // TODO: Implement deleteTags() method. + } + + /** + * Marks cache items with any of the specified tags as invalid. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::deleteTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() + */ + public function invalidateTags(array $tags) { + // TODO: Implement invalidateTags() method. + } } diff --git a/core/lib/Drupal/Core/Cache/NullCacheTagHandler.php b/core/lib/Drupal/Core/Cache/NullCacheTagHandler.php new file mode 100644 index 0000000..76b4c41 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/NullCacheTagHandler.php @@ -0,0 +1,30 @@ +invalidators[] = $service; + } + +} diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php index e188ffb..699e9e7 100644 --- a/core/lib/Drupal/Core/Cache/PhpBackend.php +++ b/core/lib/Drupal/Core/Cache/PhpBackend.php @@ -23,7 +23,7 @@ * * @ingroup cache */ -class PhpBackend implements CacheBackendInterface { +class PhpBackend implements CacheBackendInterface, CacheTagInvalidationInterface { /** * @var string diff --git a/core/lib/Drupal/Core/Cache/PhpBackendFactory.php b/core/lib/Drupal/Core/Cache/PhpBackendFactory.php index 0801b72..872bf16 100644 --- a/core/lib/Drupal/Core/Cache/PhpBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/PhpBackendFactory.php @@ -7,7 +7,7 @@ namespace Drupal\Core\Cache; -class PhpBackendFactory implements CacheFactoryInterface { +class PhpBackendFactory implements CacheFactoryInterface, CacheTagInvalidationInterface { /** * Gets PhpBackend for the specified cache bin. @@ -22,4 +22,43 @@ function get($bin) { return new PhpBackend($bin); } + /** + * Deletes items with any of the specified tags. + * + * If the cache items are being deleted because they are no longer "fresh", + * you may consider using invalidateTags() instead. This allows callers to + * retrieve the invalid items by calling get() with $allow_invalid set to TRUE. + * In some cases an invalid item may be acceptable rather than having to + * rebuild the cache. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::invalidateTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::delete() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::deleteAll() + */ + public function deleteTags(array $tags) { + // TODO: Implement deleteTags() method. + } + + /** + * Marks cache items with any of the specified tags as invalid. + * + * @param array $tags + * Associative array of tags, in the same format that is passed to + * CacheBackendInterface::set(). + * + * @see \Drupal\Core\Cache\CacheBackendInterface::set() + * @see \Drupal\Core\Cache\CacheTagInvalidationInterface::deleteTags() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidate() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple() + * @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll() + */ + public function invalidateTags(array $tags) { + // TODO: Implement invalidateTags() method. + } } diff --git a/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php b/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php index 6e93561..af823f7 100644 --- a/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php +++ b/core/lib/Drupal/Core/Installer/InstallerServiceProvider.php @@ -48,6 +48,8 @@ public function register(ContainerBuilder $container) { ->addArgument(new Reference('request_stack')); $container ->register('router.dumper', 'Drupal\Core\Routing\NullMatcherDumper'); + $container + ->register('cache_tags', 'Drupal\Core\Cache\NullCacheTagHandler'); // Replace the route builder with an empty implementation. // @todo Convert installer steps into routes; add an installer.routing.yml. diff --git a/sites/development.services.yml b/sites/development.services.yml index cc21211..75c5f44 100644 --- a/sites/development.services.yml +++ b/sites/development.services.yml @@ -5,5 +5,9 @@ services: cache.backend.memory: class: Drupal\Core\Cache\MemoryBackendFactory + tags: + - { name: cache_tags } cache.backend.null: class: Drupal\Core\Cache\NullBackendFactory + tags: + - { name: cache_tags }