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

时间:2015-12-08 15:01:20

标签: shopware

在插件中,我需要停用两个类别的Shopware HTTP-Cache。 manual说我应该发出这个事件:

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

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

public function install() 
{
    $this->subscribeEvent('Enlight_Controller_Action_PostDispatch_Frontend_Listing', 'onPostDispatchListing');
    return true;
}

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

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

首先订阅PostDispatch_Frontend_Listing事件:

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');
    }
}

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

{{1}}

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

1 个答案:

答案 0 :(得分:6)

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

您可以收听Enlight_Controller_Action_PostDispatch_Frontend_Listing事件并检查类别ID是否是您需要的,并将标题添加到回复中。

$response->setHeader('Cache-Control', 'private, no-cache');