2

嘿 Magento 专业人士,

我使用 Magento 块缓存机制在产品页面上获得更好的性能。它在 Magento CE 1.6.2 上运行良好,但现在我升级到 CE 1.9.0.1 并且块缓存不适用于 Magento 新表单键。

产品页面仍在缓存中,但它们自然会被缓存,包括表单操作中的新表单键。当另一个用户尝试将产品添加到购物车时,它将不起作用,因为其他用户的表单键已被缓存。因此,没有添加任何产品,购物车保持为空。

有没有办法将表单键注入缓存代码或另一种缓存产品页面的方法?

我扩展的 Mage_Catalog_Block_Product_View 中的缓存方法如下所示

protected function _construct()
{
        $this->addData(array(
            'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG . "_" . $this->getProduct()->getId()),
        ));
}

public function getCacheKey()
{
    if (!$this->hasData('cache_key')) {
        //$cacheKey = LAYOUTNAME_STORE+ID_PRODUCT+ID
        $cacheKey = $this->getNameInLayout().'_STORE'.Mage::app()->getStore()->getId().'_PRODUCT'.$this->getProduct()->getId();         
        $this->setCacheKey($cacheKey);
    }
    return $this->getData('cache_key');
}

public function getCacheLifetime()
{     
      if($this->getNameInLayout()!='product.info') return null;
      if(!$this->cacheEnabled()) return null;         
      return 9999999999;
}

public static function cacheEnabled() {
    return true;
}   
4

2 回答 2

4

我现在找到了一个快速修复,但仍在寻找更好的解决方案。现在,我在扩展块中添加了 _afterToHtml($html) 方法,并在从缓存返回 html 之前注入了 form_key。如果其他人需要此快速修复,请确保 if 条件正确。它确保这仅在我真正缓存的块中完成,而不是在其他产品视图块中完成。

所以这是我目前的快速修复:

public function _afterToHtml($html) {
    if($this->getNameInLayout() == 'product.info') {
        $formkey = Mage::getSingleton('core/session')->getFormKey();
        $formkey = "/form_key/".$formkey."/";        
        $html = preg_replace("/\/form_key\/[a-zA-Z0-9,.-]+\//", $formkey, $html); 
    }  
    return parent::_afterToHtml($html);
}

我正在考虑将来切换到全页缓存,但解决这个问题会很好,因为配置另一种缓存方法也需要很多时间。

因此,如果您有比这个肮脏的快速修复更好的主意,请提供帮助。

于 2014-08-04T22:08:26.797 回答
0

我想我已经建立了一个更好的解决方案。我一次修复了整个 HTML 输出,还修复了隐藏表单输入中的form_key字段。

我的分机监听事件controller_front_send_response_before。该方法controllerFrontSendResponseBeforeapp/code/community/JeroenVermeulen/BlockCache/Model/Observer.php

https://github.com/jeroenvermeulen/jeroenvermeulen-blockcache/

最好使用事件而不是覆盖,因为您没有两个扩展覆盖相同核心功能的风险。在 Magento 升级后,事件也更有可能发生。

于 2014-08-28T10:16:50.680 回答