From 3690e77e954f65ff84779d2925a62bf7775e1d3d Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 20 Mar 2024 08:59:15 +0200 Subject: [PATCH] UHF-9824: Removed unused CacheWarmer service --- helfi_navigation.services.yml | 7 --- src/CacheWarmer.php | 90 --------------------------- tests/src/Kernel/CacheWarmerTest.php | 91 ---------------------------- 3 files changed, 188 deletions(-) delete mode 100644 src/CacheWarmer.php delete mode 100644 tests/src/Kernel/CacheWarmerTest.php diff --git a/helfi_navigation.services.yml b/helfi_navigation.services.yml index cf2603a..fc6c715 100644 --- a/helfi_navigation.services.yml +++ b/helfi_navigation.services.yml @@ -37,13 +37,6 @@ services: - '@menu.link_tree' - '@plugin.manager.menu.link' - '@event_dispatcher' - helfi_navigation.cache_warmer: - class: Drupal\helfi_navigation\CacheWarmer - arguments: - - '@tempstore.shared' - - '@language_manager' - - '@cache_tags.invalidator' - - '@helfi_navigation.api_manager' helfi_navigation.api_authorization: class: Drupal\helfi_navigation\ApiAuthorization arguments: diff --git a/src/CacheWarmer.php b/src/CacheWarmer.php deleted file mode 100644 index d133fe1..0000000 --- a/src/CacheWarmer.php +++ /dev/null @@ -1,90 +0,0 @@ -tempStoreFactory->get(self::STORAGE_KEY); - - $currentHash = $storage->get($key); - $hash = hash('sha256', serialize($data)); - - // Only invalidate tags if content has actually changed. - if ($currentHash === $hash) { - return; - } - $storage->set($key, $hash); - // Invalidate menu block instances. - $this->cacheTagsInvalidator->invalidateTags(['config:system.menu.' . $menuName]); - } - - /** - * Warm caches for all available external menus. - */ - public function warm() : void { - $plugin = new ExternalMenuBlock(); - $derives = array_keys($plugin->getDerivativeDefinitions([])); - $derives[] = 'main'; - - foreach ($this->languageManager->getLanguages() as $language) { - foreach ($derives as $name) { - try { - $response = $this->apiManager - ->withBypassCache() - ->get($language->getId(), $name); - $this->invalidateTags($response, $language->getId(), $name); - } - catch (\Exception) { - } - } - } - } - -} diff --git a/tests/src/Kernel/CacheWarmerTest.php b/tests/src/Kernel/CacheWarmerTest.php deleted file mode 100644 index 90ebb2a..0000000 --- a/tests/src/Kernel/CacheWarmerTest.php +++ /dev/null @@ -1,91 +0,0 @@ -populateConfiguration('Test'); - $this->setActiveProject(Project::ASUMINEN, EnvironmentEnum::Local); - $this->sharedTempStore = $this->container->get('tempstore.shared'); - } - - /** - * Constructs a new cache warmer instance. - * - * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $invalidator - * The cache invalidator service. - * - * @return \Drupal\helfi_navigation\CacheWarmer - * The cache warmer service. - */ - private function getCacheWarmer(CacheTagsInvalidatorInterface $invalidator) : CacheWarmer { - return new CacheWarmer( - $this->sharedTempStore, - $this->container->get('language_manager'), - $invalidator, - $this->container->get('helfi_navigation.api_manager'), - ); - } - - /** - * Tests cache warmer. - */ - public function testCacheWarmer() : void { - $invalidator = $this->prophesize(CacheTagsInvalidatorInterface::class); - $invalidator->invalidateTags(Argument::cetera()) - ->shouldBeCalled(); - - // Warm caches and make sure all tags are invalidated when nothing is - // populated yet. - $this->getCacheWarmer($invalidator->reveal())->warm(); - - // Warming caches again should not invalidate anything since hash - // hasn't changed. - $invalidator = $this->prophesize(CacheTagsInvalidatorInterface::class); - $invalidator->invalidateTags(Argument::cetera()) - ->shouldNotBeCalled(); - $this->getCacheWarmer($invalidator->reveal())->warm(); - - // Reset hash for finnish main menu and make sure main menu block is - // invalidated (once). - $this->sharedTempStore->get(CacheWarmer::STORAGE_KEY)->set('fi:main', ''); - $invalidator = $this->prophesize(CacheTagsInvalidatorInterface::class); - $invalidator->invalidateTags(['config:system.menu.main']) - ->shouldBeCalledTimes(1); - $this->getCacheWarmer($invalidator->reveal())->warm(); - } - -}