пятница, 30 ноября 2012 г.

Interesting facts about Full Page Cache in Magento.

1.
File: 'app/code/core/Enterprise/PageCache/Model/Processor.php'

const XML_PATH_CACHE_MAX_SIZE       = 'system/page_cache/max_cache_size';

Method: public function processRequestResponse(Zend_Controller_Request_Http $request, Zend_Controller_Response_Http $response)

$maxSizeInBytes = Mage::getStoreConfig(self::XML_PATH_CACHE_MAX_SIZE) * 1024 * 1024;


if ($currentStorageSize >= $maxSizeInBytes) {
    Mage::app()->getCacheInstance()->invalidateType('full_page');
    return $this;
}


By default 'max_cache_size' = 1Gb.


2. Magento 1.7 CE and 1.12 EE has a bug.
'cache_lifetime' option in 'cache.xml' is not used by Magento. It means that if something was cached it won't be rendered whenever.

There is a way how to fix it.

Create your module.
Rewrite the class 'Enterprise_PageCache_Model_Config'.

This is an example of 'etc/config.xml' file for your module.


    <global>
        <models>
            <you_entpagecache>
                <class>YOU_EnterprisePageCache_Model</class>
            </you_entpagecache>
            <enterprise_pagecache>
                <rewrite>
                    <config>YOU_EnterprisePageCache_Model_Config</config>
                </rewrite>
            </enterprise_pagecache>
        </models>
    </global>


This is a new class 'YOU_EnterprisePageCache_Model_Config' without the bug.

class YOU_EnterprisePageCache_Model_Config  extends Enterprise_PageCache_Model_Config
{
    /**
     * Create placeholder object based on block information
     *
     * @param Mage_Core_Block_Abstract $block
     * @return Enterprise_PageCache_Model_Container_Placeholder
     */
    public function getBlockPlaceholder($block)
    {
        $this->_initPlaceholders();
        $type = $block->getType();

        if (isset($this->_placeholders[$type])) {
            $placeholderData = false;
            foreach ($this->_placeholders[$type] as $placeholderInfo) {
                if (!empty($placeholderInfo['name'])) {
                    if ($placeholderInfo['name'] == $block->getNameInLayout()) {
                        $placeholderData = $placeholderInfo;
                    }
                } else {
                    $placeholderData = $placeholderInfo;
                }
            }

            if (!$placeholderData) {
                return false;
            }

            $placeholder = $placeholderData['code']
                . ' container="' . $placeholderData['container'] . '"'
                . ' block="' . get_class($block) . '"';
            $placeholder.= ' cache_id="' . $block->getCacheKey() . '"';
            foreach ($block->getCacheKeyInfo() as $k => $v) {
                if (is_string($k) && !empty($k)) {
                    $placeholder .= ' ' . $k . '="' . $v . '"';
                }
            }
            // Fixed bug -- 'cache_lifetime' is not used for checking that block should be rendered because it is expired.
            $placeholder .= ' cache_lifetime="' . $placeholderData['cache_lifetime'] . '"';

            $placeholder = Mage::getModel('enterprise_pagecache/container_placeholder', $placeholder);
            return $placeholder;
        }
        return false;
    }
}