From a896aa6c59ac7bee253aa7c2406314dc968ea066 Mon Sep 17 00:00:00 2001
From: Ant Brown <ant@catalyst.net.nz>
Date: Fri, 1 Dec 2023 08:57:50 +1300
Subject: [PATCH 1/4] Cache terms returned by IslandoraUtils::getTermForUri()
 (Islandora#2272)

---
 islandora.services.yml |  2 +-
 src/IslandoraUtils.php | 55 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/islandora.services.yml b/islandora.services.yml
index 74725d80..0843c028 100644
--- a/islandora.services.yml
+++ b/islandora.services.yml
@@ -54,7 +54,7 @@ services:
     arguments: ['@entity_type.manager', '@current_user', '@language_manager', '@file_system', '@islandora.utils']
   islandora.utils:
     class: Drupal\islandora\IslandoraUtils
-    arguments: ['@entity_type.manager', '@entity_field.manager', '@context.manager', '@flysystem_factory', '@language_manager']
+    arguments: ['@entity_type.manager', '@entity_field.manager', '@context.manager', '@flysystem_factory', '@language_manager', '@cache.data', '@current_user']
   islandora.entity_mapper:
     class: Islandora\EntityMapper\EntityMapper
   islandora.stomp.auth_header_listener:
diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php
index a2df7589..31d427e7 100644
--- a/src/IslandoraUtils.php
+++ b/src/IslandoraUtils.php
@@ -3,6 +3,8 @@
 namespace Drupal\islandora;
 
 use Drupal\context\ContextManager;
+use Drupal\Component\Utility\Html;
+use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
 use Drupal\Core\Entity\EntityInterface;
@@ -10,6 +12,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Entity\Query\QueryException;
 use Drupal\Core\Entity\Query\QueryInterface;
 use Drupal\Core\Language\LanguageManagerInterface;
+use Drupal\Core\Session\AccountProxyInterface;
 use Drupal\Core\Site\Settings;
 use Drupal\Core\Url;
 use Drupal\file\FileInterface;
@@ -70,6 +73,19 @@ class IslandoraUtils {
    */
   protected $languageManager;
 
+  /**
+   * Cache backend.
+   *
+   * @var \Drupal\Core\Cache\CacheBackendInterface
+   */
+  protected $cache;
+
+  /** Current user.
+   *
+   * @var \Drupal\Core\Session\AccountProxyInterface
+   */
+  protected $currentUser;
+
   /**
    * Constructor.
    *
@@ -83,19 +99,27 @@ class IslandoraUtils {
    *   Flysystem factory.
    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
    *   Language manager.
+   * @param \Drupal\Core\Cache\CacheBackendInterface
+   *   Cache backend.
+   * @param \Drupal\Core\Session\AccountProxyInterface
+   *   Current user account.
    */
   public function __construct(
     EntityTypeManagerInterface $entity_type_manager,
     EntityFieldManagerInterface $entity_field_manager,
     ContextManager $context_manager,
     FlysystemFactory $flysystem_factory,
-    LanguageManagerInterface $language_manager
+    LanguageManagerInterface $language_manager,
+    CacheBackendInterface $cache,
+    AccountProxyInterface $currentUser
   ) {
     $this->entityTypeManager = $entity_type_manager;
     $this->entityFieldManager = $entity_field_manager;
     $this->contextManager = $context_manager;
     $this->flysystemFactory = $flysystem_factory;
     $this->languageManager = $language_manager;
+    $this->cache = $cache;
+    $this->currentUser = $currentUser;
   }
 
   /**
@@ -235,6 +259,19 @@ class IslandoraUtils {
    *   Calling getStorage() throws if the storage handler couldn't be loaded.
    */
   public function getTermForUri($uri) {
+
+    $cid_parts = [
+      'islandora',
+      'term-for-uri',
+      'user-' . $this->currentUser->id(),
+      'uri-' . Html::getClass($uri),
+    ];
+    $cid = implode(':', $cid_parts);
+
+    if ($cache = $this->cache->get($cid)) {
+      return $cache->data;
+    }
+
     // Get authority link fields to search.
     $field_map = $this->entityFieldManager->getFieldMap();
     $fields = [];
@@ -258,12 +295,20 @@ class IslandoraUtils {
       ->condition($orGroup)
       ->execute();
 
-    if (empty($results)) {
-      return NULL;
+    $term = NULL;
+    if (!empty($results)) {
+      $term = $this->entityTypeManager->getStorage('taxonomy_term')
+        ->load(reset($results));
     }
 
-    return $this->entityTypeManager->getStorage('taxonomy_term')
-      ->load(reset($results));
+    $this->cache->set(
+      $cid,
+      $term,
+      CacheBackendInterface::CACHE_PERMANENT,
+      $term->getCacheTags()
+    );
+
+    return $term;
   }
 
   /**

From 380207dcc91452bc4fd5e92f43f14c3cedda4432 Mon Sep 17 00:00:00 2001
From: Ant Brown <ant@catalyst.net.nz>
Date: Fri, 1 Dec 2023 09:10:13 +1300
Subject: [PATCH 2/4] Fix up code style issues (Islandora#2272)

---
 src/IslandoraUtils.php | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php
index 31d427e7..f5847a7a 100644
--- a/src/IslandoraUtils.php
+++ b/src/IslandoraUtils.php
@@ -80,7 +80,8 @@ class IslandoraUtils {
    */
   protected $cache;
 
-  /** Current user.
+  /**
+   * Current user.
    *
    * @var \Drupal\Core\Session\AccountProxyInterface
    */
@@ -99,9 +100,9 @@ class IslandoraUtils {
    *   Flysystem factory.
    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
    *   Language manager.
-   * @param \Drupal\Core\Cache\CacheBackendInterface
+   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
    *   Cache backend.
-   * @param \Drupal\Core\Session\AccountProxyInterface
+   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
    *   Current user account.
    */
   public function __construct(
@@ -111,7 +112,7 @@ class IslandoraUtils {
     FlysystemFactory $flysystem_factory,
     LanguageManagerInterface $language_manager,
     CacheBackendInterface $cache,
-    AccountProxyInterface $currentUser
+    AccountProxyInterface $current_user
   ) {
     $this->entityTypeManager = $entity_type_manager;
     $this->entityFieldManager = $entity_field_manager;
@@ -119,7 +120,7 @@ class IslandoraUtils {
     $this->flysystemFactory = $flysystem_factory;
     $this->languageManager = $language_manager;
     $this->cache = $cache;
-    $this->currentUser = $currentUser;
+    $this->currentUser = $current_user;
   }
 
   /**

From bd377ee7d4f1b404f3014a5bfec361157f8df011 Mon Sep 17 00:00:00 2001
From: Ant Brown <ant@catalyst.net.nz>
Date: Mon, 4 Dec 2023 10:36:58 +1300
Subject: [PATCH 3/4] Add cache tag for current user (Islandora#2272)

---
 src/IslandoraUtils.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php
index f5847a7a..fb61d8b3 100644
--- a/src/IslandoraUtils.php
+++ b/src/IslandoraUtils.php
@@ -4,6 +4,7 @@ namespace Drupal\islandora;
 
 use Drupal\context\ContextManager;
 use Drupal\Component\Utility\Html;
+use Drupal\Core\Cache\Cache;
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Entity\ContentEntityInterface;
 use Drupal\Core\Entity\EntityFieldManagerInterface;
@@ -306,7 +307,7 @@ class IslandoraUtils {
       $cid,
       $term,
       CacheBackendInterface::CACHE_PERMANENT,
-      $term->getCacheTags()
+      Cache::mergeTags(array_merge($term->getCacheTags(), ['user:' . $this->currentUser->id()]))
     );
 
     return $term;

From 14a6715696144baf06d87029390d7c328257afb7 Mon Sep 17 00:00:00 2001
From: Ant Brown <ant@catalyst.net.nz>
Date: Wed, 10 Apr 2024 16:10:25 +1200
Subject: [PATCH 4/4] Only cache terms that exist (Islandora#2272)

---
 src/IslandoraUtils.php | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php
index fb61d8b3..a026ac18 100644
--- a/src/IslandoraUtils.php
+++ b/src/IslandoraUtils.php
@@ -301,15 +301,14 @@ class IslandoraUtils {
     if (!empty($results)) {
       $term = $this->entityTypeManager->getStorage('taxonomy_term')
         ->load(reset($results));
+      $this->cache->set(
+        $cid,
+        $term,
+        CacheBackendInterface::CACHE_PERMANENT,
+        Cache::mergeTags(array_merge($term->getCacheTags(), ['user:' . $this->currentUser->id()]))
+      );
     }
 
-    $this->cache->set(
-      $cid,
-      $term,
-      CacheBackendInterface::CACHE_PERMANENT,
-      Cache::mergeTags(array_merge($term->getCacheTags(), ['user:' . $this->currentUser->id()]))
-    );
-
     return $term;
   }