Version Notes
N/A
Download this release
Release Info
| Developer | Jonathan Selander |
| Extension | Made_Cache |
| Version | 1.4.4 |
| Comparing to | |
| See all releases | |
Code changes from version 1.4.3 to 1.4.4
- app/code/community/Made/Cache/Helper/Varnish.php +2 -2
- app/code/community/Made/Cache/Model/Layout.php +25 -57
- app/code/community/Made/Cache/Model/Observer.php +49 -31
- app/code/community/Made/Cache/Model/Observer/Abstract.php +18 -5
- app/code/community/Made/Cache/Model/Observer/Catalog.php +2 -2
- app/code/community/Made/Cache/Model/Observer/Checkout.php +1 -1
- app/code/community/Made/Cache/Model/Observer/Cms.php +2 -3
- app/code/community/Made/Cache/controllers/VarnishController.php +19 -16
- app/code/community/Made/Cache/etc/config.xml +12 -3
- app/code/community/Made/Cache/varnish/magento.vcl +186 -0
- app/code/community/Made/Cache/varnish/magento_signed_cookies.vcl +47 -0
- app/design/frontend/base/default/layout/madecache.xml +8 -1
- package.xml +4 -4
app/code/community/Made/Cache/Helper/Varnish.php
CHANGED
|
@@ -40,7 +40,7 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
|
|
| 40 |
*/
|
| 41 |
public function getServers()
|
| 42 |
{
|
| 43 |
-
$serversConfig =
|
| 44 |
$servers = array();
|
| 45 |
|
| 46 |
foreach ($serversConfig as $server) {
|
|
@@ -269,4 +269,4 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
|
|
| 269 |
return false;
|
| 270 |
}
|
| 271 |
|
| 272 |
-
}
|
| 40 |
*/
|
| 41 |
public function getServers()
|
| 42 |
{
|
| 43 |
+
$serversConfig = preg_split('/[\r\n]+/', Mage::getStoreConfig('cache/varnish/servers'), null, PREG_SPLIT_NO_EMPTY);
|
| 44 |
$servers = array();
|
| 45 |
|
| 46 |
foreach ($serversConfig as $server) {
|
| 269 |
return false;
|
| 270 |
}
|
| 271 |
|
| 272 |
+
}
|
app/code/community/Made/Cache/Model/Layout.php
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
/**
|
| 3 |
* Handles arbitrary block rendering including different layout
|
| 4 |
* handle definitions
|
| 5 |
-
*
|
| 6 |
* @package Made_Cache
|
| 7 |
* @author info@madepeople.se
|
| 8 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
|
@@ -11,28 +11,28 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 11 |
{
|
| 12 |
/**
|
| 13 |
* Keep track of which blocks to cache
|
| 14 |
-
*
|
| 15 |
* @var array
|
| 16 |
*/
|
| 17 |
protected $_cacheBlocks = array();
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Keep track of which blocks should be converted to ESI tags
|
| 21 |
-
*
|
| 22 |
* @var array
|
| 23 |
*/
|
| 24 |
protected $_esiBlocks = array();
|
| 25 |
-
|
| 26 |
/**
|
| 27 |
* Default cache lifetime
|
| 28 |
-
*
|
| 29 |
* @var int
|
| 30 |
*/
|
| 31 |
const DEFAULT_CACHE_LIFETIME = 9999999999;
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Take cache/noache/ESI tags into concern for block rendering
|
| 35 |
-
*
|
| 36 |
* @return Mage_Core_Model_Layout
|
| 37 |
*/
|
| 38 |
public function generateXml()
|
|
@@ -48,10 +48,14 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 48 |
if (empty($lifetime)) {
|
| 49 |
$lifetime = self::DEFAULT_CACHE_LIFETIME;
|
| 50 |
}
|
| 51 |
-
$
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
}
|
| 54 |
-
|
| 55 |
// Find eventual nocache tags
|
| 56 |
$noCacheList = $xml->xpath("//nocache/*");
|
| 57 |
if (count($noCacheList)) {
|
|
@@ -62,7 +66,7 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 62 |
}
|
| 63 |
}
|
| 64 |
}
|
| 65 |
-
|
| 66 |
// Find blocks that should be represented by ESI tags
|
| 67 |
$esiList = $xml->xpath("//esi/*");
|
| 68 |
if (count($esiList)) {
|
|
@@ -72,7 +76,7 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 72 |
$this->_esiBlocks[$blockName] = array();
|
| 73 |
}
|
| 74 |
}
|
| 75 |
-
|
| 76 |
// Find eventual noesi tags
|
| 77 |
$noEsiList = $xml->xpath("//noesi/*");
|
| 78 |
if (count($noEsiList)) {
|
|
@@ -87,7 +91,7 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 87 |
|
| 88 |
return $this;
|
| 89 |
}
|
| 90 |
-
|
| 91 |
/**
|
| 92 |
* Create layout blocks hierarchy from layout xml configuration
|
| 93 |
*
|
|
@@ -97,51 +101,12 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 97 |
public function generateBlocks($parent=null, $parentIsMain=false)
|
| 98 |
{
|
| 99 |
// Generate parent for single block definitions
|
| 100 |
-
if ($parentIsMain !== false) {
|
| 101 |
$this->_generateBlock($parent, new Varien_Object);
|
| 102 |
}
|
| 103 |
-
if (empty($parent)) {
|
| 104 |
-
$parent = $this->getNode();
|
| 105 |
-
}
|
| 106 |
|
| 107 |
return parent::generateBlocks($parent);
|
| 108 |
}
|
| 109 |
-
|
| 110 |
-
/**
|
| 111 |
-
* Generate cache key for block to be cached via layout XML
|
| 112 |
-
*
|
| 113 |
-
* @param Varien_Simplexml_Element $node
|
| 114 |
-
* @return string
|
| 115 |
-
*/
|
| 116 |
-
protected function _getCacheKey($node)
|
| 117 |
-
{
|
| 118 |
-
if (!empty($node['cache_key'])) {
|
| 119 |
-
$cacheKey = (string)$node['cache_tags'];
|
| 120 |
-
} else {
|
| 121 |
-
$paramKeys = array();
|
| 122 |
-
foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
|
| 123 |
-
// if (is_array($value)) {
|
| 124 |
-
// $value = implode('_', $value);
|
| 125 |
-
// } elseif (is_object($value)) {
|
| 126 |
-
// $newValue = '';
|
| 127 |
-
// foreach ($value->getData() as $dataKey => $dataValue) {
|
| 128 |
-
// $newValue = $dataKey . $dataValue;
|
| 129 |
-
// }
|
| 130 |
-
// $value = $newValue;
|
| 131 |
-
// }
|
| 132 |
-
|
| 133 |
-
$value = Mage::helper('cache')->paramValueToCacheKey($value);
|
| 134 |
-
$paramKeys[] = $key . $value;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
$_customer = Mage::getSingleton('customer/session')->getCustomer();
|
| 138 |
-
$cacheKey = (string)$node['name'] .
|
| 139 |
-
$this->getUpdate()->getCacheId() .
|
| 140 |
-
md5($_customer->getGroupId() .
|
| 141 |
-
join('_', $paramKeys));
|
| 142 |
-
}
|
| 143 |
-
return $cacheKey;
|
| 144 |
-
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Add block object to layout based on XML node data
|
|
@@ -153,22 +118,25 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
|
|
| 153 |
protected function _generateBlock($node, $parent)
|
| 154 |
{
|
| 155 |
parent::_generateBlock($node, $parent);
|
| 156 |
-
|
| 157 |
$blockName = (string)$node['name'];
|
| 158 |
$block = $this->getBlock($blockName);
|
| 159 |
if (!$block) {
|
| 160 |
return $this;
|
| 161 |
}
|
| 162 |
-
|
| 163 |
if (in_array($blockName, array_keys($this->_cacheBlocks))) {
|
| 164 |
-
$block->setData('cache_lifetime', $this->_cacheBlocks[$blockName]);
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
-
|
| 168 |
if (in_array($blockName, array_keys($this->_esiBlocks))) {
|
| 169 |
$block->setData('esi', 1);
|
| 170 |
}
|
| 171 |
-
|
| 172 |
return $this;
|
| 173 |
}
|
| 174 |
}
|
| 2 |
/**
|
| 3 |
* Handles arbitrary block rendering including different layout
|
| 4 |
* handle definitions
|
| 5 |
+
*
|
| 6 |
* @package Made_Cache
|
| 7 |
* @author info@madepeople.se
|
| 8 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
| 11 |
{
|
| 12 |
/**
|
| 13 |
* Keep track of which blocks to cache
|
| 14 |
+
*
|
| 15 |
* @var array
|
| 16 |
*/
|
| 17 |
protected $_cacheBlocks = array();
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Keep track of which blocks should be converted to ESI tags
|
| 21 |
+
*
|
| 22 |
* @var array
|
| 23 |
*/
|
| 24 |
protected $_esiBlocks = array();
|
| 25 |
+
|
| 26 |
/**
|
| 27 |
* Default cache lifetime
|
| 28 |
+
*
|
| 29 |
* @var int
|
| 30 |
*/
|
| 31 |
const DEFAULT_CACHE_LIFETIME = 9999999999;
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Take cache/noache/ESI tags into concern for block rendering
|
| 35 |
+
*
|
| 36 |
* @return Mage_Core_Model_Layout
|
| 37 |
*/
|
| 38 |
public function generateXml()
|
| 48 |
if (empty($lifetime)) {
|
| 49 |
$lifetime = self::DEFAULT_CACHE_LIFETIME;
|
| 50 |
}
|
| 51 |
+
$key = (string)$node->getAttribute('key');
|
| 52 |
+
$this->_cacheBlocks[(string)$node] = array(
|
| 53 |
+
'lifetime' => $lifetime,
|
| 54 |
+
'key' => $key
|
| 55 |
+
);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
+
|
| 59 |
// Find eventual nocache tags
|
| 60 |
$noCacheList = $xml->xpath("//nocache/*");
|
| 61 |
if (count($noCacheList)) {
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
+
|
| 70 |
// Find blocks that should be represented by ESI tags
|
| 71 |
$esiList = $xml->xpath("//esi/*");
|
| 72 |
if (count($esiList)) {
|
| 76 |
$this->_esiBlocks[$blockName] = array();
|
| 77 |
}
|
| 78 |
}
|
| 79 |
+
|
| 80 |
// Find eventual noesi tags
|
| 81 |
$noEsiList = $xml->xpath("//noesi/*");
|
| 82 |
if (count($noEsiList)) {
|
| 91 |
|
| 92 |
return $this;
|
| 93 |
}
|
| 94 |
+
|
| 95 |
/**
|
| 96 |
* Create layout blocks hierarchy from layout xml configuration
|
| 97 |
*
|
| 101 |
public function generateBlocks($parent=null, $parentIsMain=false)
|
| 102 |
{
|
| 103 |
// Generate parent for single block definitions
|
| 104 |
+
if ($parentIsMain !== false && $parent && $parent->getName() === 'block') {
|
| 105 |
$this->_generateBlock($parent, new Varien_Object);
|
| 106 |
}
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
return parent::generateBlocks($parent);
|
| 109 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Add block object to layout based on XML node data
|
| 118 |
protected function _generateBlock($node, $parent)
|
| 119 |
{
|
| 120 |
parent::_generateBlock($node, $parent);
|
| 121 |
+
|
| 122 |
$blockName = (string)$node['name'];
|
| 123 |
$block = $this->getBlock($blockName);
|
| 124 |
if (!$block) {
|
| 125 |
return $this;
|
| 126 |
}
|
| 127 |
+
|
| 128 |
if (in_array($blockName, array_keys($this->_cacheBlocks))) {
|
| 129 |
+
$block->setData('cache_lifetime', $this->_cacheBlocks[$blockName]['lifetime']);
|
| 130 |
+
|
| 131 |
+
if (!empty($this->_cacheBlocks[$blockName]['key'])) {
|
| 132 |
+
$block->setData('cache_key', $this->_cacheBlocks[$blockName]['key']);
|
| 133 |
+
}
|
| 134 |
}
|
| 135 |
+
|
| 136 |
if (in_array($blockName, array_keys($this->_esiBlocks))) {
|
| 137 |
$block->setData('esi', 1);
|
| 138 |
}
|
| 139 |
+
|
| 140 |
return $this;
|
| 141 |
}
|
| 142 |
}
|
app/code/community/Made/Cache/Model/Observer.php
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Manage tag-specific cache cleaning
|
| 4 |
-
*
|
| 5 |
* @package Made_Cache
|
| 6 |
* @author info@madepeople.se
|
| 7 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
|
@@ -9,13 +9,13 @@
|
|
| 9 |
class Made_Cache_Model_Observer
|
| 10 |
{
|
| 11 |
protected $_shouldProfile = null;
|
| 12 |
-
|
| 13 |
/**
|
| 14 |
* Observer that injects cache values into specific blocks, we want
|
| 15 |
* to do it like this instead of block rewrites to prevent other
|
| 16 |
* third-party modules from breaking
|
| 17 |
-
*
|
| 18 |
-
* @param Varien_Event_Observer $observer
|
| 19 |
*/
|
| 20 |
public function setupBlock(Varien_Event_Observer $observer)
|
| 21 |
{
|
|
@@ -29,7 +29,7 @@ class Made_Cache_Model_Observer
|
|
| 29 |
Mage::dispatchEvent('made_cache_setup_block_before', array(
|
| 30 |
'block' => $block
|
| 31 |
));
|
| 32 |
-
|
| 33 |
switch (true) {
|
| 34 |
case $block instanceof Mage_Catalog_Block_Product_View:
|
| 35 |
Mage::getSingleton('cache/observer_catalog')
|
|
@@ -57,16 +57,16 @@ class Made_Cache_Model_Observer
|
|
| 57 |
->applyCartSidebar($block);
|
| 58 |
break;
|
| 59 |
}
|
| 60 |
-
|
| 61 |
Mage::dispatchEvent('made_cache_setup_block_after', array(
|
| 62 |
'block' => $block
|
| 63 |
));
|
| 64 |
}
|
| 65 |
-
|
| 66 |
/**
|
| 67 |
* Clear product cache after a review is saved
|
| 68 |
-
*
|
| 69 |
-
* @param Varien_Event_Observer $observer
|
| 70 |
*/
|
| 71 |
public function reviewSaveAfter(Varien_Event_Observer $observer)
|
| 72 |
{
|
|
@@ -79,26 +79,26 @@ class Made_Cache_Model_Observer
|
|
| 79 |
$_product->cleanCache();
|
| 80 |
}
|
| 81 |
}
|
| 82 |
-
|
| 83 |
/**
|
| 84 |
* CatalogRule invalidates cache on product save, so this must be cleaned
|
| 85 |
-
*
|
| 86 |
-
* @param Varien_Event_Observer $observer
|
| 87 |
*/
|
| 88 |
public function cleanCacheAfterProductSave(Varien_Event_Observer $observer)
|
| 89 |
{
|
| 90 |
$invalidatedTypes = Mage::app()->getCacheInstance()
|
| 91 |
->getInvalidatedTypes();
|
| 92 |
-
|
| 93 |
if (!is_array($invalidatedTypes)) {
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
-
|
| 97 |
$typesToCheck = array(
|
| 98 |
Mage_Core_Block_Abstract::CACHE_GROUP,
|
| 99 |
'full_page'
|
| 100 |
);
|
| 101 |
-
|
| 102 |
foreach ($typesToCheck as $type) {
|
| 103 |
if (isset($invalidatedTypes[Mage_Core_Block_Abstract::CACHE_GROUP])) {
|
| 104 |
Mage::app()->getCacheInstance()
|
|
@@ -109,8 +109,8 @@ class Made_Cache_Model_Observer
|
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Clear quote cache on custom tag
|
| 112 |
-
*
|
| 113 |
-
* @param Varien_Event_Observer $observer
|
| 114 |
*/
|
| 115 |
public function clearQuoteCache(Varien_Event_Observer $observer)
|
| 116 |
{
|
|
@@ -118,20 +118,20 @@ class Made_Cache_Model_Observer
|
|
| 118 |
$object = $observer->getEvent()->getQuote();
|
| 119 |
Mage::app()->cleanCache(array('SALES_QUOTE_' . $object->getId()));
|
| 120 |
}
|
| 121 |
-
|
| 122 |
/**
|
| 123 |
* Unset the uenc param for redirection of blocks that have cached links.
|
| 124 |
* If we don't do this, links redirects to where user X came from when
|
| 125 |
* the block was cached. This also means that cached return links for
|
| 126 |
* ESI blocks return a visitor to the ESI URL, and we can't have that.
|
| 127 |
-
*
|
| 128 |
-
* @param Varien_Event_Observer $observer
|
| 129 |
*/
|
| 130 |
public function removeUencParam(Varien_Event_Observer $observer)
|
| 131 |
{
|
| 132 |
$request = $observer->getEvent()->getControllerAction()
|
| 133 |
->getRequest();
|
| 134 |
-
|
| 135 |
// To this day, the only used encoding type is PARAM_NAME_URL_ENCODED
|
| 136 |
$key = Mage_Core_Controller_Varien_Action::PARAM_NAME_URL_ENCODED;
|
| 137 |
if (($param = $request->getParam($key)) !== null) {
|
|
@@ -143,10 +143,10 @@ class Made_Cache_Model_Observer
|
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|
| 146 |
-
|
| 147 |
/**
|
| 148 |
* Private getter to determine if the profiler should be enabled
|
| 149 |
-
*
|
| 150 |
* @return bool
|
| 151 |
*/
|
| 152 |
protected function _getShouldProfile()
|
|
@@ -154,14 +154,14 @@ class Made_Cache_Model_Observer
|
|
| 154 |
if (is_null($this->_shouldProfile)) {
|
| 155 |
$this->_shouldProfile = (bool)Mage::getStoreConfig('cache/general/enable_profiler');
|
| 156 |
}
|
| 157 |
-
|
| 158 |
return $this->_shouldProfile;
|
| 159 |
}
|
| 160 |
-
|
| 161 |
/**
|
| 162 |
* Start counting the time of rendering an uncached block
|
| 163 |
-
*
|
| 164 |
-
* @param Varien_Event_Observer $observer
|
| 165 |
*/
|
| 166 |
public function profilerStart(Varien_Event_Observer $observer)
|
| 167 |
{
|
|
@@ -170,15 +170,15 @@ class Made_Cache_Model_Observer
|
|
| 170 |
$blockName = $observer->getEvent()
|
| 171 |
->getBlock()
|
| 172 |
->getNameInLayout();
|
| 173 |
-
|
| 174 |
Made_Cache_Model_Profiler::start($blockName);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
-
|
| 178 |
/**
|
| 179 |
* Stop counting the time of rendering an uncached block
|
| 180 |
*
|
| 181 |
-
* @param Varien_Event_Observer $observer
|
| 182 |
*/
|
| 183 |
public function profilerEnd(Varien_Event_Observer $observer)
|
| 184 |
{
|
|
@@ -187,8 +187,26 @@ class Made_Cache_Model_Observer
|
|
| 187 |
$blockName = $observer->getEvent()
|
| 188 |
->getBlock()
|
| 189 |
->getNameInLayout();
|
| 190 |
-
|
| 191 |
Made_Cache_Model_Profiler::end($blockName);
|
| 192 |
}
|
| 193 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
}
|
| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Manage tag-specific cache cleaning
|
| 4 |
+
*
|
| 5 |
* @package Made_Cache
|
| 6 |
* @author info@madepeople.se
|
| 7 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
| 9 |
class Made_Cache_Model_Observer
|
| 10 |
{
|
| 11 |
protected $_shouldProfile = null;
|
| 12 |
+
|
| 13 |
/**
|
| 14 |
* Observer that injects cache values into specific blocks, we want
|
| 15 |
* to do it like this instead of block rewrites to prevent other
|
| 16 |
* third-party modules from breaking
|
| 17 |
+
*
|
| 18 |
+
* @param Varien_Event_Observer $observer
|
| 19 |
*/
|
| 20 |
public function setupBlock(Varien_Event_Observer $observer)
|
| 21 |
{
|
| 29 |
Mage::dispatchEvent('made_cache_setup_block_before', array(
|
| 30 |
'block' => $block
|
| 31 |
));
|
| 32 |
+
|
| 33 |
switch (true) {
|
| 34 |
case $block instanceof Mage_Catalog_Block_Product_View:
|
| 35 |
Mage::getSingleton('cache/observer_catalog')
|
| 57 |
->applyCartSidebar($block);
|
| 58 |
break;
|
| 59 |
}
|
| 60 |
+
|
| 61 |
Mage::dispatchEvent('made_cache_setup_block_after', array(
|
| 62 |
'block' => $block
|
| 63 |
));
|
| 64 |
}
|
| 65 |
+
|
| 66 |
/**
|
| 67 |
* Clear product cache after a review is saved
|
| 68 |
+
*
|
| 69 |
+
* @param Varien_Event_Observer $observer
|
| 70 |
*/
|
| 71 |
public function reviewSaveAfter(Varien_Event_Observer $observer)
|
| 72 |
{
|
| 79 |
$_product->cleanCache();
|
| 80 |
}
|
| 81 |
}
|
| 82 |
+
|
| 83 |
/**
|
| 84 |
* CatalogRule invalidates cache on product save, so this must be cleaned
|
| 85 |
+
*
|
| 86 |
+
* @param Varien_Event_Observer $observer
|
| 87 |
*/
|
| 88 |
public function cleanCacheAfterProductSave(Varien_Event_Observer $observer)
|
| 89 |
{
|
| 90 |
$invalidatedTypes = Mage::app()->getCacheInstance()
|
| 91 |
->getInvalidatedTypes();
|
| 92 |
+
|
| 93 |
if (!is_array($invalidatedTypes)) {
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
+
|
| 97 |
$typesToCheck = array(
|
| 98 |
Mage_Core_Block_Abstract::CACHE_GROUP,
|
| 99 |
'full_page'
|
| 100 |
);
|
| 101 |
+
|
| 102 |
foreach ($typesToCheck as $type) {
|
| 103 |
if (isset($invalidatedTypes[Mage_Core_Block_Abstract::CACHE_GROUP])) {
|
| 104 |
Mage::app()->getCacheInstance()
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Clear quote cache on custom tag
|
| 112 |
+
*
|
| 113 |
+
* @param Varien_Event_Observer $observer
|
| 114 |
*/
|
| 115 |
public function clearQuoteCache(Varien_Event_Observer $observer)
|
| 116 |
{
|
| 118 |
$object = $observer->getEvent()->getQuote();
|
| 119 |
Mage::app()->cleanCache(array('SALES_QUOTE_' . $object->getId()));
|
| 120 |
}
|
| 121 |
+
|
| 122 |
/**
|
| 123 |
* Unset the uenc param for redirection of blocks that have cached links.
|
| 124 |
* If we don't do this, links redirects to where user X came from when
|
| 125 |
* the block was cached. This also means that cached return links for
|
| 126 |
* ESI blocks return a visitor to the ESI URL, and we can't have that.
|
| 127 |
+
*
|
| 128 |
+
* @param Varien_Event_Observer $observer
|
| 129 |
*/
|
| 130 |
public function removeUencParam(Varien_Event_Observer $observer)
|
| 131 |
{
|
| 132 |
$request = $observer->getEvent()->getControllerAction()
|
| 133 |
->getRequest();
|
| 134 |
+
|
| 135 |
// To this day, the only used encoding type is PARAM_NAME_URL_ENCODED
|
| 136 |
$key = Mage_Core_Controller_Varien_Action::PARAM_NAME_URL_ENCODED;
|
| 137 |
if (($param = $request->getParam($key)) !== null) {
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|
| 146 |
+
|
| 147 |
/**
|
| 148 |
* Private getter to determine if the profiler should be enabled
|
| 149 |
+
*
|
| 150 |
* @return bool
|
| 151 |
*/
|
| 152 |
protected function _getShouldProfile()
|
| 154 |
if (is_null($this->_shouldProfile)) {
|
| 155 |
$this->_shouldProfile = (bool)Mage::getStoreConfig('cache/general/enable_profiler');
|
| 156 |
}
|
| 157 |
+
|
| 158 |
return $this->_shouldProfile;
|
| 159 |
}
|
| 160 |
+
|
| 161 |
/**
|
| 162 |
* Start counting the time of rendering an uncached block
|
| 163 |
+
*
|
| 164 |
+
* @param Varien_Event_Observer $observer
|
| 165 |
*/
|
| 166 |
public function profilerStart(Varien_Event_Observer $observer)
|
| 167 |
{
|
| 170 |
$blockName = $observer->getEvent()
|
| 171 |
->getBlock()
|
| 172 |
->getNameInLayout();
|
| 173 |
+
|
| 174 |
Made_Cache_Model_Profiler::start($blockName);
|
| 175 |
}
|
| 176 |
}
|
| 177 |
+
|
| 178 |
/**
|
| 179 |
* Stop counting the time of rendering an uncached block
|
| 180 |
*
|
| 181 |
+
* @param Varien_Event_Observer $observer
|
| 182 |
*/
|
| 183 |
public function profilerEnd(Varien_Event_Observer $observer)
|
| 184 |
{
|
| 187 |
$blockName = $observer->getEvent()
|
| 188 |
->getBlock()
|
| 189 |
->getNameInLayout();
|
| 190 |
+
|
| 191 |
Made_Cache_Model_Profiler::end($blockName);
|
| 192 |
}
|
| 193 |
}
|
| 194 |
+
|
| 195 |
+
/**
|
| 196 |
+
* We have to clear the product cache on order save in case the stock qty
|
| 197 |
+
* for the affected product(s) turned the product out of stock
|
| 198 |
+
*
|
| 199 |
+
* @param Varien_Event_Observer $observer
|
| 200 |
+
*/
|
| 201 |
+
public function clearProductCacheAfterOrderPlace(Varien_Event_Observer $observer)
|
| 202 |
+
{
|
| 203 |
+
$order = $observer->getEvent()->getOrder();
|
| 204 |
+
|
| 205 |
+
$tags = array();
|
| 206 |
+
foreach ($order->getAllItems() as $item) {
|
| 207 |
+
$tags[] = Mage_Catalog_Model_Product::CACHE_TAG . '_' . $item->getProductId();
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
Mage::app()->cleanCache($tags);
|
| 211 |
+
}
|
| 212 |
}
|
app/code/community/Made/Cache/Model/Observer/Abstract.php
CHANGED
|
@@ -12,19 +12,26 @@ abstract class Made_Cache_Model_Observer_Abstract
|
|
| 12 |
* @see Mage_Core_Block_Abstract
|
| 13 |
* @return string
|
| 14 |
*/
|
| 15 |
-
protected function _getCacheKey(array $keyInfo)
|
| 16 |
{
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
$key = implode('|', $keyInfo);
|
| 19 |
$key = sha1($key);
|
| 20 |
return $key;
|
| 21 |
}
|
| 22 |
-
|
| 23 |
/**
|
| 24 |
* Get generic key array including handle etc, that all blocks use
|
| 25 |
-
*
|
| 26 |
* @param Mage_Core_Block_Abstract $block
|
| 27 |
-
* @return array
|
| 28 |
*/
|
| 29 |
protected function _getBasicKeys(Mage_Core_Block_Abstract $block)
|
| 30 |
{
|
|
@@ -32,9 +39,15 @@ abstract class Made_Cache_Model_Observer_Abstract
|
|
| 32 |
if (!is_array($keys)) {
|
| 33 |
$keys = array();
|
| 34 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 36 |
$keys[] = 'SSL_' . intval(!empty($_SERVER['HTTPS']) &&
|
| 37 |
$_SERVER['HTTPS'] !== 'off') . '_';
|
|
|
|
| 38 |
return $keys;
|
| 39 |
}
|
| 40 |
}
|
| 12 |
* @see Mage_Core_Block_Abstract
|
| 13 |
* @return string
|
| 14 |
*/
|
| 15 |
+
protected function _getCacheKey(array $keyInfo, Mage_Core_Block_Abstract $block = null)
|
| 16 |
{
|
| 17 |
+
if ($block !== null) {
|
| 18 |
+
$cacheKey = $block->getData('cache_key');
|
| 19 |
+
if (!empty($cacheKey)) {
|
| 20 |
+
$keyInfo = array($cacheKey);
|
| 21 |
+
} else {
|
| 22 |
+
$keyInfo = array_values($keyInfo);
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
$key = implode('|', $keyInfo);
|
| 26 |
$key = sha1($key);
|
| 27 |
return $key;
|
| 28 |
}
|
| 29 |
+
|
| 30 |
/**
|
| 31 |
* Get generic key array including handle etc, that all blocks use
|
| 32 |
+
*
|
| 33 |
* @param Mage_Core_Block_Abstract $block
|
| 34 |
+
* @return array
|
| 35 |
*/
|
| 36 |
protected function _getBasicKeys(Mage_Core_Block_Abstract $block)
|
| 37 |
{
|
| 39 |
if (!is_array($keys)) {
|
| 40 |
$keys = array();
|
| 41 |
}
|
| 42 |
+
|
| 43 |
+
$keys[] = Mage::getSingleton('customer/session')->getCustomer()
|
| 44 |
+
->getGroupId();
|
| 45 |
+
|
| 46 |
+
$keys[] = Mage::app()->getStore()->getCurrentCurrencyCode();
|
| 47 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 48 |
$keys[] = 'SSL_' . intval(!empty($_SERVER['HTTPS']) &&
|
| 49 |
$_SERVER['HTTPS'] !== 'off') . '_';
|
| 50 |
+
|
| 51 |
return $keys;
|
| 52 |
}
|
| 53 |
}
|
app/code/community/Made/Cache/Model/Observer/Catalog.php
CHANGED
|
@@ -52,7 +52,7 @@ class Made_Cache_Model_Observer_Catalog
|
|
| 52 |
->setProductClassId($_product->getTaxClassId())
|
| 53 |
)
|
| 54 |
));
|
| 55 |
-
$block->setData('cache_key', $this->_getCacheKey($keys));
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
|
@@ -157,6 +157,6 @@ class Made_Cache_Model_Observer_Catalog
|
|
| 157 |
$_taxRateRequest->getCustomerClassId(),
|
| 158 |
Mage::registry('current_tag')
|
| 159 |
));
|
| 160 |
-
$block->setData('cache_key', $this->_getCacheKey($keys));
|
| 161 |
}
|
| 162 |
}
|
| 52 |
->setProductClassId($_product->getTaxClassId())
|
| 53 |
)
|
| 54 |
));
|
| 55 |
+
$block->setData('cache_key', $this->_getCacheKey($keys, $block));
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 157 |
$_taxRateRequest->getCustomerClassId(),
|
| 158 |
Mage::registry('current_tag')
|
| 159 |
));
|
| 160 |
+
$block->setData('cache_key', $this->_getCacheKey($keys, $block));
|
| 161 |
}
|
| 162 |
}
|
app/code/community/Made/Cache/Model/Observer/Checkout.php
CHANGED
|
@@ -42,6 +42,6 @@ class Made_Cache_Model_Observer_Checkout
|
|
| 42 |
$keys = $this->_getBasicKeys($block);
|
| 43 |
$keys[] = $this->_getQuoteId($block);
|
| 44 |
|
| 45 |
-
$block->setData('cache_key', $this->_getCacheKey($keys));
|
| 46 |
}
|
| 47 |
}
|
| 42 |
$keys = $this->_getBasicKeys($block);
|
| 43 |
$keys[] = $this->_getQuoteId($block);
|
| 44 |
|
| 45 |
+
$block->setData('cache_key', $this->_getCacheKey($keys, $block));
|
| 46 |
}
|
| 47 |
}
|
app/code/community/Made/Cache/Model/Observer/Cms.php
CHANGED
|
@@ -30,10 +30,9 @@ class Made_Cache_Model_Observer_Cms
|
|
| 30 |
|
| 31 |
// Set cache keys
|
| 32 |
$keys = $this->_getBasicKeys($block);
|
| 33 |
-
|
| 34 |
$keys[] = $block->getPage()->getId();
|
| 35 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 36 |
-
$block->setData('cache_key', $this->_getCacheKey($keys));
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
|
@@ -84,6 +83,6 @@ class Made_Cache_Model_Observer_Cms
|
|
| 84 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 85 |
}
|
| 86 |
}
|
| 87 |
-
$block->setData('cache_key', $this->_getCacheKey($keys));
|
| 88 |
}
|
| 89 |
}
|
| 30 |
|
| 31 |
// Set cache keys
|
| 32 |
$keys = $this->_getBasicKeys($block);
|
|
|
|
| 33 |
$keys[] = $block->getPage()->getId();
|
| 34 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 35 |
+
$block->setData('cache_key', $this->_getCacheKey($keys, $block));
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 83 |
$keys[] = $block->getLayout()->getUpdate()->getCacheId();
|
| 84 |
}
|
| 85 |
}
|
| 86 |
+
$block->setData('cache_key', $this->_getCacheKey($keys, $block));
|
| 87 |
}
|
| 88 |
}
|
app/code/community/Made/Cache/controllers/VarnishController.php
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
/**
|
| 3 |
* Custom action for use together with Phoenix_VarnishCache to allow
|
| 4 |
* ESI block caching
|
| 5 |
-
*
|
| 6 |
* @package Made_Cache
|
| 7 |
* @author info@madepeople.se
|
| 8 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
|
@@ -17,22 +17,22 @@ class Made_Cache_VarnishController extends Mage_Core_Controller_Front_Action
|
|
| 17 |
$layoutHandles = explode(',', base64_decode($this->getRequest()->getParam('layout')));
|
| 18 |
$blockName = base64_decode($this->getRequest()->getParam('block'));
|
| 19 |
$misc = unserialize(base64_decode($this->getRequest()->getParam('misc')));
|
| 20 |
-
|
| 21 |
if (is_array($misc)) {
|
| 22 |
if (isset($misc['product'])) {
|
| 23 |
$product = Mage::getModel('catalog/product')->load($misc['product']);
|
| 24 |
Mage::register('product', $product);
|
| 25 |
}
|
| 26 |
}
|
| 27 |
-
|
| 28 |
$layout = $this->getLayout();
|
| 29 |
$update = $layout->getUpdate();
|
| 30 |
$update->load($layoutHandles);
|
| 31 |
-
|
| 32 |
$layout->generateXml();
|
| 33 |
$blockNodes = $layout->getNode()
|
| 34 |
->xpath('//*[@name="'.$blockName.'"]');
|
| 35 |
-
|
| 36 |
if (!empty($blockNodes)) {
|
| 37 |
foreach ($blockNodes as $node) {
|
| 38 |
$layout->generateBlocks($node, true);
|
|
@@ -42,10 +42,10 @@ class Made_Cache_VarnishController extends Mage_Core_Controller_Front_Action
|
|
| 42 |
$this->getResponse()->setBody($block->toHtml());
|
| 43 |
}
|
| 44 |
}
|
| 45 |
-
|
| 46 |
/**
|
| 47 |
* Render all messages from currently used session namespaces
|
| 48 |
-
*
|
| 49 |
* @return void
|
| 50 |
*/
|
| 51 |
public function messagesAction()
|
|
@@ -53,13 +53,13 @@ class Made_Cache_VarnishController extends Mage_Core_Controller_Front_Action
|
|
| 53 |
if (empty($_SESSION)) {
|
| 54 |
return;
|
| 55 |
}
|
| 56 |
-
|
| 57 |
$messagesBlock = $this->getLayout()
|
| 58 |
->createBlock('core/messages', 'messages')
|
| 59 |
->setBypassVarnish(true);
|
| 60 |
-
|
| 61 |
foreach ($_SESSION as $contents) {
|
| 62 |
-
if (isset($contents['messages']) &&
|
| 63 |
$contents['messages'] instanceof Mage_Core_Model_Message_Collection) {
|
| 64 |
if (!$contents['messages']->count()) {
|
| 65 |
continue;
|
|
@@ -68,21 +68,24 @@ class Made_Cache_VarnishController extends Mage_Core_Controller_Front_Action
|
|
| 68 |
$contents['messages']->clear();
|
| 69 |
}
|
| 70 |
}
|
| 71 |
-
|
| 72 |
if (!$messagesBlock->getMessageCollection()->count()) {
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
-
|
| 76 |
$this->getResponse()
|
| 77 |
->setBody($messagesBlock->toHtml());
|
| 78 |
}
|
| 79 |
-
|
| 80 |
/**
|
| 81 |
* Simply make sure that the session is valid
|
| 82 |
-
*
|
| 83 |
-
* @see Made_Cache_Block_Footer
|
| 84 |
*/
|
| 85 |
public function cookieAction()
|
| 86 |
{
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
-
}
|
| 2 |
/**
|
| 3 |
* Custom action for use together with Phoenix_VarnishCache to allow
|
| 4 |
* ESI block caching
|
| 5 |
+
*
|
| 6 |
* @package Made_Cache
|
| 7 |
* @author info@madepeople.se
|
| 8 |
* @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
|
| 17 |
$layoutHandles = explode(',', base64_decode($this->getRequest()->getParam('layout')));
|
| 18 |
$blockName = base64_decode($this->getRequest()->getParam('block'));
|
| 19 |
$misc = unserialize(base64_decode($this->getRequest()->getParam('misc')));
|
| 20 |
+
|
| 21 |
if (is_array($misc)) {
|
| 22 |
if (isset($misc['product'])) {
|
| 23 |
$product = Mage::getModel('catalog/product')->load($misc['product']);
|
| 24 |
Mage::register('product', $product);
|
| 25 |
}
|
| 26 |
}
|
| 27 |
+
|
| 28 |
$layout = $this->getLayout();
|
| 29 |
$update = $layout->getUpdate();
|
| 30 |
$update->load($layoutHandles);
|
| 31 |
+
|
| 32 |
$layout->generateXml();
|
| 33 |
$blockNodes = $layout->getNode()
|
| 34 |
->xpath('//*[@name="'.$blockName.'"]');
|
| 35 |
+
|
| 36 |
if (!empty($blockNodes)) {
|
| 37 |
foreach ($blockNodes as $node) {
|
| 38 |
$layout->generateBlocks($node, true);
|
| 42 |
$this->getResponse()->setBody($block->toHtml());
|
| 43 |
}
|
| 44 |
}
|
| 45 |
+
|
| 46 |
/**
|
| 47 |
* Render all messages from currently used session namespaces
|
| 48 |
+
*
|
| 49 |
* @return void
|
| 50 |
*/
|
| 51 |
public function messagesAction()
|
| 53 |
if (empty($_SESSION)) {
|
| 54 |
return;
|
| 55 |
}
|
| 56 |
+
|
| 57 |
$messagesBlock = $this->getLayout()
|
| 58 |
->createBlock('core/messages', 'messages')
|
| 59 |
->setBypassVarnish(true);
|
| 60 |
+
|
| 61 |
foreach ($_SESSION as $contents) {
|
| 62 |
+
if (isset($contents['messages']) &&
|
| 63 |
$contents['messages'] instanceof Mage_Core_Model_Message_Collection) {
|
| 64 |
if (!$contents['messages']->count()) {
|
| 65 |
continue;
|
| 68 |
$contents['messages']->clear();
|
| 69 |
}
|
| 70 |
}
|
| 71 |
+
|
| 72 |
if (!$messagesBlock->getMessageCollection()->count()) {
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
+
|
| 76 |
$this->getResponse()
|
| 77 |
->setBody($messagesBlock->toHtml());
|
| 78 |
}
|
| 79 |
+
|
| 80 |
/**
|
| 81 |
* Simply make sure that the session is valid
|
| 82 |
+
*
|
| 83 |
+
* @see Made_Cache_Block_Footer
|
| 84 |
*/
|
| 85 |
public function cookieAction()
|
| 86 |
{
|
| 87 |
+
// IE needs this, otherwise it won't accept cookies from an AJAX request
|
| 88 |
+
$this->getResponse()
|
| 89 |
+
->setHeader('P3P', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"', true);
|
| 90 |
}
|
| 91 |
+
}
|
app/code/community/Made/Cache/etc/config.xml
CHANGED
|
@@ -9,7 +9,7 @@
|
|
| 9 |
<config>
|
| 10 |
<modules>
|
| 11 |
<Made_Cache>
|
| 12 |
-
<version>1.4.
|
| 13 |
</Made_Cache>
|
| 14 |
</modules>
|
| 15 |
<global>
|
|
@@ -77,13 +77,22 @@
|
|
| 77 |
</core_block_abstract_to_html_before>
|
| 78 |
<sales_quote_save_after>
|
| 79 |
<observers>
|
| 80 |
-
<
|
| 81 |
<type>singleton</type>
|
| 82 |
<class>cache/observer</class>
|
| 83 |
<method>clearQuoteCache</method>
|
| 84 |
-
</
|
| 85 |
</observers>
|
| 86 |
</sales_quote_save_after>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
</events>
|
| 88 |
</global>
|
| 89 |
<frontend>
|
| 9 |
<config>
|
| 10 |
<modules>
|
| 11 |
<Made_Cache>
|
| 12 |
+
<version>1.4.4</version>
|
| 13 |
</Made_Cache>
|
| 14 |
</modules>
|
| 15 |
<global>
|
| 77 |
</core_block_abstract_to_html_before>
|
| 78 |
<sales_quote_save_after>
|
| 79 |
<observers>
|
| 80 |
+
<cache_quote_save_after>
|
| 81 |
<type>singleton</type>
|
| 82 |
<class>cache/observer</class>
|
| 83 |
<method>clearQuoteCache</method>
|
| 84 |
+
</cache_quote_save_after>
|
| 85 |
</observers>
|
| 86 |
</sales_quote_save_after>
|
| 87 |
+
<sales_order_place_after>
|
| 88 |
+
<observers>
|
| 89 |
+
<cache_order_place_after>
|
| 90 |
+
<type>singleton</type>
|
| 91 |
+
<class>cache/observer</class>
|
| 92 |
+
<method>clearProductCacheAfterOrderPlace</method>
|
| 93 |
+
</cache_order_place_after>
|
| 94 |
+
</observers>
|
| 95 |
+
</sales_order_place_after>
|
| 96 |
</events>
|
| 97 |
</global>
|
| 98 |
<frontend>
|
app/code/community/Made/Cache/varnish/magento.vcl
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import std;
|
| 2 |
+
|
| 3 |
+
backend default {
|
| 4 |
+
.host = "127.0.0.1";
|
| 5 |
+
.port = "9000";
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
# The admin backend needs longer timeout values
|
| 9 |
+
backend admin {
|
| 10 |
+
.host = "127.0.0.1";
|
| 11 |
+
.port = "9000";
|
| 12 |
+
.first_byte_timeout = 18000s;
|
| 13 |
+
.between_bytes_timeout = 18000s;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Add additional (ie webserver) IPs here that should be able to purge cache
|
| 17 |
+
acl purge {
|
| 18 |
+
"localhost";
|
| 19 |
+
"127.0.0.1";
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
sub vcl_recv {
|
| 23 |
+
# Ban specific object in the cache
|
| 24 |
+
if (req.request == "PURGE") {
|
| 25 |
+
if (!client.ip ~ purge) {
|
| 26 |
+
error 405 "Not allowed.";
|
| 27 |
+
}
|
| 28 |
+
return (lookup);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# Ban something
|
| 32 |
+
if (req.request == "BAN") {
|
| 33 |
+
# Same ACL check as above:
|
| 34 |
+
if (!client.ip ~ purge) {
|
| 35 |
+
error 405 "Not allowed.";
|
| 36 |
+
}
|
| 37 |
+
if (req.http.X-Ban-String) {
|
| 38 |
+
ban(req.http.X-Ban-String);
|
| 39 |
+
|
| 40 |
+
# Throw a synthetic page so the
|
| 41 |
+
# request won't go to the backend.
|
| 42 |
+
error 200 "Ban added";
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
error 400 "Bad request.";
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Flush the whole cache
|
| 49 |
+
if (req.request == "FLUSH") {
|
| 50 |
+
if (!client.ip ~ purge) {
|
| 51 |
+
error 405 "Not allowed.";
|
| 52 |
+
}
|
| 53 |
+
# If there are multiple vhosts we only want to clear all cache for
|
| 54 |
+
# the one issuing the request
|
| 55 |
+
ban("req.url ~ .*");
|
| 56 |
+
error 200 "Flushed";
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# Refresh specific object
|
| 60 |
+
if (req.request == "REFRESH") {
|
| 61 |
+
if (!client.ip ~ purge) {
|
| 62 |
+
error 405 "Not allowed.";
|
| 63 |
+
}
|
| 64 |
+
set req.request = "GET";
|
| 65 |
+
set req.hash_always_miss = true;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
sub vcl_recv {
|
| 70 |
+
# Switch to the admin backend
|
| 71 |
+
if (req.http.Cookie ~ "adminhtml=") {
|
| 72 |
+
set req.backend = admin;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Keep track of logged in users
|
| 76 |
+
if (req.http.Cookie ~ "frontend=") {
|
| 77 |
+
set req.http.X-Session-UUID =
|
| 78 |
+
regsub(req.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1");
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
# Pass anything other than GET and HEAD directly.
|
| 82 |
+
if (req.request != "GET" && req.request != "HEAD") {
|
| 83 |
+
/* We only deal with GET and HEAD by default */
|
| 84 |
+
return (pass);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
# Normalize Aceept-Encoding header to reduce vary
|
| 88 |
+
# http://varnish.projects.linpro.no/wiki/FAQ/Compression
|
| 89 |
+
if (req.http.Accept-Encoding) {
|
| 90 |
+
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
|
| 91 |
+
# No point in compressing these
|
| 92 |
+
remove req.http.Accept-Encoding;
|
| 93 |
+
} elsif (req.http.Accept-Encoding ~ "gzip") {
|
| 94 |
+
set req.http.Accept-Encoding = "gzip";
|
| 95 |
+
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
|
| 96 |
+
set req.http.Accept-Encoding = "deflate";
|
| 97 |
+
} else {
|
| 98 |
+
# unkown algorithm
|
| 99 |
+
remove req.http.Accept-Encoding;
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
return (lookup);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
sub vcl_hash {
|
| 107 |
+
# ESI request
|
| 108 |
+
if (req.url ~ "/madecache/varnish/(esi|messages)") {
|
| 109 |
+
hash_data(regsub(req.url, "(/hash/[^\/]+/).*", "\1"));
|
| 110 |
+
|
| 111 |
+
# Logged in user, cache on UUID level
|
| 112 |
+
if (req.http.X-Session-UUID) {
|
| 113 |
+
hash_data(req.http.X-Session-UUID);
|
| 114 |
+
}
|
| 115 |
+
} else {
|
| 116 |
+
hash_data(req.url);
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
# Also consider the host name for caching (multi-site with different themes etc)
|
| 120 |
+
if (req.http.host) {
|
| 121 |
+
hash_data(req.http.host);
|
| 122 |
+
} else {
|
| 123 |
+
hash_data(server.ip);
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
return (hash);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
sub vcl_hit {
|
| 130 |
+
if (req.request == "PURGE") {
|
| 131 |
+
purge;
|
| 132 |
+
error 200 "Purged";
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
sub vcl_miss {
|
| 137 |
+
if (req.request == "PURGE") {
|
| 138 |
+
purge;
|
| 139 |
+
error 404 "Not in cache";
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
# Called when an object is fetched from the backend
|
| 144 |
+
sub vcl_fetch {
|
| 145 |
+
# Pass the cookie requests directly to the backend, without caching
|
| 146 |
+
if (req.url ~ "/madecache/varnish/cookie") {
|
| 147 |
+
return (hit_for_pass);
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
if (beresp.status == 200 || beresp.status == 301 || beresp.status == 404) {
|
| 151 |
+
if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "text/xml") {
|
| 152 |
+
set beresp.do_esi = true;
|
| 153 |
+
} else {
|
| 154 |
+
# TTL for static content
|
| 155 |
+
set beresp.ttl = 1w;
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
# Don't cache expire headers, we maintain those differently
|
| 159 |
+
unset beresp.http.expires;
|
| 160 |
+
|
| 161 |
+
# Caching the cookie header would make multiple clients share session
|
| 162 |
+
set req.http.tempCookie = beresp.http.Set-Cookie;
|
| 163 |
+
|
| 164 |
+
# Cache (if positive TTL)
|
| 165 |
+
return (deliver);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
# Don't cache
|
| 169 |
+
return (hit_for_pass);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
sub vcl_deliver {
|
| 173 |
+
# To debug if it's a hit or a miss
|
| 174 |
+
set resp.http.X-Cache-Hits = obj.hits;
|
| 175 |
+
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
|
| 176 |
+
|
| 177 |
+
if (req.http.tempCookie) {
|
| 178 |
+
# Version of https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching
|
| 179 |
+
set resp.http.age = "0";
|
| 180 |
+
} else {
|
| 181 |
+
# Probably cached deliver, we don't need to keep the Set-Cookie header
|
| 182 |
+
unset resp.http.Set-Cookie;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
return (deliver);
|
| 186 |
+
}
|
app/code/community/Made/Cache/varnish/magento_signed_cookies.vcl
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Implements signed cookie checking with the official Varnish `digest` and `var`
|
| 2 |
+
# vmods. The idea is that Magento sends the signed cookie salt and expiry time
|
| 3 |
+
# to varnish which are then stored for consecutive requests and cookie
|
| 4 |
+
# validation. If the hash check fails or the timestamp has been passed, send
|
| 5 |
+
# the client to the backend for revalidation. This timeout can be shorter than
|
| 6 |
+
# the PHP session timeout, as we emulate a paywall behaviour.
|
| 7 |
+
C{
|
| 8 |
+
#include <sys/types.h>
|
| 9 |
+
#include <time.h>
|
| 10 |
+
}C
|
| 11 |
+
|
| 12 |
+
import digest;
|
| 13 |
+
import var;
|
| 14 |
+
|
| 15 |
+
sub vcl_recv {
|
| 16 |
+
if (var.global_get("cookie_salt") == "") {
|
| 17 |
+
# No cookie signing salt has been set, pass directly to the backend
|
| 18 |
+
return (pass);
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
if (digest.hmac_sha1(var.global_get("cookie_salt"), regsub(req.http.Cookie, ".*cookie_expiry=([^;]+).*", "\1")) !=
|
| 22 |
+
regsub(req.http.Cookie, ".*cookie_hash=([^;]+).*", "\1")) {
|
| 23 |
+
# The signed cookie has been tampered with - send to backend
|
| 24 |
+
return (pass);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# If the signed cookie has expired we send the client to the backend
|
| 28 |
+
var.set_int("current_cookie_expire", std.integer(regsub(req.http.Cookie, ".*cookie_expiry=([^;]+).*", "\1"), 0));
|
| 29 |
+
C{
|
| 30 |
+
time_t current_cookie_expire = (time_t)Vmod_Func_var.get_int(sp, "current_cookie_expire");
|
| 31 |
+
time_t current_time = time(NULL);
|
| 32 |
+
|
| 33 |
+
if (current_time > current_cookie_expire) {
|
| 34 |
+
VRT_done(sp, VCL_RET_PASS);
|
| 35 |
+
}
|
| 36 |
+
}C
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
sub vcl_fetch {
|
| 40 |
+
if (beresp.http.X-Cookie-Salt && beresp.http.X-Cookie-Expiry) {
|
| 41 |
+
var.global_set("cookie_salt", beresp.http.X-Cookie-Salt);
|
| 42 |
+
var.global_set("cookie_expiry", beresp.http.X-Cookie-Expiry);
|
| 43 |
+
|
| 44 |
+
unset beresp.http.X-Cookie-Salt;
|
| 45 |
+
unset beresp.http.X-Cookie-Expiry;
|
| 46 |
+
}
|
| 47 |
+
}
|
app/design/frontend/base/default/layout/madecache.xml
CHANGED
|
@@ -20,7 +20,14 @@
|
|
| 20 |
<name>before_body_end</name>
|
| 21 |
<name>product.info</name>
|
| 22 |
<name>product_list</name>
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
<!--
|
| 26 |
People want to cache this one, but it works badly if you use
|
| 20 |
<name>before_body_end</name>
|
| 21 |
<name>product.info</name>
|
| 22 |
<name>product_list</name>
|
| 23 |
+
|
| 24 |
+
<!--
|
| 25 |
+
Cache the cart sidebar with a 7200 second lifetime. Also
|
| 26 |
+
force the cache key to be "cart_sidebar", since the cart
|
| 27 |
+
should always render the same regardless of layout, URL
|
| 28 |
+
etc. If this isn't true in your case, remove it!
|
| 29 |
+
-->
|
| 30 |
+
<name lifetime="7200" key="cart_sidebar">cart_sidebar</name>
|
| 31 |
|
| 32 |
<!--
|
| 33 |
People want to cache this one, but it works badly if you use
|
package.xml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
<?xml version="1.0"?>
|
| 2 |
<package>
|
| 3 |
<name>Made_Cache</name>
|
| 4 |
-
<version>1.4.
|
| 5 |
<stability>stable</stability>
|
| 6 |
<license>4-clause BSD License</license>
|
| 7 |
<channel>community</channel>
|
|
@@ -18,9 +18,9 @@ ESI is supported in conjunction with Phoenix_VarnishCache, and allows for super-
|
|
| 18 |
A good block cache is vital for scaling a site, be sure to implement it before residing to full page cache.</description>
|
| 19 |
<notes>N/A</notes>
|
| 20 |
<authors><author><name>Jonathan Selander</name><user>jonathan_made</user><email>info@madepeople.se</email></author></authors>
|
| 21 |
-
<date>2013-
|
| 22 |
-
<time>
|
| 23 |
-
<contents><target name="magecommunity"><dir name="Made"><dir name="Cache"><dir name="Block"><dir name="Catalog"><dir name="Product"><dir name="List"><file name="Product.php" hash="6356fde9c5acf5f0a96851e780a691b1"/></dir><file name="List.php" hash="bb1e30501c95e83901de75bcb780d072"/></dir></dir><file name="Messages.php" hash="6d7eaed6a93c0deb6037ea36e88ec62f"/><file name="Profiler.php" hash="7d1fd856f08a1900b15a72ae46eb143d"/><dir name="Varnish"><file name="Footer.php" hash="4da9584f4cf4b70149b5c48f9657f668"/></dir></dir><dir name="Helper"><file name="Data.php" hash="6ea193010f6bf79c0c92f06d15e8d73b"/><file name="Varnish.php" hash="
|
| 24 |
<compatible/>
|
| 25 |
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
|
| 26 |
</package>
|
| 1 |
<?xml version="1.0"?>
|
| 2 |
<package>
|
| 3 |
<name>Made_Cache</name>
|
| 4 |
+
<version>1.4.4</version>
|
| 5 |
<stability>stable</stability>
|
| 6 |
<license>4-clause BSD License</license>
|
| 7 |
<channel>community</channel>
|
| 18 |
A good block cache is vital for scaling a site, be sure to implement it before residing to full page cache.</description>
|
| 19 |
<notes>N/A</notes>
|
| 20 |
<authors><author><name>Jonathan Selander</name><user>jonathan_made</user><email>info@madepeople.se</email></author></authors>
|
| 21 |
+
<date>2013-04-08</date>
|
| 22 |
+
<time>15:39:57</time>
|
| 23 |
+
<contents><target name="magecommunity"><dir name="Made"><dir name="Cache"><dir name="Block"><dir name="Catalog"><dir name="Product"><dir name="List"><file name="Product.php" hash="6356fde9c5acf5f0a96851e780a691b1"/></dir><file name="List.php" hash="bb1e30501c95e83901de75bcb780d072"/></dir></dir><file name="Messages.php" hash="6d7eaed6a93c0deb6037ea36e88ec62f"/><file name="Profiler.php" hash="7d1fd856f08a1900b15a72ae46eb143d"/><dir name="Varnish"><file name="Footer.php" hash="4da9584f4cf4b70149b5c48f9657f668"/></dir></dir><dir name="Helper"><file name="Data.php" hash="6ea193010f6bf79c0c92f06d15e8d73b"/><file name="Varnish.php" hash="d9037217c76ba600d16554de6b0f29be"/></dir><dir name="Model"><file name="Layout.php" hash="b6f87785635a108884cd42147e5a2212"/><dir name="Observer"><file name="Abstract.php" hash="1d440b111ce0632a55ced2d5d48c6921"/><file name="Catalog.php" hash="3c1ee2b600e7e318c01f1408c424e7d2"/><file name="Checkout.php" hash="b4e2e2079b497960083f6d77ada268da"/><file name="Cms.php" hash="bccdb30163fe823d622d4f26edd61974"/></dir><file name="Observer.php" hash="9c057fe976122d1a9e8515aeb341b64d"/><file name="Profiler.php" hash="888d3e38e212d872a4420a3556ca4b08"/><file name="VarnishObserver.php" hash="612bd3b8f48fe88ed3567096cbf439a4"/></dir><dir name="controllers"><file name="VarnishController.php" hash="4c73b9c5b91679120a649432459759c9"/></dir><dir name="etc"><file name="config.xml" hash="b402b973c7792368b3a3fb04baaf6411"/><file name="magento.vcl" hash="a247cd54d96f824bacacda6704ad5285"/><file name="system.xml" hash="4ffc1ded6a50a61aeaf932c5a3556a28"/></dir><dir name="varnish"><file name="magento.vcl" hash="e402cfb1b399ad7a32fa9e4e01a60beb"/><file name="magento_signed_cookies.vcl" hash="69ef065b030c19382afb5aa69f5cf4c6"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Made_Cache.xml" hash="4cf53cc9b4e525eb560f7fe1278d96bd"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="madecache.xml" hash="86840b6b89418e70ee134d21a058bc26"/></dir></dir></dir></dir></target></contents>
|
| 24 |
<compatible/>
|
| 25 |
<dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
|
| 26 |
</package>
|
