尝试通过 Magento 的整页缓存获取动态内容

编程入门 行业动态 更新时间:2024-10-10 10:27:23
本文介绍了尝试通过 Magento 的整页缓存获取动态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am using Magento Enterprise 1.10.1.1 and need to get some dynamic content on our product pages. I am inserting the current time in a block to quickly see if it is working, but can't seem to get through full page cache.

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

I have tried a variety of implementations found here:

tweetorials.tumblr/post/10160075026/ee-full-page-cache-hole-punchingoggettoweb/blog/customizations-compatible-magento-全页缓存/magentophp.blogspot/2011/02/magento-enterprise-full-page-caching.html

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

Any solutions, thoughts, comments, advice is welcome.

这是我的代码:

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

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

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

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

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

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

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

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

snippet from 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" />

推荐答案

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

The <name> in the cache.xml must match your blocks full name in the layout, not the alias, e.g. <name>product.info.example</name>

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

Also, _getIdentifier() isn't implemented on Enterprise_PageCache_Model_Container_Abstract, just remove it from the string returned by your _getCacheId(). If you need to add some variants, implement _getIdentifier() to return a session id or whatever you need.

更多推荐

尝试通过 Magento 的整页缓存获取动态内容

本文发布于:2023-11-01 10:59:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   内容   动态   Magento

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!