Answers:
我在EE 1.14.2中遇到了这个问题,看来在CE 1.9.2中也出现了同样的问题。我记录了有关此SE问题的问题和解决方案。
基本上是由于以下代码被添加到的构造函数中Mage_Cms_Block_Block
:
$this->setCacheTags(array(Mage_Cms_Model_Block::CACHE_TAG));
$this->setCacheLifetime(false);
CMS静态块现在已缓存。问题来自于如何生成缓存键信息。可以归结为Mage_Core_Block_Abstract
在布局中使用块名称的行为。如果未在布局中添加块(例如在cms页面上),则此名称不存在。这可能导致静态块共享相同的缓存密钥并在缓存中混淆。
我的解决方案是覆盖Mage_Cms_Block_Block
该类,并根据块ID和当前存储设置缓存键信息。
/**
* Override cms/block to add cache key. This started being a problem as of EE 1.14.2 and CE 1.9.2 when the _construct
* method was added which turns on caching for cms blocks
*/
class Mysite_Cms_Block_Block extends Mage_Cms_Block_Block
{
/**
* If this block has a block id, use that as the cache key.
*
* @return array
*/
public function getCacheKeyInfo()
{
if ($this->getBlockId()) {
return array(
Mage_Cms_Model_Block::CACHE_TAG,
Mage::app()->getStore()->getId(),
$this->getBlockId(),
(int) Mage::app()->getStore()->isCurrentlySecure()
);
} else {
return parent::getCacheKeyInfo();
}
}
}
显然,这需要使用config.xml
文件和块替代等添加到您自己的模块中。或者,您可以将其复制Mage_Cms_Block_Block
到本地代码池并在其中添加缓存键。
您可以在此处查看 1.9.2中添加的新行
这里我们使用基于本地模块的解决方案,因为上述解决方案没有提供完整的步骤。我们需要创建自定义模块,因为大家都知道Magento Boogieman会帮到您!如果改变核心:)
您将需要以下文件: app/etc/modules/Bhupendra_Cms.xml
<?xml version="1.0"?>
<config>
<modules>
<Bhupendra_Cms>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Cms/>
</depends>
</Bhupendra_Cms>
</modules>
</config>
app/code/local/Bhupendra/Cms/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Bhupendra_Cms>
<version>1.0.0</version>
</Bhupendra_Cms>
</modules>
<global>
<blocks>
<cms>
<rewrite>
<block>Bhupendra_Cms_Block_Block</block>
<widget_block>Bhupendra_Cms_Block_Widget_Block</widget_block>
</rewrite>
</cms>
</blocks>
</global>
</config>
app/code/local/Bhupendra/Cms/Block/Block.php
<?php
class Bhupendra_Cms_Block_Block extends Mage_Cms_Block_Block {
public function getCacheKeyInfo()
{
$blockId = $this->getBlockId();
if ($blockId) {
$result = array(
$blockId,
Mage::app()->getStore()->getCode(),
);
} else {
$result = parent::getCacheKeyInfo();
}
return $result;
}
}
app/code/local/Bhupendra/Cms/Block/Widget/Block.php
class Bhupendra_Cms_Block_Widget_Block extends Mage_Cms_Block_Widget_Block
{
/**
* Storage for used widgets
*
* @var array
*/
static protected $_widgetUsageMap = array();
/**
* Prepare block text and determine whether block output enabled or not
* Prevent blocks recursion if needed
*
* @return Mage_Cms_Block_Widget_Block
*/
protected function _beforeToHtml()
{
parent::_beforeToHtml();
$blockId = $this->getData('block_id');
$blockHash = get_class($this) . $blockId;
if (isset(self::$_widgetUsageMap[$blockHash])) {
return $this;
}
self::$_widgetUsageMap[$blockHash] = true;
if ($blockId) {
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load($blockId);
if ($block->getIsActive()) {
/* @var $helper Mage_Cms_Helper_Data */
$helper = Mage::helper('cms');
$processor = $helper->getBlockTemplateProcessor();
$this->setText($processor->filter($block->getContent()));
$this->addModelTags($block);
}
}
unset(self::$_widgetUsageMap[$blockHash]);
return $this;
}
/**
* Retrieve values of properties that unambiguously identify unique content
*
* @return array
*/
public function getCacheKeyInfo()
{
$result = parent::getCacheKeyInfo();
$blockId = $this->getBlockId();
if ($blockId) {
$result[] = $blockId;
}
return $result;
}
}
有关更多信息,您可以访问以下链接,也可以从 以下链接下载https://www.milople.com/blogs/ecommerce/solved-magento-static-block-display-issue.html
没有官方的补丁程序,但是,它已在CE 1.9.2.1中解决。
diff -r magento-CE-1.9.2.0/app/code/core/Mage/Cms/Block/Block.php magento-CE-1.9.2.1/app/code/core/Mage/Cms/Block/Block.php
74a75,94
>
> /**
> * Retrieve values of properties that unambiguously identify unique content
> *
> * @return array
> */
> public function getCacheKeyInfo()
> {
> $blockId = $this->getBlockId();
> if ($blockId) {
> $result = array(
> 'CMS_BLOCK',
> $blockId,
> Mage::app()->getStore()->getCode(),
> );
> } else {
> $result = parent::getCacheKeyInfo();
> }
> return $result;
> }
diff -r magento-CE-1.9.2.0/app/code/core/Mage/Cms/Block/Widget/Block.php magento-CE-1.9.2.1/app/code/core/Mage/Cms/Block/Widget/Block.php
84a85
> $this->addModelTags($block);
90a92,106
>
> /**
> * Retrieve values of properties that unambiguously identify unique content
> *
> * @return array
> */
> public function getCacheKeyInfo()
> {
> $result = parent::getCacheKeyInfo();
> $blockId = $this->getBlockId();
> if ($blockId) {
> $result[] = $blockId;
> }
> return $result;
> }
注意:据报道,多个商店视图上的CMS页面仍然存在问题:
Magento CE 1.9.2.1仅部分修复了此问题。
对于多个商店视图上的CMS页面,问题仍然存在。这是一个更新的修补程序(请注意,这不是官方补丁):https : //gist.github.com/tux-rampage/77b286f7973336877f7b#file-luka-mce20150805-1-9-2-1-caching-hotfix-patch
来源:http://www.magentocommerce.com/products/bug-tracking/issue/index/id/870
Magento的Piotr暂时针对此问题发布了一个非官方的补丁程序:https : //gist.github.com/piotrekkaminski/ecd245e8c9390e4020db
似乎可以解决问题。是的,它正在编辑核心,但是在Magento发行正式补丁或下一版本之前解决了该问题。
在最新版本的Magento中,它通过新的安全功能进行了增强。您可以在system-> permissions中为静态块添加权限。
我的商店也遇到了同样的问题。到目前为止,我发现的最佳解决方法是为受影响的块停用缓存。您可以通过将块的缓存生存期设置为null来实现。
全局禁用实时站点上的“阻止HTML输出”缓存不是一个好主意,因为它不必要地影响站点性能。
为xml中的一个块禁用缓存:
<block ... >
...
<action method="unsetData"><key>cache_lifetime</key></action>
<action method="unsetData"><key>cache_tags</key></action>
</block>
在php中为一个块禁用缓存:
$this->getLayout()->createBlock('cms/block')
->setCacheLifetime(null)
->setBlockId('block-id')
->toHtml();
请勿将本文指出的缓存生存期设置为“ 0”
$block->setCacheLifeTime("null");
Note NULL
和“ null”是两个不同的事物(后一个是字符串),因此将无法获得预期的结果。
您将必须进行完全升级或回传1.9.2.0
1.9.2.1中的CMS Block和Widget缓存更改
magento-1921 / app / code / core / Mage / Cms / Block / Block.php
diff -r magento-1920/app/code/core/Mage/Cms/Block/Block.php magento-1921/app/code/core/Mage/Cms/Block/Block.php
74a75,94
>
> /**
> * Retrieve values of properties that unambiguously identify unique content
> *
> * @return array
> */
> public function getCacheKeyInfo()
> {
> $blockId = $this->getBlockId();
> if ($blockId) {
> $result = array(
> 'CMS_BLOCK',
> $blockId,
> Mage::app()->getStore()->getCode(),
> );
> } else {
> $result = parent::getCacheKeyInfo();
> }
> return $result;
> }
magento-1921 / app / code / core / Mage / Cms / Block / Widget / Block.php
diff -r magento-1920/app/code/core/Mage/Cms/Block/Widget/Block.php magento-1921/app/code/core/Mage/Cms/Block/Widget/Block.php
84a85
> $this->addModelTags($block);
89a91,105
> }
>
> /**
> * Retrieve values of properties that unambiguously identify unique content
> *
> * @return array
> */
> public function getCacheKeyInfo()
> {
> $result = parent::getCacheKeyInfo();
> $blockId = $this->getBlockId();
> if ($blockId) {
> $result[] = $blockId;
> }
> return $result;
我正在使用Magento 1.9.3.8,问题仍然存在。
您可以在这里找到我的解决方法:
基本上,我将基于页面url和blockId的唯一字符串添加到每个缓存键信息,因此每个块将具有唯一键:
/**
* Generates a string based on the page url (for example category/product pages) and concatenate the block id to the url
* Removes the caracters: /, . , &, = and , from this string
*/
private function generateUrlBasedString($blockId = null)
{
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = '_' . $url->getPath();
$path = str_replace('/', '', $path);
$path = str_replace('.', '', $path);
$path = str_replace('&', '', $path);
$path = str_replace(',', '', $path);
$path = str_replace('=', '', $path);
if(isset($blockId)) {
$path .= '_' . $blockId;
}
return $path;
}
/**
* Retrieve values of properties that unambiguously identify unique content
*
* @return array
*/
public function getCacheKeyInfo()
{
$blockId = $this->getBlockId();
if ($blockId) {
$result = array(
'CMS_BLOCK',
$blockId,
Mage::app()->getStore()->getCode() . $this->generateUrlBasedString($blockId),
);
} else {
$result = parent::getCacheKeyInfo();
}
return $result;
}
在Magento为该问题准备修复程序之前,您可以创建以下文件:
应用程序/代码/本地/法师/厘米/块/Block.php
并插入上述github网址中的代码作为内容。
此代码已针对Magento 1.9.2。*和1.9.3。*进行了测试
这已在1.9.2版本中确认为错误,暂时您可以通过从管理员->缓存管理部分禁用“阻止HTML输出”缓存来解决此问题。
希望能帮助到你