caching - 尝试通过 Magento 的整页缓存来打洞动态内容

标签 caching magento enterprise hole-punching

我正在使用 Magento Enterprise 1.10.1.1,需要在我们的产品页面上获取一些动态内容。我将当前时间插入一个 block 中以快速查看它是否正常工作,但似乎无法通过全页缓存。

我尝试了这里找到的各种实现:

http://tweetorials.tumblr.com/post/10160075026/ee-full-page-cache-hole-punching http://oggettoweb.com/blog/customizations-compatible-magento-full-page-cache/ http://magentophp.blogspot.com/2011/02/magento-enterprise-full-page-caching.html

欢迎任何解决方案、想法、评论、建议。

这是我的代码:

app/code/local/Fido/Example/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Fido_Example>
            <version>0.1.0</version>
        </Fido_Example>
    </modules>
    <global>
        <blocks>
            <fido_example>
                <class>Fido_Example_Block</class>
            </fido_example>     
        </blocks>
    </global>
</config>

app/code/local/Fido/Example/etc/cache.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <placeholders>
        <fido_example>
            <block>fido_example/view</block>
            <name>example</name>
            <placeholder>CACHE_TEST</placeholder>
            <container>Fido_Example_Model_Container_Cachetest</container>
            <cache_lifetime>86400</cache_lifetime>
        </fido_example>
    </placeholders>
</config>

app/code/local/Fido/Example/Block/View.php

<?php

class Fido_Example_Block_View extends Mage_Core_Block_Template
{
    private $message;
    private $att;

    protected function createMessage($msg) {
        $this->message = $msg;
    }

    public function receiveMessage() {
        if($this->message != '') {
            return $this->message;
        } 
        else {
            $this->createMessage('Hello World');
            return $this->message;
        }
    }

    protected function _toHtml() {
        $html = parent::_toHtml();

        if($this->att = $this->getMyCustom() && $this->getMyCustom() != '') {
            $html .= '<br />'.$this->att;
        } 
        else {

        $now = date('m-d-Y h:i:s A');
        $html .= $now;
        $html .= '<br />' ;
        }

        return $html;
    }

}

app/code/local/Fido/Example/Model/Container/Cachetest.php

<?php

class Fido_Example_Model_Container_Cachetest extends Enterprise_PageCache_Model_Container_Abstract { 

    protected function _getCacheId()
    {
        return 'HOMEPAGE_PRODUCTS' . md5($this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier());
    }

    protected function _renderBlock()
    {
        $blockClass = $this->_placeholder->getAttribute('block');
        $template = $this->_placeholder->getAttribute('template');

        $block = new $blockClass;
        $block->setTemplate($template);
        return $block->toHtml();
    }

protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { return false; }  

}

app/design/frontend/enterprise/[mytheme]/template/example/view.phtml

<?php echo $this->receiveMessage() ?>

来自 app/design/frontend/enterprise/[mytheme]/layout/catalog.xml 的片段

<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
            <block type="fido_example/view" name="product.info.example"  as="example" template="example/view.phtml" />

最佳答案

<name> cache.xml 中的 block 必须与布局中的 block 全名匹配,而不是别名,例如<name>product.info.example</name>

此外,_getIdentifier()未在 Enterprise_PageCache_Model_Container_Abstract 上实现,只需将其从 _getCacheId() 返回的字符串中删除即可。 如果需要添加一些变体,请执行 _getIdentifier()返回 session ID 或任何您需要的内容。

关于caching - 尝试通过 Magento 的整页缓存来打洞动态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8126548/

相关文章:

Windows Azure 缓存 - 命名约定

api - Magento 2 Rest Api 在产品更新时过于频繁地清除缓存

php - url重写导致Magento admin无法访问

android - 在 Android 中连接到 WPA2 Enterprise 后 WiFi 无法工作

javascript - 在 angularjs 服务中缓存数组

google-app-engine - 使用 AppEngine 后端进行缓存的可行性/值(value)?

Magento - 在 vi​​ew.phtml 中获取某个类别的项目总数

magento 注册表已经

ios - 为什么我不能删除或删除 iPhone/iPad 重复的企业应用程序图标副本?

ios - xcode 使用 "in house"方法分发 ipa