Skip to content

Commit

Permalink
D8/D9 - Configurable tag overriding (#30)
Browse files Browse the repository at this point in the history
* adding placeholder for readme update

* adding override setting

* override fixing

* test

* update function

* install config

* readme

* better readme

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* Update README.md

Co-authored-by: Clay Freeman <[email protected]>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: Clay Freeman <[email protected]>

* codesniff fix

Co-authored-by: Clay Freeman <[email protected]>
  • Loading branch information
stevector and clayfreeman authored Jan 4, 2021
1 parent 4f0808d commit 9f0dfca
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ A direct way of inspecting headers is with `curl -I`. This command will make a r

`curl -IH "Pantheon-Debug:1" https://dev-cache-tags-demo.pantheonsite.io/ | grep -i Surrogate-Key-Raw`

## Changing Listing Tags

Prior to the 1.2 release, this module would change the cache tags used on default listings.
This changing of was done to make cache hits more likely but resulted in [confusing cache clearing behavior](https://www.drupal.org/project/pantheon_advanced_page_cache/issues/2944229).
Sites that installed this module prior to 1.2 should uninstall and reinstall or run this command to update their settings.

```
terminus drush [MACHINE-NAME-OF-SITE].[ENV-NAME] -- config:set pantheon_advanced_page_cache.settings --input-format=yaml "override_list_tags" "false"
```

## Limit on header size

Pantheon's nginx configuration limits total header size to 32k.
Expand Down
1 change: 1 addition & 0 deletions config/install/pantheon_advanced_page_cache.settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
override_list_tags: false
13 changes: 13 additions & 0 deletions pantheon_advanced_page_cache.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @file
* Update functions for the Pantheon Advanced Page Cache module.
*/

/**
* Set override_list_tags to TRUE for backwards compatibility. We recommend manually changing to FALSE for more consistent clearing. See README
*/
function pantheon_advanced_page_cache_update_8001() {
\Drupal::configFactory()->getEditable('pantheon_advanced_page_cache.settings')->set('override_list_tags', TRUE)->save();
}
2 changes: 1 addition & 1 deletion pantheon_advanced_page_cache.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ services:
- { name: cache_tags_invalidator }
pantheon_advanced_page_cache.cacheable_response_subscriber:
class: Drupal\pantheon_advanced_page_cache\EventSubscriber\CacheableResponseSubscriber
arguments: ['@logger.channel.pantheon_advanced_page_cache']
arguments: ['@logger.channel.pantheon_advanced_page_cache', '@config.factory']
tags:
- { name: event_subscriber }
39 changes: 36 additions & 3 deletions src/EventSubscriber/CacheableResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\Log\LoggerInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Config\ConfigFactoryInterface;

/**
* Adds Surrogate-Key header to cacheable master responses.
Expand All @@ -21,14 +22,44 @@ class CacheableResponseSubscriber implements EventSubscriberInterface {
*/
protected $logger;

/**
* Configuration Factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs a new DefaultExceptionHtmlSubscriber.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Configuration for this module.
*/
public function __construct(LoggerInterface $logger) {
public function __construct(LoggerInterface $logger, ConfigFactoryInterface $config_factory = NULL) {
if (!$config_factory instanceof ConfigFactoryInterface) {
@trigger_error('Not passing the config factory service as the second parameter to ' . __METHOD__ . ' is deprecated in pantheon_advanced_page_cache:8.x-1.2 and will throw a type error in pantheon_advanced_page_cache:8.x-2.0. Pass an instance of \\Drupal\\Core\\Config\\ConfigFactoryInterface. See https://www.drupal.org/node/2944229', E_USER_DEPRECATED);
$config_factory = \Drupal::service('config.factory');
}
$this->logger = $logger;
$this->configFactory = $config_factory;
}

/**
* Returns whether entity_list tags should be overridden.
*
* Overriding these tags was the initial behavior of the 1.0 version of this
* module. That is no longer recommended.
*/
public function getOverrideListTagsSetting() {
$config = $this->configFactory->get('pantheon_advanced_page_cache.settings');
// Only return FALSE if this config value is really set to false.
// A null value should return TRUE for backwards compatibility.
if ($config->get('override_list_tags') === FALSE) {
return FALSE;
}
return TRUE;
}

/**
Expand All @@ -49,8 +80,10 @@ public function onRespond(FilterResponseEvent $event) {

// Rename all _list cache tags to _emit_list to avoid clearing list cache
// tags by default.
foreach ($tags as $key => $tag) {
$tags[$key] = str_replace('_list', '_emit_list', $tag);
if ($this->getOverrideListTagsSetting()) {
foreach ($tags as $key => $tag) {
$tags[$key] = str_replace('_list', '_emit_list', $tag);
}
}

$tags_string = implode(' ', $tags);
Expand Down
21 changes: 21 additions & 0 deletions tests/behat/features/override_list_tag.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Listing tags
In order to control caching of lists
As an administrator
I want to toggle the setting for overriding cache tags

@api
Scenario: Core behavior
Given there are some "article" nodes
And "/" is caching
When a generate a "article" node
Then "/" has been purged
And "/" is caching

@api @current
Scenario: Old override
Given there are some "article" nodes
When I run drush "config:set pantheon_advanced_page_cache.settings --input-format=yaml override_list_tags true"
And "/" is caching
When a generate a "article" node
And "/" has not been purged

0 comments on commit 9f0dfca

Please sign in to comment.