Caching Drupal 8 cookie缓存问题

Caching Drupal 8 cookie缓存问题,caching,drupal,drupal-7,drupal-8,Caching,Drupal,Drupal 7,Drupal 8,我有一个Drupal8站点,它动态地设置cookie值“DrupalVisitor\uCountry”。但是这个值正在被缓存,我无法在页面刷新期间正确检索它 我在theme_preprocess_菜单函数中使用这个值,但它总是返回缓存的cookie值,而不是实际值。我有没有办法克服这种情况 任何帮助都将不胜感激 谢谢使用EventSubscriber:$events[KernelEvents::REQUEST][]=['onRequest'] /** * @file * Contains \

我有一个Drupal8站点,它动态地设置cookie值“DrupalVisitor\uCountry”。但是这个值正在被缓存,我无法在页面刷新期间正确检索它

我在theme_preprocess_菜单函数中使用这个值,但它总是返回缓存的cookie值,而不是实际值。我有没有办法克服这种情况

任何帮助都将不胜感激


谢谢

使用
EventSubscriber:$events[KernelEvents::REQUEST][]=['onRequest']

/**
 * @file
 * Contains \Drupal\kvantstudio\EventSubscriber\KvantstudioEventSubscriber.
 */

namespace Drupal\kvantstudio\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event Subscriber KvantstudioEventSubscriber.
 */
class KvantstudioEventSubscriber implements EventSubscriberInterface {

  /**
   * Code that should be triggered on event specified 
   */
  public function onRequest(GetResponseEvent $event) {
     if (!isset($_COOKIE['Drupal_visitor_userHash'])) {  
        $uuid = \Drupal::service('uuid');
        user_cookie_save(['userHash' => 'user-' . $uuid->generate()]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];       
    $events[KernelEvents::REQUEST][] = ['onRequest'];
    return $events;
  }
}
对我来说,成功了。见下文

不起作用:尝试使用-未在页面请求时触发
KerneleEvents::REQUEST
确实在资产请求时触发

不起作用:试图滥用
服务.yml
,但发现我实际上无法排除此处的页面

renderer.config:
  required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions', ... ]
无效:然后我尝试创建自己的缓存上下文。发现它也没有提供排除的方法。充其量,您可以为特定节点的每个负载生成唯一的缓存,但这会杀死缓存。此外:

确实有效:。它在Drupal缓存之前执行。 最好从页面缓存开始:

请注意,在中间件处理过程中,Drupal可能没有完全引导,某些功能可能不存在


谢谢Mario Vercellotti为我指明了正确的方向

我有类似的场景,但KerneleEvents::REQUEST事件似乎只在非缓存页面上执行,所以对我没有帮助。