diff --git a/core/includes/cache.inc b/core/includes/cache.inc index 74afcbf..c792520 100644 --- a/core/includes/cache.inc +++ b/core/includes/cache.inc @@ -5,7 +5,9 @@ * Functions and interfaces for cache handling. */ +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheFactory; +use Drupal\Core\Cache\Cache; /** * Instantiates and statically caches the correct class for a cache bin. @@ -26,40 +28,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]; -} - -/** - * 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 array $tags - * 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); - } + return drupal_container()->get("cache.$bin"); } /** @@ -74,9 +46,8 @@ function cache_delete_tags(array $tags) { * * @param array $tags * The list of tags to invalidate cache items for. + * @deprecated */ function cache_invalidate_tags(array $tags) { - foreach (CacheFactory::getBackends() as $bin => $class) { - cache($bin)->invalidateTags($tags); - } + Cache::invalidateTags($tags); } diff --git a/core/includes/common.inc b/core/includes/common.inc index 88d6fd3..b1bbba3 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1,5 +1,6 @@ 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(); + }, CacheHelper::getBins()); + */ + foreach (Cache::getBins() 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..bcceac9 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 @@ -1622,13 +1626,7 @@ function install_load_profile(&$install_state) { * @param $install_state * An array of information about the current installation state. */ -function install_bootstrap_full(&$install_state) { - // The early stages of the installer override the cache backend since Drupal - // isn't fully set up yet. Here the override is removed so that the standard - // cache backend will be used again. - unset($GLOBALS['conf']['cache_classes']['cache']); - drupal_static_reset('cache'); - +function install_bootstrap_full() { drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); } diff --git a/core/includes/update.inc b/core/includes/update.inc index bf11562..b29d94d 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -9,6 +9,7 @@ */ use Drupal\Component\Graph\Graph; +use Drupal\Component\Utility\Settings; use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\ConfigException; use Drupal\Core\DrupalKernel; @@ -97,7 +98,9 @@ function update_prepare_d8_bootstrap() { // During the bootstrap to DRUPAL_BOOTSTRAP_PAGE_CACHE, code will try to read // the cache but the cache tables are not compatible yet. Use the Null backend // by default to avoid exceptions. - $GLOBALS['conf']['cache_classes'] = array('cache' => 'Drupal\Core\Cache\NullBackend'); + $settings = settings()->getAll(); + $settings['cache']['default'] = 'cache.backend.memory'; + new Settings($settings); // Enable UpdateBundle service overrides. $GLOBALS['conf']['container_bundles'][] = 'Drupal\Core\DependencyInjection\UpdateBundle'; @@ -434,8 +437,11 @@ function update_prepare_d8_bootstrap() { } } // Now remove the cache override. - unset($GLOBALS['conf']['cache_classes']['cache']); - drupal_static_reset('cache'); + $settings = settings()->getAll(); + unset($settings['cache']['default']); + new Settings($settings); + $kernel = new DrupalKernel('update', FALSE, drupal_classloader(), FALSE); + $kernel->boot(); } /** diff --git a/core/lib/Drupal/Core/Cache/Cache.php b/core/lib/Drupal/Core/Cache/Cache.php new file mode 100644 index 0000000..2db56d8 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/Cache.php @@ -0,0 +1,84 @@ +register("cache.$bin", 'Drupal\Core\Cache\CacheBackendInterface') + ->setFactoryService('cache_factory') + ->setFactoryMethod('get') + ->addArgument($bin) + ->addTag('cache.bin'); + } + + /** + * 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 array $tags + * The list of tags to delete cache items for. + */ + static function deleteTags(array $tags) { + array_map(function (CacheBackendInterface $cache_backend) use ($tags) { + $cache_backend->deleteTags($tags); + }, static::getBins()); + } + + /** + * 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 array $tags + * The list of tags to invalidate cache items for. + */ + static function invalidateTags(array $tags) { + array_map(function (CacheBackendInterface $cache_backend) use ($tags) { + $cache_backend->invalidateTags($tags); + }, static::getBins()); + } + + /** + * Gets all cache bin services. + * + * @return array + * An array of cache backend objects keyed by cache bins. + */ + static function getBins() { + $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/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..1ed02c2 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Cache; +use Drupal\Core\Database\Connection; use Drupal\Core\Database\Database; use Drupal\Core\Database\DatabaseException; @@ -23,19 +24,30 @@ class DatabaseBackend implements CacheBackendInterface { */ protected $bin; + + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a DatabaseBackend object. * + * @param \Drupal\Core\Database\Connection $connection + * The database connection. * @param string $bin * The cache bin for which the object is created. */ - public function __construct($bin) { + public function __construct(Connection $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 +70,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 +154,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 +164,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 +175,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 +189,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 +201,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 +227,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 +242,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 +254,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 +315,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 +335,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/ListCacheBinsPass.php b/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php new file mode 100644 index 0000000..04c422c --- /dev/null +++ b/core/lib/Drupal/Core/Cache/ListCacheBinsPass.php @@ -0,0 +1,30 @@ +findTaggedServiceIds('cache.bin') as $id => $attributes) { + $cache_bins[$id] = substr($id, strpos($id, '.') + 1); + } + $container->setParameter('cache_bins', $cache_bins); + } +} diff --git a/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php new file mode 100644 index 0000000..7059529 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/MemoryBackendFactory.php @@ -0,0 +1,16 @@ +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 + ->register('cache.backend.memory', 'Drupal\Core\Cache\MemoryBackendFactory'); + $container->addCompilerPass(new ListCacheBinsPass()); + // 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 +167,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 +210,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..ab6b17f 100644 --- a/core/modules/block/lib/Drupal/block/BlockBundle.php +++ b/core/modules/block/lib/Drupal/block/BlockBundle.php @@ -7,6 +7,7 @@ namespace Drupal\Block; +use Drupal\Core\Cache\Cache; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -22,6 +23,7 @@ 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%'); + Cache::registerBin($container, 'block'); } } 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..8577fc1 100644 --- a/core/modules/field/lib/Drupal/field/FieldBundle.php +++ b/core/modules/field/lib/Drupal/field/FieldBundle.php @@ -7,6 +7,7 @@ namespace Drupal\field; +use Drupal\Core\Cache\Cache; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -24,6 +25,7 @@ public function build(ContainerBuilder $container) { ->addArgument('%container.namespaces%'); $container->register('plugin.manager.field.formatter', 'Drupal\field\Plugin\Type\Formatter\FormatterPluginManager') ->addArgument('%container.namespaces%'); + Cache::registerBin($container, 'field'); } } diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index 725d931..5de251c 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..05fcb8f --- /dev/null +++ b/core/modules/filter/lib/Drupal/filter/FilterBundle.php @@ -0,0 +1,26 @@ +siteSchemaManager = new SiteSchemaManager(new DatabaseBackend('cache')); + $this->siteSchemaManager = new SiteSchemaManager($container->get('cache.cache')); // Construct RDF mapping manager. $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new MappingSubscriber()); diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php index a9e057d..0371ed9 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php @@ -51,7 +51,7 @@ public static function getInfo() { function setUp() { parent::setUp(); - $setup_helper = new JsonldTestSetupHelper(); + $setup_helper = new JsonldTestSetupHelper($this->container); $this->normalizers = $setup_helper->getNormalizers(); // Add German as a language. diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php index eacf9df..e5349f1 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php @@ -38,7 +38,7 @@ function testSchemaSerialization() { $schema = new SiteSchema(SiteSchema::CONTENT_DEPLOYMENT); $bundle_schema = $schema->bundle($entity_type, $bundle); // Set up the serializer. - $setup_helper = new JsonldTestSetupHelper(); + $setup_helper = new JsonldTestSetupHelper($this->container); $normalizer = new JsonldRdfSchemaNormalizer($setup_helper->getSiteSchemaManager(), $setup_helper->getRdfMappingManager()); $serializer = new Serializer(array($normalizer), array(new JsonldEncoder())); diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php index 38cee8a..d9d4a7f 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php @@ -46,7 +46,7 @@ public static function getInfo() { function setUp() { parent::setUp(); - $setup_helper = new JsonldTestSetupHelper(); + $setup_helper = new JsonldTestSetupHelper($this->container); $this->normalizers = $setup_helper->getNormalizers(); } 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/rdf/lib/Drupal/rdf/Tests/RdfMappingEventTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingEventTest.php index 7a7a7b5..c099a9a 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingEventTest.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfMappingEventTest.php @@ -6,7 +6,6 @@ namespace Drupal\rdf\Tests; -use Drupal\Core\Cache\DatabaseBackend; use Drupal\rdf\RdfMappingManager; use Drupal\rdf\EventSubscriber\MappingSubscriber; use Drupal\rdf_test_mapping\EventSubscriber\TestMappingSubscriber; @@ -45,7 +44,7 @@ public function testMapInputType() { $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new MappingSubscriber()); $dispatcher->addSubscriber(new TestMappingSubscriber()); - $site_schema_manager = new SiteSchemaManager(new DatabaseBackend('cache')); + $site_schema_manager = new SiteSchemaManager($this->container->get('cache.cache')); $mapping_manager = new RdfMappingManager($dispatcher, $site_schema_manager); // Test that a site schema URI is mapped to itself. This is the default diff --git a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php index bc472c4..e5f33d4 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/DrupalUnitTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\simpletest; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DrupalKernel; use Drupal\Core\KeyValueStore\KeyValueMemoryFactory; use Symfony\Component\DependencyInjection\Reference; @@ -139,14 +140,13 @@ protected function tearDown() { * @see DrupalUnitTestBase::enableModules() * @see DrupalUnitTestBase::disableModules() */ - public function containerBuild($container) { + public function containerBuild(ContainerBuilder $container) { global $conf; // Keep the container object around for tests. $this->container = $container; $container->register('lock', 'Drupal\Core\Lock\NullLockBackend'); - - $conf['cache_classes'] = array('cache' => 'Drupal\Core\Cache\MemoryBackend'); + $this->settingsSet('cache', array('default' => 'cache.backend.memory')); $container ->register('config.storage', 'Drupal\Core\Config\FileStorage') diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 44e29c2..4f2418b 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'; + $this->settingsSet('cache', array('default' => 'cache.backend.memory')); install_drupal($settings); + $this->settingsSet('cache', array()); $this->rebuildContainer(); // 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..492cfeb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Cache/ClearTest.php @@ -10,6 +10,8 @@ /** * Tests cache clearing methods. */ +use Drupal\Core\Cache\Cache; + class ClearTest extends CacheTestBase { public static function getInfo() { @@ -32,19 +34,26 @@ 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::getBins(); + // @todo remove after http://drupal.org/node/512026 + unset($bins['form']); + 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/lib/Drupal/system/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Cache/DatabaseBackendUnitTest.php index 525de36..aa4a414 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Cache/DatabaseBackendUnitTest.php @@ -8,6 +8,7 @@ namespace Drupal\system\Tests\Cache; use Drupal\Core\Cache\DatabaseBackend; +use Drupal\Core\Database\Database; /** * Tests DatabaseBackend using GenericCacheBackendUnitTestBase. @@ -29,7 +30,7 @@ public static function getInfo() { * A new DatabaseBackend object. */ protected function createCacheBackend($bin) { - return new DatabaseBackend($bin); + return new DatabaseBackend(Database::getConnection(), $bin); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php b/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php index 9e869ee..0b08def 100644 --- a/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/DrupalKernel/DrupalKernelTest.php @@ -36,7 +36,7 @@ function setUp() { 'secret' => $GLOBALS['drupal_hash_salt'], ); // Use a non-persistent cache to avoid queries to non-existing tables. - $conf['cache_classes'] = array('cache' => 'Drupal\Core\Cache\MemoryBackend'); + $this->settingsSet('cache', array('default' => 'cache.backend.memory')); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php index 327e46e..5ada90c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Plugin; +use Drupal\Core\Flood\MemoryBackend; use Drupal\system\Tests\Plugin\Discovery\DiscoveryTestBase; use Drupal\Component\Plugin\Discovery\StaticDiscovery; use Drupal\Core\Plugin\Discovery\CacheDecorator; @@ -45,7 +46,8 @@ public function setUp() { // Use a non-db cache backend, so that we can use DiscoveryTestBase (which // extends UnitTestBase). - $conf['cache_classes'][$this->cacheBin] = 'Drupal\Core\Cache\MemoryBackend'; + // @todo switch to injecting the MemoryBackend http://drupal.org/node/1903346 + drupal_container()->set("cache.$this->cacheBin", new MemoryBackend()); // Create discovery objects to test. $this->emptyDiscovery = new StaticDiscovery(); diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index abac578..2b337e0 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 677e5ca..a432e6e 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -5,6 +5,8 @@ * Configuration system that lets administrators modify the workings of the site. */ +use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Cache\Cache; use Drupal\Core\Database\Database; use Drupal\Core\Utility\ModuleInfo; use Drupal\Core\TypedData\Primitive; @@ -3521,10 +3523,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::getBins()); // Cleanup the batch table and the queue for failed batches. db_delete('batch') @@ -3547,14 +3549,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..60d8297 --- /dev/null +++ b/core/modules/toolbar/lib/Drupal/toolbar/ToolbarBundle.php @@ -0,0 +1,26 @@ +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 +37,8 @@ public function build(ContainerBuilder $container) { $container->register('views.analyzer', 'Drupal\views\Analyzer') ->addArgument(new Reference('module_handler')); + Cache::registerBin($container, 'views_info'); + Cache::registerBin($container, 'views_results'); } } diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 31c47ff..279d827 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -9,6 +9,7 @@ * incoming page and block requests. */ +use Drupal\Core\Cache\Cache; use Drupal\Core\Database\Query\AlterableInterface; use Drupal\views\ViewExecutable; use Drupal\Component\Plugin\Exception\PluginException; @@ -649,13 +650,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) { @@ -687,7 +681,7 @@ function views_invalidate_cache() { cache('views_info')->deleteAll(); // Clear the page and block cache. - cache_delete_tags(array('content' => TRUE)); + Cache::deleteTags(array('content' => TRUE)); // Set the menu as needed to be rebuilt. state()->set('menu_rebuild_needed', TRUE); diff --git a/core/update.php b/core/update.php index d24d6e0..bc84f20 100644 --- a/core/update.php +++ b/core/update.php @@ -276,8 +276,8 @@ function update_info_page() { // Change query-strings on css/js files to enforce reload for all users. _drupal_flush_css_js(); // Flush the cache of all data for the update status module. - if (db_table_exists('cache_update')) { - cache('update')->deleteAll(); + if (module_exists('update')) { + _update_cache_clear(); } update_task_list('info');