/** * Generate ESI data to be encoded in URL * * @param Mage_Core_Block_Template $blockObject * @param array $esiOptions * @return Varien_Object */ protected function _getEsiData($blockObject, $esiOptions) { Varien_Profiler::start('turpentine::observer::esi::_getEsiData'); $esiHelper = Mage::helper('turpentine/esi'); $cacheTypeParam = $esiHelper->getEsiCacheTypeParam(); $scopeParam = $esiHelper->getEsiScopeParam(); $methodParam = $esiHelper->getEsiMethodParam(); $esiData = new Varien_Object(); $esiData->setStoreId(Mage::app()->getStore()->getId()); $esiData->setDesignPackage(Mage::getDesign()->getPackageName()); $esiData->setDesignTheme(Mage::getDesign()->getTheme('layout')); $esiData->setNameInLayout($blockObject->getNameInLayout()); $esiData->setBlockType(get_class($blockObject)); $esiData->setLayoutHandles($this->_getBlockLayoutHandles($blockObject)); $esiData->setEsiMethod($esiOptions[$methodParam]); if ($esiOptions[$cacheTypeParam] == 'private' || $esiOptions[$cacheTypeParam] == 'customer_group') { if (is_array(@$esiOptions['flush_events'])) { $esiData->setFlushEvents(array_merge($esiHelper->getDefaultCacheClearEvents(), array_keys($esiOptions['flush_events']))); } else { $esiData->setFlushEvents($esiHelper->getDefaultCacheClearEvents()); } } if ($esiOptions[$scopeParam] == 'page') { $esiData->setParentUrl(Mage::app()->getRequest()->getRequestString()); } if (is_array($esiOptions['dummy_blocks'])) { $dummyBlocks = array(); foreach ($esiOptions['dummy_blocks'] as $key => $value) { $dummyBlocks[] = empty($value) && !is_numeric($key) ? $key : $value; } $esiData->setDummyBlocks($dummyBlocks); } else { Mage::helper('turpentine/debug')->logWarn('Invalid dummy_blocks for block: %s', $blockObject->getNameInLayout()); } $simpleRegistry = array(); $complexRegistry = array(); if (is_array($esiOptions['registry_keys'])) { foreach ($esiOptions['registry_keys'] as $key => $options) { $value = Mage::registry($key); if ($value) { if (is_object($value) && $value instanceof Mage_Core_Model_Abstract) { $complexRegistry[$key] = $this->_getComplexRegistryData($options, $value); } else { $simpleRegistry[$key] = $value; } } } } else { Mage::helper('turpentine/debug')->logWarn('Invalid registry_keys for block: %s', $blockObject->getNameInLayout()); } $esiData->setSimpleRegistry($simpleRegistry); $esiData->setComplexRegistry($complexRegistry); Varien_Profiler::stop('turpentine::observer::esi::_getEsiData'); return $esiData; }
/** * Called on * core_block_abstract_to_html_before * * Checks if the "esi" variable is set on a block. * If yes, the template of the block is replaced by varnish/esi.phtml which contains the <esi:include> tag * it also adds to the response the header X-magento-doesi which will be interpreted by varnish and tell it to do the esi processing * * @param array $eventObject */ public function injectEsi($eventObject) { //No ESI injection if the module is disabled or the request is made on HTTPS if (!Mage::helper('varnish')->isVarnishModuleEnabled() || Mage::app()->getRequest()->isSecure()) { return; } $block = $eventObject->getBlock(); if ($block instanceof Mage_Core_Block_Template) { $esi = $block->getEsi(); if ($esi == true) { //We don't allow ESI in admin, for now. Maybe in future releases. if (Mage::app()->getStore()->getCode() == 'admin') { throw new Mage_Adminhtml_Exception("ESI includes are forbidden in Admin"); } // We replace the template of the block by the varnish/esi.phtml template // The HTML of our template will replace the real HTML of the block $block->setTemplate('varnish/esi.phtml'); $src = new Varien_Object(); //Blocks change depending on the store id, so we keep track of that $src->setStoreId(Mage::app()->getStore()->getId()); //Blocks also change depending on the design so we keep track of the package and the theme of the current block $src->setDesignPackage(Mage::getDesign()->getPackageName()); $src->setDesignTheme(Mage::getDesign()->getTheme('layout')); $src->setNameInLayout($block->getNameInLayout()); /* * Set the cache type * per-client * per-page * global * * * per-client tells varnish to cache the block per-client basis. * per-page tells varnish to cache the content based on the url of the page. * global tells varnish to serve the same cached version to every client. * * The per-client cache is based on the frontend cookie of the client. * * We default the cache type to "global" */ if (empty($esi['cache_type'])) { $esi['cache_type'] = 'global'; } $src->setCacheType($esi['cache_type']); /** * If the block is cached on a per-page basis * we create an entry in our ESI table to keep track the URLs where the block appear * */ if ($src->getCacheType() === 'per-page') { $parentUrl = Mage::app()->getRequest()->getRequestString(); $src->setParentUrl($parentUrl); } /** * Expiry (or TTL in Varnish lingo). How lon will the object be stored in Varnish? * TODO: make sure the expiry is in format 1d 24h 1140m 86400s */ if (!empty($esi['expiry'])) { $src->setExpiry($esi['expiry']); } else { if ($src->getCacheType() === 'per-client') { $src->setExpiry(Mage::getStoreConfig('varnish/cache/per_client_default_expiry')); } else { if ($src->getCacheType() === 'per-page') { $src->setExpiry(Mage::getStoreConfig('varnish/cache/per_page_default_expiry')); } else { $src->setExpiry(Mage::getStoreConfig('varnish/cache/global_default_expiry')); } } } //We create a unique fingerprint with all our values foreach ($src->getData() as $value) { $this->_hash($value); } // $src is the source for our <esi:include> it is composed of all the above variables $src->setUrl("/varnish/cache/getBlock/cachetype/{$src->getCacheType()}/expiry/{$src->getExpiry()}/fingerprint/{$this->_hash()}"); /** * Registry save: * some block rely on values stored in the Mage::registry(). * For example, the product page relies on Mage::registry('current_product'); * The problem is that the Mage::registry is not persistent between requests. This means that once the request is served, * the registry looses its data. * This means that when Varnish makes the ESI request, the registry is empty and if the block makes a call to Mage::registry('current_product') * the function will return null. * In order to strive this problem, when you setEsi on a block using the mage registry, you need to specify in the layout which keys you want to keep. * These keys will be saved in the magento cache and thus be accessible when the ESI request is made */ if (!empty($esi['registry_keys'])) { // We create an array with the <registry_keys></registry_keys> set in the layout $registryKeys = explode(',', $esi['registry_keys']); // We iterate through each of the registrykey... foreach ($registryKeys as $registryKey) { $registryContent = Mage::registry($registryKey); if ($registryContent !== null) { // If the key exist we save the content in an array $registry[] = array("key" => $registryKey, "content" => $registryContent); } } $src->setRegistry($registry); } $src->setBlockType(get_class($block)); // and we save the content in the cache with the hash as the id $cache = Mage::helper('varnish')->getRedisCache(); $tags = array("VARNISH_CACHETYPE_{$src->getCacheType()}", "VARNISH_BLOCKTYPE_{$src->getBlockType()}", "VARNISH_BLOCKNAME_{$src->getNameInLayout()}"); $cache->save(serialize($src), $this->_hash(), $tags, null); $block->setSrc($src); // Tell varnish to do esi processing on the page Mage::getSingleton('varnish/cache')->setDoEsi(true); } } }