From a5b32819a188fd2abb69424565d37d709cc21f08 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 13:31:56 +0300 Subject: [PATCH 01/13] UHF-10048: add all related annif keywords as cache tags for the block --- .../src/Plugin/Block/RecommendationsBlock.php | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php index 1ba6a66ac..55977cd7a 100644 --- a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php +++ b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php @@ -4,9 +4,11 @@ namespace Drupal\helfi_annif\Plugin\Block; +use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\Context\EntityContextDefinition; use Drupal\Core\Plugin\ContextAwarePluginInterface; @@ -94,7 +96,11 @@ public function getCacheContexts(): array { * {@inheritdoc} */ public function getCacheTags(): array { - return Cache::mergeTags(parent::getCacheTags(), $this->getContextValue('node')->getCacheTags()); + return Cache::mergeTags( + parent::getCacheTags(), + $this->getContextValue('node')->getCacheTags(), + $this->getAnnifKeywordCacheTags() + ); } /** @@ -115,4 +121,22 @@ private function handleNoRecommendations(array $response): array { return $response; } + /** + * Get list of cache tags for the block. + * + * @return array + * Array of cache tags for Annif-keywords related to this node. + */ + private function getAnnifKeywordCacheTags(): array { + $entity = $this->getContextValue('node'); + if (!$entity->hasField('field_annif_keywords') || $entity->get('field_annif_keywords')->isEmpty()) { + return []; + } + + return array_map( + fn ($item) => "taxonomy_term:{$item['target_id']}", + $entity->get('field_annif_keywords')->getValue() + ); + } + } From 62ade812aa61aeeb2db5d41467ca44a7eb645f50 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 13:56:52 +0300 Subject: [PATCH 02/13] UHF-10048: get cache tag from entity instead of doing it manually --- .../src/Plugin/Block/RecommendationsBlock.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php index 55977cd7a..28fcc5907 100644 --- a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php +++ b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php @@ -133,10 +133,13 @@ private function getAnnifKeywordCacheTags(): array { return []; } - return array_map( - fn ($item) => "taxonomy_term:{$item['target_id']}", - $entity->get('field_annif_keywords')->getValue() + $cacheTags = array_map( + fn ($term) => $term->getCacheTags(), + $entity->get('field_annif_keywords')->referencedEntities() ); + + // flatten array by merging the destructed arrays. + return array_merge(...$cacheTags); } } From f33027bf869ff94584ae9ced2badd719f2774752 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 14:41:04 +0300 Subject: [PATCH 03/13] UHF-10048: invalidate Annif-keyword terms' cache tags when processing the annif request --- .../custom/helfi_annif/src/KeywordManager.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/public/modules/custom/helfi_annif/src/KeywordManager.php b/public/modules/custom/helfi_annif/src/KeywordManager.php index 08d91e3f7..027e10e81 100644 --- a/public/modules/custom/helfi_annif/src/KeywordManager.php +++ b/public/modules/custom/helfi_annif/src/KeywordManager.php @@ -4,6 +4,7 @@ namespace Drupal\helfi_annif; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -52,6 +53,7 @@ public function __construct( private readonly EntityTypeManagerInterface $entityTypeManager, private readonly KeywordClient $keywordGenerator, private readonly QueueFactory $queueFactory, + private readonly CacheTagsInvalidatorInterface $cacheTagsInvalidator, ) { $this->termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); } @@ -268,6 +270,7 @@ private function saveKeywords(EntityInterface $entity, array $keywords) : void { // processedItems is set for update hooks. $this->processedItems[$this->getEntityKey($entity)] = TRUE; + $this->invalidateKeywordTermsCacheTags($terms); $entity->save(); } @@ -315,4 +318,19 @@ private function getTerm(Keyword $keyword, string $langcode) { return $term; } + /** + * Invalidate Annif-keyword terms' cache tags. + * + * @param array $terms + * Array of terms to process. + */ + private function invalidateKeywordTermsCacheTags(array $terms): void { + $cacheTags = array_map( + fn ($term) => $term->getCacheTags(), + $terms + ); + + $this->cacheTagsInvalidator->invalidateTags(array_merge(...$cacheTags)); + } + } From e261f147269070ef24c5f33b84f893887879bc34 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:06:05 +0300 Subject: [PATCH 04/13] UHF-10048: code fixes --- public/modules/custom/helfi_annif/src/KeywordManager.php | 2 ++ .../helfi_annif/src/Plugin/Block/RecommendationsBlock.php | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/KeywordManager.php b/public/modules/custom/helfi_annif/src/KeywordManager.php index 027e10e81..11c2ebb1f 100644 --- a/public/modules/custom/helfi_annif/src/KeywordManager.php +++ b/public/modules/custom/helfi_annif/src/KeywordManager.php @@ -45,6 +45,8 @@ final class KeywordManager { * The keyword generator. * @param \Drupal\Core\Queue\QueueFactory $queueFactory * The queue factory. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface + * The cache validator. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException diff --git a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php index 28fcc5907..ee1ec8660 100644 --- a/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php +++ b/public/modules/custom/helfi_annif/src/Plugin/Block/RecommendationsBlock.php @@ -4,11 +4,9 @@ namespace Drupal\helfi_annif\Plugin\Block; -use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Core\Block\Attribute\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\Cache; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\Context\EntityContextDefinition; use Drupal\Core\Plugin\ContextAwarePluginInterface; @@ -138,7 +136,7 @@ private function getAnnifKeywordCacheTags(): array { $entity->get('field_annif_keywords')->referencedEntities() ); - // flatten array by merging the destructed arrays. + // Flatten array by merging the destructed arrays. return array_merge(...$cacheTags); } From fc3e9ca7db55e92475a192711d28067506ca0ebe Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:09:26 +0300 Subject: [PATCH 05/13] UHF-10048: missing parameter name in comment --- public/modules/custom/helfi_annif/src/KeywordManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/src/KeywordManager.php b/public/modules/custom/helfi_annif/src/KeywordManager.php index 11c2ebb1f..e68d44314 100644 --- a/public/modules/custom/helfi_annif/src/KeywordManager.php +++ b/public/modules/custom/helfi_annif/src/KeywordManager.php @@ -45,7 +45,7 @@ final class KeywordManager { * The keyword generator. * @param \Drupal\Core\Queue\QueueFactory $queueFactory * The queue factory. - * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheTagsInvalidator * The cache validator. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException From cd966c84ff444addb371576e0bdf309a323ed21e Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:14:19 +0300 Subject: [PATCH 06/13] UHF-10048: update tests --- .../helfi_annif/tests/src/Kernel/KeywordManagerTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php index 17204fc88..c0f9c6f1f 100644 --- a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php @@ -133,10 +133,13 @@ private function getSut( ->get(Argument::any()) ->willReturn($queue); + $cacheInvalidator = $this->container->get('cache_tags.invalidator'); + return new KeywordManager( $entityTypeManager, $client, - $queueFactory->reveal() + $queueFactory->reveal(), + $cacheInvalidator ); } From b601fe590c68dee667a6260a1c5cf93accd1cb67 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:21:30 +0300 Subject: [PATCH 07/13] UHF-10048: remove whitespace --- .../custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php index c0f9c6f1f..88826d4a3 100644 --- a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php @@ -134,7 +134,7 @@ private function getSut( ->willReturn($queue); $cacheInvalidator = $this->container->get('cache_tags.invalidator'); - + return new KeywordManager( $entityTypeManager, $client, From 9338a5857d9fb54eb45cb92e160b3842580631c5 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:28:44 +0300 Subject: [PATCH 08/13] UHF-10048: another test fix --- .../custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php index 12c1b56b8..cc3a56ab8 100644 --- a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php @@ -128,6 +128,8 @@ private function getSut( ->get(Argument::any()) ->willReturn($queue); + $cacheInvalidator = $this->container->get('cache_tags.invalidator'); + return new KeywordManager( $entityTypeManager->reveal(), $client, From e38ea4047305ae707a583034722d1b448fd31134 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 15:31:33 +0300 Subject: [PATCH 09/13] UHF-10048: fix test --- .../custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php index cc3a56ab8..47830ad5d 100644 --- a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php @@ -133,7 +133,8 @@ private function getSut( return new KeywordManager( $entityTypeManager->reveal(), $client, - $queueFactory->reveal() + $queueFactory->reveal(), + $cacheInvalidator ); } From 97953ae218486c8e1e346b868713528946848dbb Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 16:14:13 +0300 Subject: [PATCH 10/13] UHF-10048: added cache validator to tests --- .../helfi_annif/tests/src/Kernel/KeywordManagerTest.php | 1 + .../helfi_annif/tests/src/Unit/KeywordManagerTest.php | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php index 88826d4a3..1d07e3438 100644 --- a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php @@ -35,6 +35,7 @@ class KeywordManagerTest extends KernelTestBase { 'taxonomy', 'language', 'helfi_annif', + 'cache_tags.invalidator', ]; /** diff --git a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php index 47830ad5d..a14941db1 100644 --- a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php @@ -4,6 +4,8 @@ namespace Drupal\Tests\helfi_annif\Unit\TextConverter; +use Drupal\Core\Cache\CacheTagsInvalidator; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Queue\QueueFactory; @@ -128,13 +130,15 @@ private function getSut( ->get(Argument::any()) ->willReturn($queue); - $cacheInvalidator = $this->container->get('cache_tags.invalidator'); + $cacheInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class); + $cacheInvalidator + ->invalidateTags(['taxonomy_term:1234']); return new KeywordManager( $entityTypeManager->reveal(), $client, $queueFactory->reveal(), - $cacheInvalidator + $cacheInvalidator->reveal(), ); } From 383b42b34c257af402ae63afc2f0da2384d17788 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 16:19:27 +0300 Subject: [PATCH 11/13] UHF-10048: unused use statement --- .../custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php index a14941db1..89973ac22 100644 --- a/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Unit/KeywordManagerTest.php @@ -4,7 +4,6 @@ namespace Drupal\Tests\helfi_annif\Unit\TextConverter; -use Drupal\Core\Cache\CacheTagsInvalidator; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; From c6a60d5b117449e87e18db38e4fbb0c16ef6e3f9 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Mon, 24 Jun 2024 16:33:36 +0300 Subject: [PATCH 12/13] UHF-10048: remove service from modules --- .../custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php index 1d07e3438..4dc41198b 100644 --- a/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php +++ b/public/modules/custom/helfi_annif/tests/src/Kernel/KeywordManagerTest.php @@ -25,6 +25,7 @@ class KeywordManagerTest extends KernelTestBase { use AnnifApiTestTrait; + /** * {@inheritdoc} */ @@ -35,7 +36,6 @@ class KeywordManagerTest extends KernelTestBase { 'taxonomy', 'language', 'helfi_annif', - 'cache_tags.invalidator', ]; /** From 5dbac6e1d3b04a4feb2f6ba1735d7e3dc03d47a2 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 25 Jun 2024 08:38:37 +0300 Subject: [PATCH 13/13] UHF-10048: better name for function --- public/modules/custom/helfi_annif/src/KeywordManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/modules/custom/helfi_annif/src/KeywordManager.php b/public/modules/custom/helfi_annif/src/KeywordManager.php index e68d44314..35bfd8129 100644 --- a/public/modules/custom/helfi_annif/src/KeywordManager.php +++ b/public/modules/custom/helfi_annif/src/KeywordManager.php @@ -272,7 +272,7 @@ private function saveKeywords(EntityInterface $entity, array $keywords) : void { // processedItems is set for update hooks. $this->processedItems[$this->getEntityKey($entity)] = TRUE; - $this->invalidateKeywordTermsCacheTags($terms); + $this->invalidateCacheTags($terms); $entity->save(); } @@ -326,7 +326,7 @@ private function getTerm(Keyword $keyword, string $langcode) { * @param array $terms * Array of terms to process. */ - private function invalidateKeywordTermsCacheTags(array $terms): void { + private function invalidateCacheTags(array $terms): void { $cacheTags = array_map( fn ($term) => $term->getCacheTags(), $terms