public function setNeverExpireBrowserCacheHeader(Mage_Core_Controller_Request_Http $httpRequest, Mage_Core_Controller_Response_Http $response) { if (!Mage::app()->useCache('config')) { return $this; } if (!$httpRequest->has(self::FUZE_VALUE_CACHE_KEY)) { Mage::log("MegaMenu cache problem: You cannot set never expire browser cache, because this http request has not been secured by the fuze url param."); return $this; } $expires = gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 3600 * self::HOURS_IN_BROWSER_CACHE); $maxAge = self::HOURS_IN_BROWSER_CACHE * 3600; $response->setHeader('Pragma', 'public', true); $response->setHeader('Expires', $expires, true); $response->setHeader('Cache-Control', "public, max-age={$maxAge}", true); return $this; }
/** * Cache a url so that when the associated tags are cleared the url can be added to a list of urls to be * invalidated by an external process. * * Set the Cache-Control header so the proxy will cache the response. * * @param Mage_Core_Controller_Response_Http $response * @param $lifetime */ public function httpResponseSendBefore(Mage_Core_Controller_Response_Http $response, $lifetime) { // Cache the url with all of the related tags (prefixed) $cacheKey = $this->getCacheKey(); if ($lastModified = Mage::app()->getCacheInstance()->getFrontend()->test($cacheKey)) { // TODO - touch cache record? } else { $url = $this->getBaseUrl() . Mage::app()->getRequest()->getRequestUri(); $tags = $this->_getCacheTags($this->helper()->getTags()); $tags[] = self::CACHE_TAG; Mage::app()->saveCache($url, self::PREFIX_KEY . $cacheKey, $tags, $lifetime); } // Set a header so the page is cached $cacheControl = sprintf(Mage::getStoreConfig('system/diehard/cachecontrol'), $lifetime); $response->setHeader('Cache-Control', $cacheControl, true); }
/** * When caching a page simply generate and cache a random value as the ETag * * @param Mage_Core_Controller_Response_Http $response * @param $lifetime */ public function httpResponseSendBefore(Mage_Core_Controller_Response_Http $response, $lifetime) { $useEtags = Mage::getStoreConfigFlag('system/diehard/use_etags'); $cacheKey = $this->getCacheKey(); // Use existing cache data if it exists in case there are multiple upstream proxies // If a record exists then any content generated at the time the record was is assumed to not be stale if (!($cacheData = Mage::app()->loadCache($cacheKey))) { $fullActionName = $this->helper()->getFullActionName(); if ($useEtags) { $cacheData = $fullActionName . ':' . sha1(microtime() . mt_rand()); } else { $cacheData = $fullActionName . ':' . $this->_rfc1123Date(); } $tags = $this->helper()->getTags(); $tags[] = Cm_Diehard_Helper_Data::CACHE_TAG; Mage::app()->saveCache($cacheData, $cacheKey, $tags, $lifetime); } list($fullActionName, $cacheData) = explode(':', $cacheData, 2); // Set headers so the page is cached with the ETag/Last-Modified value for invalidation session_cache_limiter(''); $cacheControl = sprintf(Mage::getStoreConfig('system/diehard/cachecontrol'), $lifetime); $response->setHeader('Cache-Control', $cacheControl, true); $response->setHeader('Expires', $this->_rfc1123Date(time() + $lifetime), true); if ($useEtags) { $response->setHeader('ETag', 'W/"' . $cacheData . '"', true); } else { $response->setHeader('Last-Modified', $cacheData, true); } }
/** * Apply custom Cache-Control: max-age from db * * @param Mage_Core_Controller_Request_Http $request * @param Mage_Core_Controller_Response_Http $response */ protected function _applyCustomMaxAgeFromDb(Mage_Core_Controller_Request_Http $request, Mage_Core_Controller_Response_Http $response) { if (!$this->messagesToShow) { // apply custom max-age from db $urls = array($request->getRequestString()); $alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS); if ($alias) { $urls[] = $alias; } /** @var $customUrlModel Aoe_Static_Model_CustomUrl */ $customUrlModel = Mage::getModel('aoestatic/customUrl'); $customUrlModel->setStoreId(Mage::app()->getStore()->getId()); $customUrlModel->loadByRequestPath($urls); if ($customUrlModel->getId() && $customUrlModel->getMaxAge()) { $response->setHeader('Cache-Control', 'max-age=' . (int) $customUrlModel->getMaxAge(), true); $response->setHeader('X-Magento-Lifetime', (int) $customUrlModel->getMaxAge(), true); $response->setHeader('aoestatic', 'cache', true); } } }