diff --git a/core/includes/cache.inc b/core/includes/cache.inc index 74afcbf..d6e499e 100644 --- a/core/includes/cache.inc +++ b/core/includes/cache.inc @@ -5,6 +5,7 @@ * Functions and interfaces for cache handling. */ +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheFactory; /** @@ -26,21 +27,10 @@ * @see Drupal\Core\Cache\CacheBackendInterface */ function cache($bin = 'cache') { - // Use the advanced drupal_static() pattern, since this is called very often. - static $drupal_static_fast; - if (!isset($drupal_static_fast)) { - $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__, array()); - } - $cache_objects = &$drupal_static_fast['cache']; - // Temporary backwards compatibiltiy layer, allow old style prefixed cache // bin names to be passed as arguments. $bin = str_replace('cache_', '', $bin); - - if (!isset($cache_objects[$bin])) { - $cache_objects[$bin] = CacheFactory::get($bin); - } - return $cache_objects[$bin]; + return drupal_container()->get("cache.$bin"); } /** @@ -57,9 +47,9 @@ function cache($bin = 'cache') { * The list of tags to delete cache items for. */ function cache_delete_tags(array $tags) { - foreach (CacheFactory::getBackends() as $bin => $class) { - cache($bin)->deleteTags($tags); - } + array_map(function (CacheBackendInterface $cache_backend) use ($tags) { + $cache_backend->deleteTags($tags); + }, cache_get_bins()); } /** @@ -76,7 +66,21 @@ function cache_delete_tags(array $tags) { * The list of tags to invalidate cache items for. */ function cache_invalidate_tags(array $tags) { - foreach (CacheFactory::getBackends() as $bin => $class) { - cache($bin)->invalidateTags($tags); + array_map(function (CacheBackendInterface $cache_backend) use ($tags) { + $cache_backend->invalidateTags($tags); + }, cache_get_bins()); +} + +/** + * @return array + * An array of cache backend objects keyed by cache bins. + */ +function cache_get_bins() { + $bins = array(); + $container = drupal_container(); + foreach ($container->getParameter('cache_bins') as $service_id => $bin) { + $bins[$bin] = $container->get($service_id); } + return $bins; } + diff --git a/core/includes/common.inc b/core/includes/common.inc index 88d6fd3..1d3d4b7 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -6311,8 +6311,7 @@ function drupal_implode_tags($tags) { * * At times, it is necessary to re-initialize the entire system to account for * changed or new code. This function: - * - Clears all persistent caches (invoking hook_cache_flush()), which always - * includes: + * - Clears all persistent caches (invoking cache_get_bins()): * - The bootstrap cache bin containing base system, module system, and theme * system information. * - The common 'cache' cache bin containing arbitrary caches. @@ -6364,8 +6363,18 @@ function drupal_flush_all_caches() { // Flush all persistent caches. // This is executed based on old/previously known information, which is // sufficient, since new extensions cannot have any primed caches yet. - foreach (module_invoke_all('cache_flush') as $bin) { - cache($bin)->deleteAll(); + module_invoke_all('cache_flush'); + // @todo Once http://drupal.org/node/512026 is in, decide what to do with + // menu and convert this to array_map as well. + /* + array_map(function (CacheBackendInterface $cache_backend) { + $cache_backend->deleteAll(); + }, cache_get_bins()); + */ + foreach (cache_get_bins() as $bin => $cache_backend) { + if ($bin != 'form' && $bin != 'menu') { + $cache_backend->deleteAll(); + } } // Flush asset file caches. diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 44e5a85..e8969f9 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -350,6 +350,11 @@ function install_begin_request(&$install_state) { // Register the 'language_manager' service. $container->register('language_manager', 'Drupal\Core\Language\LanguageManager'); + foreach (array('bootstrap', 'config', 'cache', 'form', 'menu', 'page', 'path') as $bin) { + $container + ->register("cache.$bin", 'Drupal\Core\Cache\MemoryBackend') + ->addArgument($bin); + } // The install process cannot use the database lock backend since the database // is not fully up, so we use a null backend implementation during the @@ -390,7 +395,6 @@ function install_begin_request(&$install_state) { $module_handler->load('system'); require_once DRUPAL_ROOT . '/core/includes/cache.inc'; - $conf['cache_classes'] = array('cache' => 'Drupal\Core\Cache\MemoryBackend'); // Prepare for themed output. We need to run this at the beginning of the // page request to avoid a different theme accidentally getting set. (We also diff --git a/core/lib/Drupal/Core/Cache/CacheFactory.php b/core/lib/Drupal/Core/Cache/CacheFactory.php index da42a26..747f287 100644 --- a/core/lib/Drupal/Core/Cache/CacheFactory.php +++ b/core/lib/Drupal/Core/Cache/CacheFactory.php @@ -10,7 +10,21 @@ /** * Defines the cache backend factory. */ -class CacheFactory { +use Drupal\Component\Utility\Settings; +use Symfony\Component\DependencyInjection\ContainerAware; + +class CacheFactory extends ContainerAware { + + /** + * The settings array. + * + * @var \Drupal\Component\Utility\Settings + */ + protected $settings; + + function __construct(Settings $settings) { + $this->settings = $settings; + } /** * Instantiates a cache backend class for a given cache bin. @@ -24,33 +38,21 @@ class CacheFactory { * @param string $bin * The cache bin for which a cache backend object should be returned. * - * @return Drupal\Core\Cache\CacheBackendInterface + * @return \Drupal\Core\Cache\CacheBackendInterface * The cache backend object associated with the specified bin. */ - public static function get($bin) { - // Check whether there is a custom class defined for the requested bin or - // use the default 'cache' definition otherwise. - $cache_backends = self::getBackends(); - $class = isset($cache_backends[$bin]) ? $cache_backends[$bin] : $cache_backends['cache']; - return new $class($bin); - } - - /** - * Returns a list of cache backends for this site. - * - * @return array - * An associative array with cache bins as keys, and backend class names as - * value. - */ - public static function getBackends() { - // @todo Improve how cache backend classes are defined. Cannot be - // configuration, since e.g. the CachedStorage config storage controller - // requires the definition in its constructor already. - global $conf; - $cache_backends = isset($conf['cache_classes']) ? $conf['cache_classes'] : array(); - // Ensure there is a default 'cache' bin definition. - $cache_backends += array('cache' => 'Drupal\Core\Cache\DatabaseBackend'); - return $cache_backends; + public function get($bin) { + $cache_settings = $this->settings->get('cache'); + if (isset($cache_settings[$bin])) { + $service_name = $cache_settings[$bin]; + } + elseif (isset($cache_settings['default'])) { + $service_name = $cache_settings['default']; + } + else { + $service_name = 'cache.backend.database'; + } + return $this->container->get($service_name)->get($bin); } } diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index e4d86c6..b28a1f6 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -23,19 +23,28 @@ class DatabaseBackend implements CacheBackendInterface { */ protected $bin; + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a DatabaseBackend object. * * @param string $bin * The cache bin for which the object is created. */ - public function __construct($bin) { + public function __construct($connection, $bin) { // All cache tables should be prefixed with 'cache_', except for the // default 'cache' bin. if ($bin != 'cache') { $bin = 'cache_' . $bin; } $this->bin = $bin; + $this->connection = $connection; } /** @@ -58,7 +67,7 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) { // is used here only due to the performance overhead we would incur // otherwise. When serving an uncached page, the overhead of using // ::select() is a much smaller proportion of the request. - $result = Database::getConnection()->query('SELECT cid, data, created, expire, serialized, tags, checksum_invalidations, checksum_deletions FROM {' . Database::getConnection()->escapeTable($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids)); + $result = $this->connection->query('SELECT cid, data, created, expire, serialized, tags, checksum_invalidations, checksum_deletions FROM {' . $this->connection->escapeTable($this->bin) . '} WHERE cid IN (:cids)', array(':cids' => $cids)); $cache = array(); foreach ($result as $item) { $item = $this->prepareItem($item, $allow_invalid); @@ -142,7 +151,7 @@ public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANEN $fields['serialized'] = 0; } - Database::getConnection()->merge($this->bin) + $this->connection->merge($this->bin) ->key(array('cid' => $cid)) ->fields($fields) ->execute(); @@ -152,7 +161,7 @@ public function set($cid, $data, $expire = CacheBackendInterface::CACHE_PERMANEN * Implements Drupal\Core\Cache\CacheBackendInterface::delete(). */ public function delete($cid) { - Database::getConnection()->delete($this->bin) + $this->connection->delete($this->bin) ->condition('cid', $cid) ->execute(); } @@ -163,7 +172,7 @@ public function delete($cid) { public function deleteMultiple(array $cids) { // Delete in chunks when a large array is passed. do { - Database::getConnection()->delete($this->bin) + $this->connection->delete($this->bin) ->condition('cid', array_splice($cids, 0, 1000), 'IN') ->execute(); } @@ -177,7 +186,7 @@ public function deleteTags(array $tags) { $tag_cache = &drupal_static('Drupal\Core\Cache\CacheBackendInterface::tagCache'); foreach ($this->flattenTags($tags) as $tag) { unset($tag_cache[$tag]); - Database::getConnection()->merge('cache_tags') + $this->connection->merge('cache_tags') ->insertFields(array('deletions' => 1)) ->expression('deletions', 'deletions + 1') ->key(array('tag' => $tag)) @@ -189,14 +198,14 @@ public function deleteTags(array $tags) { * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll(). */ public function deleteAll() { - Database::getConnection()->truncate($this->bin)->execute(); + $this->connection->truncate($this->bin)->execute(); } /** * Implements Drupal\Core\Cache\CacheBackendInterface::deleteExpired(). */ public function deleteExpired() { - Database::getConnection()->delete($this->bin) + $this->connection->delete($this->bin) ->condition('expire', CacheBackendInterface::CACHE_PERMANENT, '<>') ->condition('expire', REQUEST_TIME, '<') ->execute(); @@ -215,7 +224,7 @@ public function invalidate($cid) { public function invalidateMultiple(array $cids) { // Update in chunks when a large array is passed. do { - Database::getConnection()->update($this->bin) + $this->connection->update($this->bin) ->fields(array('expire' => REQUEST_TIME - 1)) ->condition('cid', array_splice($cids, 0, 1000), 'IN') ->execute(); @@ -230,7 +239,7 @@ public function invalidateTags(array $tags) { $tag_cache = &drupal_static('Drupal\Core\Cache\CacheBackendInterface::tagCache'); foreach ($this->flattenTags($tags) as $tag) { unset($tag_cache[$tag]); - Database::getConnection()->merge('cache_tags') + $this->connection->merge('cache_tags') ->insertFields(array('invalidations' => 1)) ->expression('invalidations', 'invalidations + 1') ->key(array('tag' => $tag)) @@ -242,7 +251,7 @@ public function invalidateTags(array $tags) { * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). */ public function invalidateAll() { - Database::getConnection()->update($this->bin) + $this->connection->update($this->bin) ->fields(array('expire' => REQUEST_TIME - 1)) ->execute(); } @@ -303,7 +312,7 @@ protected function checksumTags($flat_tags) { $query_tags = array_diff($flat_tags, array_keys($tag_cache)); if ($query_tags) { - $db_tags = Database::getConnection()->query('SELECT tag, invalidations, deletions FROM {cache_tags} WHERE tag IN (:tags)', array(':tags' => $query_tags))->fetchAllAssoc('tag', \PDO::FETCH_ASSOC); + $db_tags = $this->connection->query('SELECT tag, invalidations, deletions FROM {cache_tags} WHERE tag IN (:tags)', array(':tags' => $query_tags))->fetchAllAssoc('tag', \PDO::FETCH_ASSOC); $tag_cache += $db_tags; // Fill static cache with empty objects for tags not found in the database. @@ -323,7 +332,7 @@ protected function checksumTags($flat_tags) { */ public function isEmpty() { $this->garbageCollection(); - $query = Database::getConnection()->select($this->bin); + $query = $this->connection->select($this->bin); $query->addExpression('1'); $result = $query->range(0, 1) ->execute() diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php new file mode 100644 index 0000000..2505099 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -0,0 +1,29 @@ +connection = $connection; + } + + function get($bin) { + return new DatabaseBackend($this->connection, $bin); + } + +} diff --git a/core/lib/Drupal/Core/Cache/ListCacheBins.php b/core/lib/Drupal/Core/Cache/ListCacheBins.php new file mode 100644 index 0000000..cd54a16 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/ListCacheBins.php @@ -0,0 +1,30 @@ +findTaggedServiceIds('cache.bin') as $id => $attributes) { + $cache_bins[$id] = substr($id, 6); + } + $container->setParameter('cache_bins', $cache_bins); + } +} diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 88b25bf..8868e18 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -37,17 +37,30 @@ class CoreBundle extends Bundle { */ public function build(ContainerBuilder $container) { + // Register cache services. + foreach (array('bootstrap', 'config', 'cache', 'form', 'menu', 'page', 'path') as $bin) { + $container + ->register("cache.$bin", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('bootstrap') + ->addTag('cache.bin'); + } + + $container + ->register('cache_factory', 'Drupal\Core\Cache\CacheFactory') + ->addArgument(new Reference('settings')) + ->addMethodCall('setContainer', array(new Reference('service_container'))); + + $container + ->register('cache.backend.database', 'Drupal\Core\Cache\DatabaseBackendFactory') + ->addArgument(new Reference('database')); + $container->addCompilerPass(new \Drupal\Core\Cache\ListCacheBins()); + // Register active configuration storage. $container ->register('config.cachedstorage.storage', 'Drupal\Core\Config\FileStorage') ->addArgument(config_get_config_directory(CONFIG_ACTIVE_DIRECTORY)); - // @todo Replace this with a cache.factory service plus 'config' argument. - $container - ->register('cache.config', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('config'); - $container ->register('config.storage', 'Drupal\Core\Config\CachedStorage') ->addArgument(new Reference('config.cachedstorage.storage')) @@ -155,17 +168,6 @@ public function build(ContainerBuilder $container) { $container->register('controller_resolver', 'Drupal\Core\ControllerResolver') ->addArgument(new Reference('service_container')); - $container - ->register('cache.cache', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('cache'); - $container - ->register('cache.bootstrap', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('bootstrap'); - $this->registerModuleHandler($container); $container->register('http_kernel', 'Drupal\Core\HttpKernel') @@ -209,12 +211,6 @@ public function build(ContainerBuilder $container) { ->addArgument(new Reference('event_dispatcher')) ->addArgument(new Reference('module_handler')); - $container - ->register('cache.path', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('path'); - $container->register('path.alias_manager.cached', 'Drupal\Core\CacheDecorator\AliasManagerCacheDecorator') ->addArgument(new Reference('path.alias_manager')) ->addArgument(new Reference('cache.path')); diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 18b9d27..3e3c4af 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -497,13 +497,6 @@ function _block_get_renderable_block($element) { } /** - * Implements hook_cache_flush(). - */ -function block_cache_flush() { - return array('block'); -} - -/** * Implements hook_rebuild(). */ function block_rebuild() { diff --git a/core/modules/block/lib/Drupal/block/BlockBundle.php b/core/modules/block/lib/Drupal/block/BlockBundle.php index 71dfd33..6eeb594 100644 --- a/core/modules/block/lib/Drupal/block/BlockBundle.php +++ b/core/modules/block/lib/Drupal/block/BlockBundle.php @@ -22,6 +22,12 @@ public function build(ContainerBuilder $container) { // Register the BlockManager class with the dependency injection container. $container->register('plugin.manager.block', 'Drupal\block\Plugin\Type\BlockManager') ->addArgument('%container.namespaces%'); + $container + ->register('cache.block', 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('block') + ->addTag('cache.bin'); } } diff --git a/core/modules/config/tests/config_test/config_test.module b/core/modules/config/tests/config_test/config_test.module index d0d59fd..4aa95f7 100644 --- a/core/modules/config/tests/config_test/config_test.module +++ b/core/modules/config/tests/config_test/config_test.module @@ -146,8 +146,6 @@ function config_test_delete_form_submit($form, &$form_state) { function config_test_cache_flush() { // Set a global value we can check in test code. $GLOBALS['hook_cache_flush'] = __FUNCTION__; - - return array(); } /** diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 6cdd628..9b51eb3 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -504,14 +504,6 @@ function field_language_fallback(&$field_langcodes, EntityInterface $entity, $la } /** - * Implements hook_cache_flush(). - */ -function field_cache_flush() { - // Request a flush of our cache table. - return array('field'); -} - -/** * Implements hook_rebuild(). */ function field_rebuild() { diff --git a/core/modules/field/lib/Drupal/field/FieldBundle.php b/core/modules/field/lib/Drupal/field/FieldBundle.php index b491a9c..bca3b05 100644 --- a/core/modules/field/lib/Drupal/field/FieldBundle.php +++ b/core/modules/field/lib/Drupal/field/FieldBundle.php @@ -24,6 +24,12 @@ public function build(ContainerBuilder $container) { ->addArgument('%container.namespaces%'); $container->register('plugin.manager.field.formatter', 'Drupal\field\Plugin\Type\Formatter\FormatterPluginManager') ->addArgument('%container.namespaces%'); + $container + ->register("cache.field", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('field') + ->addTag('cache.bin'); } } diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index b827b4f..c3cd87b 100644 --- a/core/modules/filter/filter.module +++ b/core/modules/filter/filter.module @@ -30,13 +30,6 @@ const FILTER_TYPE_TRANSFORM_IRREVERSIBLE = 3; /** - * Implements hook_cache_flush(). - */ -function filter_cache_flush() { - return array('filter'); -} - -/** * Implements hook_help(). */ function filter_help($path, $arg) { diff --git a/core/modules/filter/lib/Drupal/filter/FilterBundle.php b/core/modules/filter/lib/Drupal/filter/FilterBundle.php new file mode 100644 index 0000000..1b57dfb --- /dev/null +++ b/core/modules/filter/lib/Drupal/filter/FilterBundle.php @@ -0,0 +1,30 @@ +register("cache.filter", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('filter') + ->addTag('cache.bin'); + } + +} diff --git a/core/modules/rdf/lib/Drupal/rdf/RdfBundle.php b/core/modules/rdf/lib/Drupal/rdf/RdfBundle.php index 40affcc..2d3aa76 100644 --- a/core/modules/rdf/lib/Drupal/rdf/RdfBundle.php +++ b/core/modules/rdf/lib/Drupal/rdf/RdfBundle.php @@ -20,15 +20,9 @@ class RdfBundle extends Bundle { * Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build(). */ public function build(ContainerBuilder $container) { - // Site schema type cache. - $container->register('cache.rdf.site_schema.types', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('cache'); - // Site schema manager service. $container->register('rdf.site_schema_manager', 'Drupal\rdf\SiteSchema\SiteSchemaManager') - ->addArgument(new Reference('cache.rdf.site_schema.types')); + ->addArgument(new Reference('cache.cache')); // Mapping manager service. $container->register('rdf.mapping_manager', 'Drupal\rdf\RdfMappingManager') ->addArgument(new Reference('event_dispatcher')) diff --git a/core/modules/simpletest/lib/Drupal/simpletest/InstallBundle.php b/core/modules/simpletest/lib/Drupal/simpletest/InstallBundle.php new file mode 100644 index 0000000..a02b8a1 --- /dev/null +++ b/core/modules/simpletest/lib/Drupal/simpletest/InstallBundle.php @@ -0,0 +1,26 @@ +register("cache.$bin", 'Drupal\Core\Cache\MemoryBackend') + ->addArgument($bin); + } + } + +} diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 44e29c2..16801e0 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -806,16 +806,15 @@ protected function setUp() { $GLOBALS['conf']['file_public_path'] = $this->public_files_directory; // Execute the non-interactive installer. require_once DRUPAL_ROOT . '/core/includes/install.core.inc'; + $GLOBALS['conf']['container_bundles']['simpletest_install_bundle'] = 'Drupal\simpletest\InstallBundle'; install_drupal($settings); $this->rebuildContainer(); + unset($GLOBALS['conf']['container_bundles']['simpletest_install_bundle']); // Restore the original Simpletest batch. $batch = &batch_get(); $batch = $this->originalBatch; - // Revert install_begin_request() cache service overrides. - unset($conf['cache_classes']); - // Set path variables. // Set 'parent_profile' of simpletest to add the parent profile's diff --git a/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php b/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php index 2c03b8b..f0e5cad 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php @@ -32,19 +32,24 @@ function setUp() { */ function testFlushAllCaches() { // Create cache entries for each flushed cache bin. - $bins = module_invoke_all('cache_flush'); - $this->assertTrue($bins, 'hook_cache_flush() returned bins to flush.'); - $bins = array_merge($bins, array('menu')); - foreach ($bins as $id => $bin) { - $cid = 'test_cid_clear' . $id; - cache($bin)->set($cid, $this->default_value); + $bins = cache_get_bins(); + foreach ($bins as $bin => $service) { + if (!$service instanceof \Drupal\Core\Cache\CacheBackendInterface) { + $this->fail(format_string('@bin is not a valid cache backend', array('@in' => $bin))); + } + } + $this->assertTrue($bins, 'cache_get_bins() returned bins to flush.'); + $bins['menu'] = $this->container->get('cache.menu'); + foreach ($bins as $bin => $cache_backend) { + $cid = 'test_cid_clear' . $bin; + $cache_backend->set($cid, $this->default_value); } // Remove all caches then make sure that they are cleared. drupal_flush_all_caches(); - foreach ($bins as $id => $bin) { - $cid = 'test_cid_clear' . $id; + foreach ($bins as $bin => $cache_backend) { + $cid = 'test_cid_clear' . $bin; $this->assertFalse($this->checkCacheExists($cid, $this->default_value, $bin), format_string('All cache entries removed from @bin.', array('@bin' => $bin))); } } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index a8e132e..520affc 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -1985,14 +1985,13 @@ function hook_mail($key, &$message, $params) { * This hook is invoked by drupal_flush_all_caches(). It runs before module data * is updated and before hook_rebuild(). * - * @return array - * An array of cache bins to be flushed. - * * @see drupal_flush_all_caches() * @see hook_rebuild() */ function hook_cache_flush() { - return array('example'); + if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') { + _update_cache_clear(); + } } /** diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 30317e4..66b9d56 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -5,6 +5,7 @@ * Configuration system that lets administrators modify the workings of the site. */ +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Database\Database; use Drupal\Core\Utility\ModuleInfo; use Drupal\Core\TypedData\Primitive; @@ -3521,10 +3522,10 @@ function system_cron() { // Cleanup the flood. drupal_container()->get('flood')->garbageCollection(); - $cache_bins = array_merge(module_invoke_all('cache_flush'), array('form', 'menu')); - foreach ($cache_bins as $bin) { - cache($bin)->deleteExpired(); - } + module_invoke_all('cache_flush'); + array_map(function (CacheBackendInterface $cache_backend) { + $cache_backend->deleteExpired(); + }, cache_get_bins()); // Cleanup the batch table and the queue for failed batches. db_delete('batch') @@ -3547,14 +3548,6 @@ function system_cron() { } /** - * Implements hook_cache_flush(). - */ -function system_cache_flush() { - // Do NOT flush the 'form' cache bin to retain in-progress form submissions. - return array('bootstrap', 'config', 'cache', 'page', 'path'); -} - -/** * Implements hook_mail(). */ function system_mail($key, &$message, $params) { diff --git a/core/modules/toolbar/lib/Drupal/toolbar/ToolbarBundle.php b/core/modules/toolbar/lib/Drupal/toolbar/ToolbarBundle.php new file mode 100644 index 0000000..7000093 --- /dev/null +++ b/core/modules/toolbar/lib/Drupal/toolbar/ToolbarBundle.php @@ -0,0 +1,30 @@ +register("cache.toolbar", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('toolbar') + ->addTag('cache.bin'); + } + +} diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index e4996bc..f0f1837 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -655,13 +655,6 @@ function toolbar_library_info() { } /** - * Implements hook_cache_flush(). - */ -function toolbar_cache_flush() { - return array('toolbar'); -} - -/** * Returns the hash of the per-user rendered toolbar subtrees. */ function _toolbar_get_subtree_hash() { diff --git a/core/modules/update/update.module b/core/modules/update/update.module index 13e1771..f003678 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -878,7 +878,6 @@ function update_cache_flush() { if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') { _update_cache_clear(); } - return array(); } /** diff --git a/core/modules/views/lib/Drupal/views/ViewsBundle.php b/core/modules/views/lib/Drupal/views/ViewsBundle.php index e4443f8..712999b 100644 --- a/core/modules/views/lib/Drupal/views/ViewsBundle.php +++ b/core/modules/views/lib/Drupal/views/ViewsBundle.php @@ -27,12 +27,6 @@ public function build(ContainerBuilder $container) { ->addArgument('%container.namespaces%'); } - $container - ->register('cache.views_info', 'Drupal\Core\Cache\CacheBackendInterface') - ->setFactoryClass('Drupal\Core\Cache\CacheFactory') - ->setFactoryMethod('get') - ->addArgument('views_info'); - $container->register('views.views_data', 'Drupal\views\ViewsDataCache') ->addArgument(new Reference('cache.views_info')) ->addArgument(new Reference('config.factory')) @@ -42,6 +36,18 @@ public function build(ContainerBuilder $container) { $container->register('views.analyzer', 'Drupal\views\Analyzer') ->addArgument(new Reference('module_handler')); + $container + ->register("cache.views_info", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('views_info') + ->addTag('cache.bin'); + $container + ->register("cache.views_results", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument('views_results') + ->addTag('cache.bin'); } } diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 31c47ff..8b02e76 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -649,13 +649,6 @@ function views_language_list($field = 'name', $flags = LANGUAGE_ALL) { } /** - * Implements hook_cache_flush(). - */ -function views_cache_flush() { - return array('views_info', 'views_results'); -} - -/** * Implements hook_field_create_instance. */ function views_field_create_instance($instance) {