简体   繁体   中英

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.

$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. 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. 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. But I thought I'd check if there were any known "gotchas" with caching a block set by createBlock in controller.

Thanks! Wilson

Easy but tricky.

First you should insert the block throug the xml (it works better with the cache).

Second you have just to insert a pice of code like this one on the block file (ex: /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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