Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use separate block caches for secure and unsecure shop access in Magento?

Tags:

magento

I use the Magento block cache for the top navigation block. The problem is the block has to generate some urls for files in the skin directory that cannot be put into css files as the file names depend on category model data.

Now when I open magento using a secure connection (https://) the navigation block is fetched from the cache and is sent to the browser but with the http:// urls resulting in a warning in most browsers about unsecure elements on the page.

I'd like the have separat caches for secure and unsecure connections. The navigation block extends the class Mage_Catalog_Block_Navigation and therefore has the following cache configuration:

    $this->addData(array(
        'cache_lifetime'    => false,
        'cache_tags'        => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Core_Model_Store_Group::CACHE_TAG),
    ));
like image 722
Uwe Mesecke Avatar asked May 07 '10 16:05

Uwe Mesecke


1 Answers

Hmmm simpler than I thought...

I tried overriding the method getCacheKey() by appending a flag with the current security status but at first this was no success but after several cache purges this seems to work now:

public function getCacheKey()
{
    $key = parent::getCacheKey();
    $key .= Mage::app()->getStore()->isCurrentlySecure() ? '_S' : '_U';

    return $key;
}
like image 139
Uwe Mesecke Avatar answered Nov 10 '22 01:11

Uwe Mesecke