diff --git a/src/EventSubscriber/EmbargoJoinProcessorEventSubscriber.php b/src/EventSubscriber/EmbargoJoinProcessorEventSubscriber.php index da5d9ad..d97bd25 100644 --- a/src/EventSubscriber/EmbargoJoinProcessorEventSubscriber.php +++ b/src/EventSubscriber/EmbargoJoinProcessorEventSubscriber.php @@ -71,16 +71,22 @@ public function preQuery(PreQueryEvent $event) : void { $backend = $search_api_query->getIndex()->getServerInstance()->getBackend(); assert($backend instanceof SolrBackendInterface); $map = $backend->getSolrFieldNames($search_api_query->getIndex()); - $get_field_name = function (?string $datasource_id, string $property_path) use ($search_api_query, $map) { - $fields = $this->fieldsHelper->filterForPropertyPath( - $search_api_query->getIndex()->getFieldsByDatasource($datasource_id), - $datasource_id, - $property_path, - ); - /** @var \Drupal\search_api\Item\FieldInterface $field */ - $field = reset($fields); + $memoized_map = []; + $get_field_name = function (?string $datasource_id, string $property_path) use ($search_api_query, $map, &$memoized_map) { + $key = "{$datasource_id}__{$property_path}"; + if (!isset($memoized_map[$key])) { + $fields = $this->fieldsHelper->filterForPropertyPath( + $search_api_query->getIndex()->getFieldsByDatasource($datasource_id), + $datasource_id, + $property_path, + ); + /** @var \Drupal\search_api\Item\FieldInterface $field */ + $field = reset($fields); - return $map[$field->getFieldIdentifier()]; + $memoized_map[$key] = $map[$field->getFieldIdentifier()]; + } + + return $memoized_map[$key]; }; $solarium_query = $event->getSolariumQuery(); diff --git a/src/Plugin/search_api/processor/EmbargoJoinProcessor.php b/src/Plugin/search_api/processor/EmbargoJoinProcessor.php index bd95aac..b9d1bf6 100644 --- a/src/Plugin/search_api/processor/EmbargoJoinProcessor.php +++ b/src/Plugin/search_api/processor/EmbargoJoinProcessor.php @@ -205,8 +205,8 @@ protected function doAddNodeField(ItemInterface $item, EntityInterface $entity) protected function doAddEmbargoField(ItemInterface $item, EntityInterface $entity) : void { assert($entity instanceof EmbargoInterface); $paths = match ($entity->getEmbargoType()) { - EmbargoInterface::EMBARGO_TYPE_FILE => ['embargo_node__file', 'embargo_node__node'], - EmbargoInterface::EMBARGO_TYPE_NODE => ['embargo_node__node'], + EmbargoInterface::EMBARGO_TYPE_FILE => ['embargo_node__file'], + EmbargoInterface::EMBARGO_TYPE_NODE => ['embargo_node__node', 'embargo_node__file'], }; $fields = $item->getFields(FALSE); @@ -274,8 +274,18 @@ public function preprocessSearchQuery(QueryInterface $query) : void { } $query->addCacheContexts([ + // Caching by groups of ranges instead of individually should promote + // cacheability. + 'ip.embargo_range', + // Exemptable users, so need to deal with them. 'user', ]); + // Embargo dates deal with granularity to the day. + $query->mergeCacheMaxAge(24 * 3600); + + /** @var \Drupal\Core\Entity\EntityTypeInterface $embargo_type */ + $embargo_type = $this->entityTypeManager->getDefinition('embargo'); + $query->addCacheTags($embargo_type->getListCacheTags()); $query->addTag('embargo_join_processor'); $query->setOption('embargo_join_processor', $info);