Skip to content

Commit

Permalink
Start thinking to roll in more cache headers; however, shouldn't be n…
Browse files Browse the repository at this point in the history
…ecessary.
  • Loading branch information
adam-vessey committed Mar 4, 2024
1 parent 15b901b commit 12ba90e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions embargo.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ services:
- '@url_generator'
tags:
- { name: 'event_subscriber' }
embargo.search_api.pre_execute_subscriber:
class: Drupal\embargo\EventSubscriber\ViewsCacheEventSubscriber
tags:
- { name: 'event_subscriber' }
63 changes: 63 additions & 0 deletions src/EventSubscriber/ViewsCacheEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Drupal\embargo\EventSubscriber;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\search_api\Event\QueryPreExecuteEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Drupal\search_api\Query\QueryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Views cache event subscriber implementation.
*/
class ViewsCacheEventSubscriber implements EventSubscriberInterface {

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() {
return [
SearchApiEvents::QUERY_PRE_EXECUTE => 'preExecute',
//'search_api.query_pre_execute.alter_cache_metadata' => ['alterCacheMetadata'],
];
}

public function preExecute(QueryPreExecuteEvent $event) {
dsm($event->getQuery()->getTags());
if ($event->getQuery()->hasTag('alter_cache_metadata')) {
$this->alterCacheMetadata($event);
}

$query = $event->getQuery();
if ($query instanceof RefinableCacheableDependencyInterface) {
dsm($query->getCacheContexts());
dsm($query->getCacheTags());
dsm($query->getCacheMaxAge());
}
}

/**
* Alter cache metadata.
*
* @param \Drupal\search_api\Event\QueryPreExecuteEvent $event
* The pre-execution event.
*/
public function alterCacheMetadata(QueryPreExecuteEvent $event) : void {
$query = $event->getQuery();
if (!($query instanceof RefinableCacheableDependencyInterface)) {
dsm('blah');
// Cannot add metadata to it.
return;
}
if ($this->containsRelevantIndex($query)) {
//$query->add
}
}

protected function containsRelevantIndex(QueryInterface $query) : bool {
dsm($query->getTags());
return FALSE;
}

}

0 comments on commit 12ba90e

Please sign in to comment.