Unverified Commit 9f0dfcac authored by Steve Persch's avatar Steve Persch Committed by GitHub
Browse files

D8/D9 - Configurable tag overriding (#30)



* 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: default avatarClay Freeman <git@clayfreeman.com>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* Update README.md

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* Update src/EventSubscriber/CacheableResponseSubscriber.php

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>

* codesniff fix

Co-authored-by: default avatarClay Freeman <git@clayfreeman.com>
parent 4f0808dc
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -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.
+1 −0
Original line number Diff line number Diff line
override_list_tags: false
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
<?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();
}
+1 −1
Original line number Diff line number Diff line
@@ -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 }
+36 −3
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Symfony\Component\HttpKernel\KernelEvents;
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.
@@ -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;
  }

  /**
@@ -49,9 +80,11 @@ class CacheableResponseSubscriber implements EventSubscriberInterface {

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

      $tags_string = implode(' ', $tags);
      if (25000 < strlen($tags_string)) {
Loading