diff --git a/src/XmlSitemapGenerator.php b/src/XmlSitemapGenerator.php index 46fe948..398700a 100644 --- a/src/XmlSitemapGenerator.php +++ b/src/XmlSitemapGenerator.php @@ -18,6 +18,7 @@ use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; use Psr\Log\LoggerInterface; +use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; /** * XmlSitemap generator service class. @@ -120,6 +121,13 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface { */ protected $time; + /** + * The entity memory cache service. + * + * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface + */ + protected $memoryCache; + /** * Constructs a XmlSitemapGenerator object. * @@ -143,8 +151,10 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface { * The file system. * @param \Drupal\Component\Datetime\TimeInterface $time * The time service. + * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache + * The memory cache service. */ - public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, LanguageManagerInterface $language_manager, LoggerInterface $logger, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, Connection $connection, MessengerInterface $messenger, FileSystemInterface $file_system, TimeInterface $time) { + public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, LanguageManagerInterface $language_manager, LoggerInterface $logger, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, Connection $connection, MessengerInterface $messenger, FileSystemInterface $file_system, TimeInterface $time, MemoryCacheInterface $memory_cache) { $this->config = $config_factory->getEditable('xmlsitemap.settings'); $this->state = $state; $this->languageManager = $language_manager; @@ -155,6 +165,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface { $this->messenger = $messenger; $this->fileSystem = $file_system; $this->time = $time; + $this->memoryCache = $memory_cache; } /** @@ -574,6 +585,10 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface { else { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } + + // Flush the entity memory cache to avoid exhausting memory for every + // entity loaded during the sitemap generation. + $this->memoryCache->deleteAll(); } /** diff --git a/xmlsitemap.services.yml b/xmlsitemap.services.yml index d9e9b9e..56daeab 100644 --- a/xmlsitemap.services.yml +++ b/xmlsitemap.services.yml @@ -1,7 +1,7 @@ services: xmlsitemap_generator: class: Drupal\xmlsitemap\XmlSitemapGenerator - arguments: ['@config.factory', '@state', '@language_manager', '@logger.channel.xmlsitemap', '@module_handler', '@entity_type.manager', '@database', '@messenger', '@file_system', '@datetime.time'] + arguments: ['@config.factory', '@state', '@language_manager', '@logger.channel.xmlsitemap', '@module_handler', '@entity_type.manager', '@database', '@messenger', '@file_system', '@datetime.time', '@entity.memory_cache'] tags: - { name: xmlsitemap_generator } xmlsitemap.link_storage: