Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions modules/rl_page_title/rl_page_title.module
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function rl_page_title_help($route_name, RouteMatchInterface $route_match) {
/**
* Implements hook_preprocess_page_title().
*
* Captures the original title text in a per-request static so the
* Captures the original title text in request-only memory so the
* preprocess_html hook can do a targeted substitution within whatever
* structure the head_title has been built into (which may already have
* been mutated by metatag, dxpr_seo, etc.).
Expand All @@ -48,8 +48,7 @@ function rl_page_title_preprocess_page_title(array &$variables) {
}
$original = (string) $variables['title'];
if ($original !== '') {
$captured = &drupal_static('rl_page_title_original', NULL);
$captured = $original;
\Drupal::service('rl.runtime_cache')->set('rl_page_title_original', $original);
}
$variables['title'] = $result['text'];
}
Expand Down Expand Up @@ -88,7 +87,7 @@ function rl_page_title_preprocess_html(array &$variables) {
// Resolve the original title text. Prefer the value captured during
// preprocess_page_title; fall back to the title resolver for pages
// without a rendered page title block.
$original = drupal_static('rl_page_title_original', NULL);
$original = \Drupal::service('rl.runtime_cache')->get('rl_page_title_original')->data ?? NULL;
if ($original === NULL || $original === '') {
$original = _rl_page_title_resolve_route_title();
}
Expand Down
8 changes: 7 additions & 1 deletion rl.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ services:
tags:
- { name: service_collector, tag: rl_experiment_decorator, call: addDecorator }

# Keep request-only values in memory on every supported core version.
rl.runtime_cache:
class: Drupal\Core\Cache\MemoryCache\MemoryCache
arguments: ['@datetime.time']

rl.cache_manager:
class: Drupal\rl\Service\CacheManager
arguments: ['@config.factory']
arguments: ['@config.factory', '@rl.runtime_cache']

rl.cache_response_subscriber:
class: Drupal\rl\EventSubscriber\CacheResponseSubscriber
arguments: ['@rl.runtime_cache']
tags:
- { name: event_subscriber }

Expand Down
14 changes: 10 additions & 4 deletions src/EventSubscriber/CacheResponseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\rl\EventSubscriber;

use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
Expand All @@ -12,10 +13,15 @@
*/
class CacheResponseSubscriber implements EventSubscriberInterface {

/**
* Constructs the cache response subscriber.
*/
public function __construct(protected MemoryCacheInterface $runtimeCache) {}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
public static function getSubscribedEvents(): array {
return [
KernelEvents::RESPONSE => 'onResponse',
];
Expand All @@ -28,7 +34,7 @@ public static function getSubscribedEvents() {
* The response event.
*/
public function onResponse(ResponseEvent $event) {
$cache_override = &drupal_static('rl_cache_override');
$cache_override = $this->runtimeCache->get('rl_cache_override')->data ?? NULL;

if ($cache_override !== NULL) {
$response = $event->getResponse();
Expand All @@ -39,8 +45,8 @@ public function onResponse(ResponseEvent $event) {
$response->getCacheableMetadata()->setCacheMaxAge($cache_override);
}

// Clear the static variable.
$cache_override = NULL;
// Consume the override so it cannot affect another response.
$this->runtimeCache->delete('rl_cache_override');
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/Service/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\rl\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;

/**
* Cache manager service for RL experiments.
Expand All @@ -21,8 +22,10 @@ class CacheManager {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $runtimeCache
* The request-only memory cache.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
public function __construct(ConfigFactoryInterface $config_factory, protected MemoryCacheInterface $runtimeCache) {
$this->configFactory = $config_factory;
}

Expand All @@ -43,8 +46,7 @@ public function overridePageCacheIfShorter(int $experiment_cache_seconds): void

// Experiment cache is shorter than site cache - store for response
// subscriber.
$cache_override = &drupal_static('rl_cache_override');
$cache_override = $experiment_cache_seconds;
$this->runtimeCache->set('rl_cache_override', $experiment_cache_seconds);
}

}
Loading