Skip to content

Commit

Permalink
Merge pull request #654 from City-of-Helsinki/UHF-10048
Browse files Browse the repository at this point in the history
UHF-10048 annif-keyword cache tag invalidation
  • Loading branch information
rpnykanen authored Jun 26, 2024
2 parents 16733dd + 5dbac6e commit 5f24992
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
20 changes: 20 additions & 0 deletions public/modules/custom/helfi_annif/src/KeywordManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,8 @@ final class KeywordManager {
* The keyword generator.
* @param \Drupal\Core\Queue\QueueFactory $queueFactory
* The queue factory.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheTagsInvalidator
* The cache validator.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
Expand All @@ -52,6 +55,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');
}
Expand Down Expand Up @@ -268,6 +272,7 @@ private function saveKeywords(EntityInterface $entity, array $keywords) : void {
// processedItems is set for update hooks.
$this->processedItems[$this->getEntityKey($entity)] = TRUE;

$this->invalidateCacheTags($terms);
$entity->save();
}

Expand Down Expand Up @@ -315,4 +320,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 invalidateCacheTags(array $terms): void {
$cacheTags = array_map(
fn ($term) => $term->getCacheTags(),
$terms
);

$this->cacheTagsInvalidator->invalidateTags(array_merge(...$cacheTags));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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()
);
}

/**
Expand All @@ -115,4 +119,25 @@ 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 [];
}

$cacheTags = array_map(
fn ($term) => $term->getCacheTags(),
$entity->get('field_annif_keywords')->referencedEntities()
);

// Flatten array by merging the destructed arrays.
return array_merge(...$cacheTags);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class KeywordManagerTest extends KernelTestBase {

use AnnifApiTestTrait;


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -133,10 +134,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
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\helfi_annif\Unit\TextConverter;

use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Queue\QueueFactory;
Expand Down Expand Up @@ -128,10 +129,15 @@ private function getSut(
->get(Argument::any())
->willReturn($queue);

$cacheInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class);
$cacheInvalidator
->invalidateTags(['taxonomy_term:1234']);

return new KeywordManager(
$entityTypeManager->reveal(),
$client,
$queueFactory->reveal()
$queueFactory->reveal(),
$cacheInvalidator->reveal(),
);
}

Expand Down

0 comments on commit 5f24992

Please sign in to comment.