简体   繁体   English

在Magento阻止HTML缓存中保存Ajax响应

[英]Save Ajax response in Magento block html cache

For this particular Magento EE site, I've got a controller that creates a block and returns it via Ajax. 对于这个特定的Magento EE网站,我有一个控制器,该控制器创建一个块并通过Ajax返回它。

$this->loadLayout();
$block = $this->getLayout()->createBlock('catalog/product_list_related','catalog.product.related')->setTemplate(...
$this->getResponse()->setBody($block->toHtml());

Simple enough. 很简单。 I'd like to cache the response with the key being SKU - CUSTOMER GROUP - DATE. 我想使用键SKU-客户组-DATE来缓存响应。 I've put the appropriate methods in the block for setting key, tags and lifetime. 我将适当的方法放在用于设置键,标签和生存期的代码块中。 All good. 都好。

However, nothing is saved to the cache. 但是,没有任何内容保存到缓存中。

I figured, since it's an EE site, that the Enterprise_PageCache placeholders were blocking it. 由于这是一个EE网站,因此我认为Enterprise_PageCache占位符会阻止它。 But even after removing those, it's not saving. 但是即使删除了这些内容,它也无法保存。

I've been digging through app/code/core/Mage/Core/Block/Abstract.php to see what's going on. 我一直在研究app / code / core / Mage / Core / Block / Abstract.php以查看发生了什么。 But I thought I'd check if there were any known "gotchas" with caching a block set by createBlock in controller. 但是我以为我会检查是否有任何已知的“陷阱”,它会在控制器中缓存由createBlock设置的块。

Thanks! 谢谢! Wilson 威尔逊

Easy but tricky. 简单但棘手。

First you should insert the block throug the xml (it works better with the cache). 首先,您应该通过xml插入块(它与缓存更好地配合使用)。

Second you have just to insert a pice of code like this one on the block file (ex: /fishpig/wordpress/block/post/post.php) 其次,您只需要在块文件中插入一小段这样的代码即可(例如:/fishpig/wordpress/block/post/post.php)

public function __construct(){
    parent::__construct();
    $this->addData(array(
        'cache_lifetime' => 86400, // lifetime on second
        'cache_tags'     => array(Mage_Core_Model_Store::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG), // cache tag
        'cache_key'      => 'Fishpig_Wordpress_Block_Post_List_'.$this->getRequest()->getRequestUri() // cache unique ID IT MUST BE UNIQUE, MUST BE UNIQUE, MUST BE UNIQUE, MUST BE UNIQUE, MUST BE UNIQUE, MUST BE UNIQUE, MUST BE UNIQUE
    ));
}

If you need any help with that code i might be able to help you. 如果您需要有关该代码的任何帮助,我也许可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM