Browse Source

Cache terms returned by IslandoraUtils::getTermForUri() (Islandora#2272)

pull/997/head
Ant Brown 1 year ago
parent
commit
a896aa6c59
  1. 2
      islandora.services.yml
  2. 55
      src/IslandoraUtils.php

2
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:

55
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;
}
/**

Loading…
Cancel
Save