/** * Save data to cache storage. Store many block instances in one cache record depending on additional cache ids. * * @param string $data * @param string $id * @param array $tags * @param null|int $lifetime * @return Enterprise_PageCache_Model_Container_Advanced_Abstract */ protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { $additionalCacheId = $this->_getAdditionalCacheId(); if (!$additionalCacheId) { Mage::throwException(Mage::helper('enterprise_pagecache')->__('Additional id should not be empty')); } $tags[] = Enterprise_PageCache_Model_Processor::CACHE_TAG; if (is_null($lifetime)) { $lifetime = $this->_placeholder->getAttribute('cache_lifetime') ? $this->_placeholder->getAttribute('cache_lifetime') : false; } /** * Replace all occurrences of session_id with unique marker */ Enterprise_PageCache_Helper_Url::replaceSid($data); $result = array(); $cacheRecord = parent::_loadCache($id); if ($cacheRecord) { $cacheRecord = json_decode($cacheRecord, true); if ($cacheRecord) { $result = $cacheRecord; } } $result[$additionalCacheId] = $data; Enterprise_PageCache_Model_Cache::getCacheInstance()->save(json_encode($result), $id, $tags, $lifetime); return $this; }
/** * Prepare content for saving * * @param string $content */ public static function prepareContentPlaceholders(&$content) { /** * Replace all occurrences of session_id with unique marker */ Enterprise_PageCache_Helper_Url::replaceSid($content); /** * Replace all occurrences of form_key with unique marker */ Enterprise_PageCache_Helper_Form_Key::replaceFormKey($content); }
/** * Generate placeholder content before application was initialized and apply to page content if possible * * @param string $content * @return bool */ public function applyWithoutApp(&$content) { $cacheId = $this->_getCacheId(); if ($cacheId === false) { $this->_applyToContent($content, ''); return true; } $block = $this->_loadCache($cacheId); if ($block === false) { return false; } $block = Enterprise_PageCache_Helper_Url::replaceUenc($block); $this->_applyToContent($content, $block); return true; }
/** * Clear request path cache by tag * (used for redirects invalidation) * * @param Varien_Event_Observer $observer * @return $this */ public function clearRequestCacheByTag(Varien_Event_Observer $observer) { if (!$this->isCacheEnabled()) { return $this; } $redirect = $observer->getEvent()->getRedirect(); Enterprise_PageCache_Model_Cache::getCacheInstance()->clean(array(Enterprise_PageCache_Helper_Url::prepareRequestPathTag($redirect->getData('identifier')), Enterprise_PageCache_Helper_Url::prepareRequestPathTag($redirect->getData('target_path')), Enterprise_PageCache_Helper_Url::prepareRequestPathTag($redirect->getOrigData('identifier')), Enterprise_PageCache_Helper_Url::prepareRequestPathTag($redirect->getOrigData('target_path')))); return $this; }
/** * Save data to cache storage * * @param string $data * @param string $id * @param array $tags * @param null|int $lifetime * @return Enterprise_PageCache_Model_Container_Abstract */ protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { $tags[] = Enterprise_PageCache_Model_Processor::CACHE_TAG; if (is_null($lifetime)) { $lifetime = $this->_placeholder->getAttribute('cache_lifetime') ? $this->_placeholder->getAttribute('cache_lifetime') : false; } /** * Replace all occurrences of session_id with unique marker */ Enterprise_PageCache_Helper_Url::replaceSid($data); Enterprise_PageCache_Model_Cache::getCacheInstance()->save($data, $id, $tags, $lifetime); return $this; }
/** * Process response body by specific request * * @param Zend_Controller_Request_Http $request * @param Zend_Controller_Response_Http $response * @return Enterprise_PageCache_Model_Processor */ public function processRequestResponse(Zend_Controller_Request_Http $request, Zend_Controller_Response_Http $response) { // we should add original path info tag as another way we can't drop some entities from cron job $this->addRequestTag(Enterprise_PageCache_Helper_Url::prepareRequestPathTag($request->getOriginalPathInfo())); $cacheInstance = Enterprise_PageCache_Model_Cache::getCacheInstance(); /** * Basic validation for request processing */ if ($this->canProcessRequest($request)) { $processor = $this->getRequestProcessor($request); if ($processor && $processor->allowCache($request)) { $this->setMetadata('cache_subprocessor', get_class($processor)); $cacheId = $this->prepareCacheId($processor->getPageIdInApp($this)); $content = $processor->prepareContent($response); /** * Replace all occurrences of session_id with unique marker */ Enterprise_PageCache_Helper_Url::replaceSid($content); Enterprise_PageCache_Helper_Form_Key::replaceFormKey($content); if (function_exists('gzcompress')) { $content = gzcompress($content); } $contentSize = strlen($content); $currentStorageSize = (int) $cacheInstance->load(self::CACHE_SIZE_KEY); if (Mage::getStoreConfig(Enterprise_PageCache_Model_Processor::XML_PATH_CACHE_DEBUG)) { $response->setBody(implode(', ', $this->getRequestTags()) . $response->getBody()); } $maxSizeInBytes = Mage::getStoreConfig(self::XML_PATH_CACHE_MAX_SIZE) * 1024 * 1024; if ($currentStorageSize >= $maxSizeInBytes) { Mage::app()->getCacheInstance()->invalidateType('full_page'); return $this; } $cacheInstance->save($content, $cacheId, $this->getRequestTags()); $cacheInstance->save($currentStorageSize + $contentSize, self::CACHE_SIZE_KEY, $this->getRequestTags()); /* * Save design change in cache */ $designChange = Mage::getSingleton('core/design'); if ($designChange->getData()) { $cacheInstance->save(serialize($designChange->getData()), $this->getRequestCacheId() . self::DESIGN_CHANGE_CACHE_SUFFIX, $this->getRequestTags()); } // save response headers $this->setMetadata('response_headers', $response->getHeaders()); // save original routing info $this->setMetadata('routing_aliases', Mage::app()->getRequest()->getAliases()); $this->setMetadata('routing_requested_route', Mage::app()->getRequest()->getRequestedRouteName()); $this->setMetadata('routing_requested_controller', Mage::app()->getRequest()->getRequestedControllerName()); $this->setMetadata('routing_requested_action', Mage::app()->getRequest()->getRequestedActionName()); $this->setMetadata('sid_cookie_name', Mage::getSingleton('core/session')->getSessionName()); Mage::dispatchEvent('pagecache_processor_metadata_before_save', array('processor' => $this)); $this->_saveMetadata(); } if (isset($_GET[Mage_Core_Model_Session_Abstract::SESSION_ID_QUERY_PARAM])) { Mage::getSingleton('enterprise_pagecache/cookie')->updateCustomerCookies(); Mage::getModel('enterprise_pagecache/observer')->updateCustomerProductIndex(); } } return $this; }
/** * Clear request path cache by tag * (used for redirects invalidation) * * @param Varien_Event_Observer $observer * @return $this */ public function clearRequestCacheByTag(Varien_Event_Observer $observer) { $redirects = $observer->getEvent()->getRedirects(); foreach ($redirects as $redirect) { Enterprise_PageCache_Model_Cache::getCacheInstance()->clean(array(Enterprise_PageCache_Helper_Url::prepareRequestPathTag($redirect['identifier']))); } return $this; }
/** * Process response body by specific request * * @param Zend_Controller_Request_Http $request * @param Zend_Controller_Response_Http $response * @return Enterprise_PageCache_Model_Processor */ public function processRequestResponse(Zend_Controller_Request_Http $request, Zend_Controller_Response_Http $response) { /** * Basic validation for request processing */ if ($this->canProcessRequest($request)) { $processor = $this->getRequestProcessor($request); if ($processor && $processor->allowCache($request)) { $this->setMetadata('cache_subprocessor', get_class($processor)); $cacheId = $this->prepareCacheId($processor->getPageIdInApp($this)); $content = $processor->prepareContent($response); /** * Replace all occurrences of session_id with unique marker */ Enterprise_PageCache_Helper_Url::replaceSid($content); if (function_exists('gzcompress')) { $content = gzcompress($content); } Mage::app()->saveCache($content, $cacheId, $this->getRequestTags()); // save original routing info $this->setMetadata('routing_aliases', Mage::app()->getRequest()->getAliases()); $this->setMetadata('routing_requested_route', Mage::app()->getRequest()->getRequestedRouteName()); $this->setMetadata('routing_requested_controller', Mage::app()->getRequest()->getRequestedControllerName()); $this->setMetadata('routing_requested_action', Mage::app()->getRequest()->getRequestedActionName()); $this->setMetadata('sid_cookie_name', Mage::getSingleton('core/session')->getSessionName()); $this->_saveMetadata(); } } return $this; }