Answers:
我发现在Magento Enterprise的“全页缓存”模块中打孔CMS块的最简单方法有几个步骤:
首先,让我们看一下所需的目录结构:
BranchLabs/CacheBuster/
Block/Cms.php # We inherit almost all functions from the Mage CMS
block, only overriding the "getCacheKeyInfo" function.
We do this to set the CMS block ID for later use by
our placeholder model.
etc/cache.xml # Here we target our module's version of the CMS block
and set their cache lifetimes to 0.
Model/Placeholder.php # This module is responsible for freshly rendering our
CMS blocks every time they're requested.
考虑到这种自上而下的理解,这里是如何填写这些文件的方法。
创建您自己的块类,以扩展内置的Magento CMS块。您还需要重写“ getCacheKeyInfo”函数,如下所示:
<?php
// BranchLabs/CacheBuster/Block/Cms.php
class BranchLabs_CacheBuster_Block_Cms extends Mage_Cms_Block_Block {
// Used to set the cache placeholder attribute definitions, required in
// the placeholder's "_renderBlock" function.
public function getCacheKeyInfo() {
return array('block_id' => $this->getBlockId());
}
}
设置占位符模型,该模型负责呈现CMS块而不应用缓存。
<?php
// BranchLabs/CacheBuster/Model/Placeholder.php
class BranchLabs_CacheBuster_Model_Placeholder extends Enterprise_PageCache_Model_Container_Abstract {
public function applyWithoutApp(&$content)
{
return false;
}
protected function _getCacheId()
{
$id = 'CACHEBUSTER_HOLEPUNCH_' . microtime() . '_' . rand(0,99);
return $id;
}
/**
* CacheBuster doesn't cache data! Do nothing.
*/
protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
{
return $this;
}
/**
* Render fresh block content.
*
* @return false|string
*/
protected function _renderBlock()
{
$block = $this->_placeholder->getAttribute('block');
$block = new $block;
// Get the block_id attribute we originally set in our CMS block's
// getCacheKeyInfo function.
$block_id = $this->_placeholder->getAttribute('block_id');
$block->setBlockId($block_id);
$block->setLayout(Mage::app()->getLayout());
return $block->toHtml();
}
}
设置cache.xml以定位我们新创建的CMS块并使用我们新创建的占位符进行呈现。
<!-- BranchLabs/CacheBuster/etc/cache.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<config>
<placeholders>
<arbitrary_unique_identifier>
<block>cachebuster/cms</block>
<placeholder>ARBITRARY_UNIQUE_IDENTIFIER</placeholder>
<container>BranchLabs_CacheBuster_Model_Placeholder</container>
<cache_lifetime>0</cache_lifetime>
</arbitrary_unique_identifier>
</placeholders>
</config>
在CMS中,将您要尝试在缓存外部渲染的块的块类型替换为我们新创建的CMS验证块: {{block type="cachebuster/cms" block_id="cacheproof"}}