Skip to content

Commit

Permalink
(#2745) Cache clear when redirect is created
Browse files Browse the repository at this point in the history
Closes #2745
  • Loading branch information
Naveen Sunkara authored and bryanpizzillo committed Jan 15, 2025
1 parent 66f8121 commit 40773e6
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 5 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@
"2834130 : Updates Features Diff to Include Alters": "https://www.drupal.org/files/issues/2019-06-09/features-detect-overrides-update-2834130-4-D8.patch"
},
"drupal/purge": {
"3094343 : Garbled purge tag IDs": "https://www.drupal.org/files/issues/2023-09-07/purge-3094343-15.patch"
"3094343 : Garbled purge tag IDs": "https://www.drupal.org/files/issues/2023-09-07/purge-3094343-15.patch",
"3460094 : Running drush p:queue-add fails (https://www.drupal.org/files/issues/2024-07-25/3460094-5-there-is-no-purger-supporting-array.patch)": "patches/drupal/purge/3460094.diff",
"3460094 : Purge defines drush commands twice (https://www.drupal.org/files/issues/2024-10-18/3460094-remove_drush_services_yml.patch)": "patches/drupal/purge/3460094-2.diff"

},
"drupal/responsive_favicons": {
"3376766 : Incorrect path if Drupal is installed in subfolder": "https://git.drupalcode.org/project/responsive_favicons/-/merge_requests/6.patch",
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function cgov_caching_cdn_install() {
// Set config.
$config_factory = \Drupal::service('config.factory');
$config_installer = new CgovPurgeConfigInstaller($config_factory);
$config_installer->installPurgeConfiguration(['acquia_purge', 'akamai_tag']);
$config_installer->installPurgeConfiguration(['acquia_purge', 'akamai_tag', 'akamai']);
}

/**
Expand All @@ -42,3 +42,13 @@ function cgov_caching_cdn_update_8001() {
$config_installer = new CgovPurgeConfigInstaller($config_factory);
$config_installer->installPurgeConfiguration(['acquia_purge', 'akamai_tag']);
}

/**
* Updates purge config to include Akamai purger for URLs.
*/
function cgov_caching_cdn_update_10001() {
// Set config.
$config_factory = \Drupal::service('config.factory');
$config_installer = new CgovPurgeConfigInstaller($config_factory);
$config_installer->installPurgeConfiguration(['acquia_purge', 'akamai_tag', 'akamai']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Site\Settings;
use Drupal\redirect\Entity\Redirect;
use Drupal\Core\Language\Language;

/**
* Implements hook_block_build_alter().
Expand Down Expand Up @@ -878,3 +881,75 @@ function cgov_core_form_taxonomy_term_form_alter(&$form, FormStateInterface $for
// checkbox and comments.
$form['revision_information']['#access'] = FALSE;
}

/**
* Method to clear source paths for redirects when inserted or updated.
*/
function _cgov_core_clear_redirect_source_cache(Redirect $entity) {
// On local development without purge, make sure we don't fail.
$moduleHandler = \Drupal::service('module_handler');
if (!$moduleHandler->moduleExists('purge')) {
\Drupal::logger('cgov_core')->notice("Attempted to purge redirect without the purge module installed.");
return;
}

// Set up our needed variables for calculating the URL.
// The value for cgdp.cache_url_host is computed in cgov_caching.settings.php.
$host = Settings::get('cgdp.cache_url_host');
$host = rtrim($host, '/');
$sourcePath = '/' . $entity->redirect_source->path;

// The purge invalidation factory helps us construct an invalidation object
// that can be understood by the queuer and purgers.
$purgeInvalidationFactory = \Drupal::service('purge.invalidation.factory');

// If no language is specified for the redirect,
// purge the url for all languages.
$language = [$entity->language->value];
if ($entity->language->value === Language::LANGCODE_NOT_SPECIFIED) {
$langcodes = \Drupal::languageManager()->getLanguages();
$language = array_keys($langcodes);
}

// Create an absolute url for all languages based on $language,
// then create an invalidation object using it.
$invalidations = [];
foreach ($language as $value) {
$language_prefix = \Drupal::config('language.negotiation')->get("url.prefixes.$value");
if (!empty($language_prefix)) {
$sourcePath = Url::fromUri("internal:/{$language_prefix}{$sourcePath}")->toString();
}
$invalidateUrl = $host . $sourcePath;
$invalidations[] = $purgeInvalidationFactory->get('url', $invalidateUrl);
}

// Actually add the invalidations to the purge queue.
$purgeQueuers = \Drupal::service('purge.queuers');
$queuer = $purgeQueuers->get('drush_purge_queue_add');
if (empty($queuer)) {
\Drupal::logger('cgov_core')->notice("Attempted to purge redirect without the drush purgue queuer installed.");
return;
}
$purgeQueue = \Drupal::service('purge.queue');
$purgeQueue->add($queuer, $invalidations);
}

/**
* Implements hook_ENTITY_TYPE_insert().
*
* hook_redirect_insert is called on all new redirect creations, per Redirect module API.
*/
function cgov_core_redirect_insert(Redirect $entity) {
_cgov_core_clear_redirect_source_cache($entity);
}

/**
* Implements hook_ENTITY_TYPE_update().
*
* hook_redirect_update is called on all redirect updates, per Redirect module API.
*/
function cgov_core_redirect_update(Redirect $entity) {
_cgov_core_clear_redirect_source_cache($entity);
// We do not need to clear the $entity->original because the redirect module
// already clears that on updates and deletes via cache tag.
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CgovPurgeConfigInstaller {
private const PURGERS = [
'acquia_purge' => '8813de186f',
'akamai_tag' => 'bf950143a3',
'akamai' => '08153881ce',
];

/**
Expand Down
3 changes: 3 additions & 0 deletions docroot/sites/settings/cgov_caching.settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
$env = CGovSettingsUtil::getEnvironmentName();
$domain = CGovSettingsUtil::getDomain();

// Set the domain for use when purging the cache.
$settings['cgdp.cache_url_host'] = 'https://' . $domain;

// Verify we're on an Acquia-hosted environment.
if (CGovSettingsUtil::isAcquia()) {

Expand Down
57 changes: 57 additions & 0 deletions patches/drupal/purge/3460094-2.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/src/Drush/Commands/QueueCommands.php b/src/Drush/Commands/QueueCommands.php
index 38e64bc..604bb6b 100644
--- a/src/Drush/Commands/QueueCommands.php
+++ b/src/Drush/Commands/QueueCommands.php
@@ -123,6 +123,12 @@ class QueueCommands extends DrushCommands implements SiteAliasManagerAwareInterf
* @hook init p:queue-add
*/
public function queueAddParseExpressions(InputInterface $input, AnnotationData $annotationData) {
+ // This hook is run twice for some reason, so skip if we have already run.
+ $hasinitHookRunAlready = static::initHookStaticVariable();
+ if ($hasinitHookRunAlready) {
+ // Already run.
+ return;
+ }
$raw = trim(implode(" ", $input->getArguments()['expressions']));
$expressions = [];
if ($raw) {
@@ -147,6 +153,15 @@ class QueueCommands extends DrushCommands implements SiteAliasManagerAwareInterf
$input->setArgument('expressions', $expressions);
}

+ /**
+ * Reset static varaible so command can be called again in same request.
+ *
+ * @hook post-init p:queue-add
+ */
+ public function queueAddParseExpressionsPostRun(InputInterface $input, AnnotationData $annotationData) {
+ static::initHookStaticVariable(TRUE);
+ }
+
/**
* Add one or more items to the queue for later processing.
*
@@ -635,4 +650,23 @@ class QueueCommands extends DrushCommands implements SiteAliasManagerAwareInterf
return $result;
}

+ /**
+ * Provide a init hook static variable tracker. Default return is FALSE.
+ *
+ * @param bool $reset
+ * Reset the staic varibale back to TRUE. Return is unaffected.
+ *
+ * @return bool
+ * The previous static, regardless if reset.
+ */
+ protected static function initHookStaticVariable($reset = FALSE) {
+ static $hasInitHookRun = FALSE;
+ $old_state = $hasInitHookRun;
+ $hasInitHookRun = TRUE;
+ if ($reset) {
+ $hasInitHookRun = FALSE;
+ }
+ return $old_state;
+ }
+
}
63 changes: 63 additions & 0 deletions patches/drupal/purge/3460094.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
diff --git a/composer.json b/composer.json
index 08b04a8..3d2a947 100644
--- a/composer.json
+++ b/composer.json
@@ -12,11 +12,6 @@
"extra": {
"branch-alias": {
"dev-8.x-3.x": "3.x-dev"
- },
- "drush": {
- "services": {
- "drush.services.yml": ">=10"
- }
}
}
}
diff --git a/drush.services.yml b/drush.services.yml
deleted file mode 100644
index f026873..0000000
--- a/drush.services.yml
+++ /dev/null
@@ -1,41 +0,0 @@
-services:
- purge_drush.debug_commands:
- class: Drupal\purge\Drush\Commands\DebugCommands
- arguments: ['@purge.logger']
- tags:
- - { name: drush.command }
- purge_drush.diagnostics_command:
- class: Drupal\purge\Drush\Commands\DiagnosticsCommand
- arguments: ['@purge.diagnostics']
- tags:
- - { name: drush.command }
- purge_drush.invalidate_command:
- class: Drupal\purge\Drush\Commands\InvalidateCommand
- arguments: ['@purge.invalidation.factory', '@purge.processors', '@purge.purgers']
- tags:
- - { name: drush.command }
- purge_drush.processor_commands:
- class: Drupal\purge\Drush\Commands\ProcessorCommands
- arguments: ['@purge.processors']
- tags:
- - { name: drush.command }
- purge_drush.purger_commands:
- class: Drupal\purge\Drush\Commands\PurgerCommands
- arguments: ['@purge.purgers']
- tags:
- - { name: drush.command }
- purge_drush.queue_commands:
- class: Drupal\purge\Drush\Commands\QueueCommands
- arguments: ['@purge.processors', '@purge.purgers', '@purge.invalidation.factory', '@purge.queue', '@purge.queue.stats', '@purge.queuers']
- tags:
- - { name: drush.command }
- purge_drush.queuer_commands:
- class: Drupal\purge\Drush\Commands\QueuerCommands
- arguments: ['@purge.queuers']
- tags:
- - { name: drush.command }
- purge_drush.types_command:
- class: Drupal\purge\Drush\Commands\TypesCommand
- arguments: ['@purge.invalidation.factory', '@purge.purgers']
- tags:
- - { name: drush.command }

0 comments on commit 40773e6

Please sign in to comment.