在插件中停用Shopware 5中的HTTP缓存

McP*_*hil 1 shopware

在插件中,我需要为两个类别停用Shopware HTTP-Cache.该手册说我应该发出这样的事件:

Shopware()->Events()->notify(
    'Shopware_Plugins_HttpCache_InvalidateCacheId',
    array(
        'cacheId' => 'a14',
    )
);
Run Code Online (Sandbox Code Playgroud)

a14代表ID为14的文章.根据手册,ac可用于解冻类别页面.所以我把它放在我的插件bootstrap.php中,以阻止ID为113和114的类别的缓存:

public function afterInit()
{
    Shopware()->Events()->notify(
        'Shopware_Plugins_HttpCache_InvalidateCacheId',
        array(
            'cacheId' => 'c113',
            'cacheId' => 'c114',
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

我已在所有级别上手动清空缓存,但没有任何反应,无论是好还是坏,没有抛出错误,并且在清空后重建缓存时不会从缓存中删除类别.有人知道我应该改变什么吗?

这是完整的解决方案,感谢Thomas的回答,一切都在Bootstrap.php中完成:

首先订阅PostDispatch_Frontend_Listing事件:

public function install() 
{
    $this->subscribeEvent('Enlight_Controller_Action_PostDispatch_Frontend_Listing', 'onPostDispatchListing');
    return true;
}
Run Code Online (Sandbox Code Playgroud)

其次创建一个函数,在某些条件下发送no-cache-header:

public function onPostDispatchListing(Enlight_Event_EventArgs $arguments)
{
    $response = $arguments->getResponse();
    $categoryId = (int)Shopware()->Front()->Request()->sCategory;
    if ($categoryId === 113 || $categoryId === 114) {
        $response->setHeader('Cache-Control', 'private, no-cache');
    }
}
Run Code Online (Sandbox Code Playgroud)

第三次安装或重新安装插件,以便对事件的订阅将保留在数据库中.

Tho*_*mas 6

我认为最好的方法是添加一个插件,Cache-Control: no-cache为指定类别的响应添加标题.设置此标头时,类别不会存储在HTTP缓存中,您无需使其无效.

您可以监听Enlight_Controller_Action_PostDispatch_Frontend_Listing事件并检查类别ID是否是您需要的,并将标头添加到响应中.

$response->setHeader('Cache-Control', 'private, no-cache');
Run Code Online (Sandbox Code Playgroud)