magento禁用价格块的缓存 [英] magento disable cache for price block

查看:79
本文介绍了magento禁用价格块的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我们使用的是Magento企业版1.14.1.商店中的价格直接从数据库中动态更改.因此,问题在于Magento缓存了所有内容(全页缓存),因此更改不会在前端生效.因此,我们决定禁用该特定块的缓存.我知道它可以在布局文件中完成.我检查了layout catalog.xml,发现它是块

In my project we are using Magento Enterprise Edition 1.14.1. The prices in store are changed dynamically direct from Database. So the problem is Magento cache every thing (full page cache) so the changes are not effected in front end. So we are decided disable the cache for that particular block. I know it can done in layout files. I checked in layout catalog.xml and I found it the block

<block type="catalog/product_price_template" name="catalog_product_price_template" />

所以在这里我不知道如何禁用它.我尝试在 app/etc/local.xml 中将的缓存时间设置为null

So here I don't know how to disable it. I have tried set fife time of cache to null in app/etc/local.xml ,

 <layout>
    <default>
         <reference name="catalog_product_price_template">
            <action method="setCacheLifetime" />
        </reference>
    </default>
    </layout>

并在layout/catalog.xml中尝试过

and tried in layout/catalog.xml

<block type="catalog/product_price_template" name="catalog_product_price_template" >
         <action method="setCacheLifetime" />
        </block>

并尝试过,

<block type="catalog/product_price" name="Mage_Catalog_Block_Product_Price">
        <action method="setCacheLifetime"><value>false</value></action>
        </block>

 <block type="catalog/product_price" name="catalog_product_price">
            <action method="setCacheLifetime"><s>null</s></action>
            </block>

但是没有运气.

我在layout/bundle.xml文件中发现了其他一些价格块.我们也在使用捆绑产品.因此,我们也必须禁用此缓存吗?任何帮助,将不胜感激.谢谢.

And I have found some other price blocks in layout/bundle.xml file. We are using bundle product as well. So we have to disable this cache also ? Any help would be appreciated. Thanks.

推荐答案

在深入研究了magento缓存技术之后,我遇到了一个决定.在此之前,我想描述一下我得到了什么.基本上有两种解决方案可以为特定的块或缓存标签禁用缓存.

After deep digging in magento caching techniques I came across one decision. Before that I want describe that what I got. There is basically 2 solutions to disable cache for particular block or caching tags.

1.使用阻止"功能禁用缓存

这类似于设置缓存的生存期

It is something like set lifetime of cache

<reference name="needed block">
    <action method="setCacheLifetime"><s>null</s></action>
</reference>

但是在我们的情况下,magento没有提供产品价格的特定限制.价格模板直接在抽象类中定义,

But in our case magento doesn’t have specific block for rendering the price of the product. The price template is directly defined in abstract class,

Mage_Catalog_Block_Product_Abstract 

protected $_priceBlockDefaultTemplate = 'catalog/product/price.phtml';

而且这种类型的缓存也只会清除块缓存. 因此,我们无法使用此解决方案.

And also this type cache will clear only the Block cache. So we can’t use this solution.

2.打孔: Magento还有另一个默认功能,那就是我们可以使用缓存标签覆盖FPC流程.

2. Hole punching : Magento having another default functionality which is we can override the FPC process using cache tags.

Magento通过标签名称保存缓存.这样我们就可以在页面渲染之前获得该标签,并且可以再次禁用该标签.因此,每次页面加载时,该标签都不会被缓存.

Magento saving caches by tag names. So that we can get that tags before the page rendering and we can disable to cache that tag again. So every time when the page loads that tag will not be cached.

我试图使用观察者(core_block_abstract_to_html_before)来实现这一点,

I was trying to implement this using observer (core_block_abstract_to_html_before),

public function disableCache(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();

if ($block instanceof Mage_Catalog_Block_Product_Price) {

//echo 'got it'; exit;
$cacheKeyData = array(
Mage_Catalog_Block_Product_Price::CACHE_TAG,
$block->getBlockId(),
Mage::app()->getStore()->getId(),
intval(Mage::app()->getStore()->isCurrentlySecure())
);
}
}

但是价格没有CACHE_TAG(价格没有定义).

But there is no CACHE_TAG for price (not defined for price).

我们可以使用__construct()函数直接在价格类中设置缓存的生存时间,例如

And we can set the life time of cache directly in price class using __construct() function, like

$this->addData(array(
‘cache_lifetime’=> false,
‘cache_tags’    => array(Mage_Core_Model_Store::CACHE_TAG, Mage_Catalog_Product_Price::CACHE_TAG)
));

就像我上面提到的,价格没有CACHE_TAG.因此,我们也无法使用此技巧.

As like above I mentioned, there is no CACHE_TAG for price. So we can’t use this trick also.

但是我发现一件事,如果我们从管理员保存产品,那么更改立即反映在前端.因此,我在其中进行了更多研究,然后找到了解决方案.

But I found one thing that if we save the product from admin then immediately the changes is reflecting in front end. So I dig more in that and I found the solution.

Magento调用以下控制动作,

Magento calls the following control action,

Mage_Adminhtml_Catalog_ProductController::saveAction()  

这将调用以下方法,

Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId)

如果看到此功能,它们将清除缓存并重新编制索引.

If you see this function they clear cache and re-indexing.

public function applyAllRulesToProduct($product)
    {
      .......    
        $this->getResource()->applyAllRules($product);
        $this->_invalidateCache();

        Mage::getSingleton('index/indexer')->processEntityAction(
            new Varien_Object(array('id' => $product->getId())),
            Mage_Catalog_Model_Product::ENTITY,
            Mage_Catalog_Model_Product_Indexer_Price::EVENT_TYPE_REINDEX_PRICE
        );

        return $this;
    } 

因此,就我而言,我仅使用以下代码.

So in my case I just using the following code.

$object->setPrice(555);
Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);
$object->save();

这篇关于magento禁用价格块的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆