Skip to content

Commit

Permalink
More cache info.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Mar 4, 2024
1 parent 12ba90e commit 80d5b99
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 71 deletions.
4 changes: 0 additions & 4 deletions embargo.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ services:
- '@url_generator'
tags:
- { name: 'event_subscriber' }
embargo.search_api.pre_execute_subscriber:
class: Drupal\embargo\EventSubscriber\ViewsCacheEventSubscriber
tags:
- { name: 'event_subscriber' }
63 changes: 0 additions & 63 deletions src/EventSubscriber/ViewsCacheEventSubscriber.php

This file was deleted.

17 changes: 13 additions & 4 deletions src/Plugin/search_api/processor/EmbargoProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\embargo\Plugin\search_api\processor;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
Expand Down Expand Up @@ -100,6 +101,8 @@ public function preIndexSave() {
* {@inheritDoc}
*/
public function preprocessSearchQuery(QueryInterface $query) : void {
assert($query instanceof RefinableCacheableDependencyInterface);
$query->addCacheContexts(['user.permissions']);
if ($this->currentUser->hasPermission('bypass embargo access')) {
return;
}
Expand Down Expand Up @@ -133,13 +136,15 @@ public function preprocessSearchQuery(QueryInterface $query) : void {
* The query to which to add filters.
*/
protected function addEmbargoFilters(string $datasource_id, QueryInterface $query) : ?ConditionGroupInterface {
assert($query instanceof RefinableCacheableDependencyInterface);
$or_group = $query->createConditionGroup('OR', [
"embargo:$datasource_id",
]);

// No embargo.
if ($field = $this->findField($datasource_id, 'embargo:entity:id')) {
$or_group->addCondition($field->getFieldIdentifier(), NULL);
$query->addCacheTags(['embargo_list']);
}

// Embargo duration/schedule.
Expand All @@ -158,16 +163,19 @@ protected function addEmbargoFilters(string $datasource_id, QueryInterface $quer
0 => date('Y-m-d', strtotime('+1 DAY', $this->time->getRequestTime())),
1 => date('Y-m-d', PHP_INT_MAX),
], 'NOT BETWEEN');
// Cacheable up to a day.
$query->mergeCacheMaxAge(24 * 3600);
}

$or_group->addConditionGroup($schedule_group);
}

if (
!$this->currentUser->isAnonymous() &&
($field = $this->findField($datasource_id, 'embargo:entity:exempt_users:entity:uid'))
) {
if ($this->currentUser->isAnonymous()) {
$query->addCacheContexts(['user.roles:anonymous']);
}
elseif ($field = $this->findField($datasource_id, 'embargo:entity:exempt_users:entity:uid')) {
$or_group->addCondition($field->getFieldIdentifier(), $this->currentUser->id());
$query->addCacheContexts(['user']);
}

if ($field = $this->findField($datasource_id, 'embargo:entity:exempt_ips:entity:id')) {
Expand All @@ -177,6 +185,7 @@ protected function addEmbargoFilters(string $datasource_id, QueryInterface $quer
->getClientIp()) as $ipRange) {
$or_group->addCondition($field->getFieldIdentifier(), $ipRange->id());
}
$query->addCacheContexts(['ip']);
}

return (count($or_group->getConditions()) > 0) ? $or_group : NULL;
Expand Down

0 comments on commit 80d5b99

Please sign in to comment.