Made_Cache - Version 1.4.5

Version Notes

N/A

Download this release

Release Info

Developer Jonathan Selander
Extension Made_Cache
Version 1.4.5
Comparing to
See all releases


Code changes from version 1.4.4 to 1.4.5

app/code/community/Made/Cache/Block/Catalog/Product/List.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Use this for granular product list cache
4
- *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
7
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
@@ -12,9 +12,9 @@ class Made_Cache_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_L
12
  * For granular caching of product list blocks. Requires the markup
13
  * of a single product to be broken out of list.phtml into
14
  * catalog/product/list/product.phtml
15
- *
16
  * @param Mage_Catalog_Model_Product $product
17
- * @return type
18
  */
19
  public function getProductHtml($product)
20
  {
@@ -23,13 +23,12 @@ class Made_Cache_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_L
23
  Mage::unregister('product');
24
  }
25
 
26
- $name = 'prod_list_prod_' . $product->getId();
27
- $block = $this->getLayout()->createBlock('cache/catalog_product_list_product')
28
- ->setName($name)
29
- ->setCacheLifetime($this->getCacheLifetime())
30
- ->setTemplate('catalog/product/list/product.phtml')
31
- ->setProduct($product);
32
-
33
  $html = $block->toHtml();
34
  Mage::register('product', $viewedProduct);
35
  return $html;
1
  <?php
2
  /**
3
  * Use this for granular product list cache
4
+ *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
7
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
12
  * For granular caching of product list blocks. Requires the markup
13
  * of a single product to be broken out of list.phtml into
14
  * catalog/product/list/product.phtml
15
+ *
16
  * @param Mage_Catalog_Model_Product $product
17
+ * @return string
18
  */
19
  public function getProductHtml($product)
20
  {
23
  Mage::unregister('product');
24
  }
25
 
26
+ $block = $this->getLayout()
27
+ ->createBlock('cache/catalog_product_list_product')
28
+ ->setCacheLifetime($this->getCacheLifetime())
29
+ ->setTemplate('catalog/product/list/product.phtml')
30
+ ->setProduct($product);
31
+
 
32
  $html = $block->toHtml();
33
  Mage::register('product', $viewedProduct);
34
  return $html;
app/code/community/Made/Cache/Block/Catalog/Product/List/Product.php CHANGED
@@ -76,4 +76,14 @@ class Made_Cache_Block_Catalog_Product_List_Product
76
  $url = preg_replace('#/uenc/[^/]+/#', '/', $url);
77
  return $url;
78
  }
79
- }
 
 
 
 
 
 
 
 
 
 
76
  $url = preg_replace('#/uenc/[^/]+/#', '/', $url);
77
  return $url;
78
  }
79
+
80
+ /**
81
+ * We don't need the extra collection/toolbar overhead created by default
82
+ *
83
+ * @return \Made_Cache_Block_Catalog_Product_List_Product
84
+ */
85
+ protected function _beforeToHtml()
86
+ {
87
+ return $this;
88
+ }
89
+ }
app/code/community/Made/Cache/Helper/Varnish.php CHANGED
@@ -1,10 +1,10 @@
1
  <?php
2
  /**
3
  * Contains functions related to Varnish
4
- *
5
  * Methods from https://www.varnish-software.com/static/book/Cache_invalidation.html
6
  * are used in order to create a dynamic cache invalidation approach
7
- *
8
  * @package Made_Cache
9
  * @author info@madepeople.se
10
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
@@ -13,7 +13,7 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
13
  {
14
  /**
15
  * Determine if varnish is in front of Magento
16
- *
17
  * @return boolean
18
  */
19
  public function isInFront()
@@ -22,52 +22,52 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
22
  ->getRequest()
23
  ->getHeader('X-Varnish');
24
  }
25
-
26
  /**
27
  * Determine if Varnish functions should be used
28
- *
29
- * @return boolean
30
  */
31
  public function shouldUse()
32
  {
33
  return Mage::app()->useCache('varnish')
34
  && $this->isInFront();
35
  }
36
-
37
  /**
38
  * Returns an array of defined Varnish servers
39
- * @return type
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) {
47
  $server = trim($server);
48
-
49
  // Skip new lines
50
  if (empty($server)) {
51
  continue;
52
  }
53
-
54
  $servers[] = $server;
55
  }
56
-
57
  return $servers;
58
  }
59
 
60
  /**
61
- * Flush varnish cache by banning all content
62
  */
63
  public function flush()
64
  {
65
  return $this->_callVarnish('', 'FLUSH');
66
  }
67
-
68
  /**
69
  * Bans an URL or more from the Varnish cache
70
- *
71
  * @param string|array $urls
72
  */
73
  public function ban($urls)
@@ -83,31 +83,31 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
83
 
84
  /**
85
  * Purge specific object in varnish cache
86
- *
87
- * @param string|array $urls
88
  */
89
  public function purge($urls)
90
  {
91
  return $this->_callVarnish($urls, 'PURGE');
92
  }
93
-
94
  /**
95
  * Refresh specific content in varnish, might be more costly than PURGE
96
  * because backend is called, but also doesn't invalidate cache if the
97
  * backend is acting up
98
- *
99
- * @param string|array $urls
100
  */
101
  public function refresh($urls)
102
  {
103
  return $this->_callVarnish($urls, 'REFRESH');
104
  }
105
-
106
  /**
107
  * Send a message to all defined Varnish servers
108
- *
109
  * Uses code from magneto-varnish.
110
- *
111
  * @see https://github.com/madalinoprea/magneto-varnish/blob/master/code/Varnish/Helper/Data.php#L48
112
  * @param string|array $urls
113
  * @param string $type
@@ -117,11 +117,11 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
117
  {
118
  $urls = (array)$urls;
119
  $servers = $this->getServers();
120
-
121
  // Init curl handler
122
  $curlHandlers = array(); // keep references for clean up
123
  $mh = curl_multi_init();
124
-
125
  foreach ($servers as $varnishServer) {
126
  foreach ($urls as $url) {
127
  $varnishUrl = "http://" . $varnishServer . $url;
@@ -132,7 +132,7 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
132
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
133
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
134
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
135
-
136
  if (!empty($headers)) {
137
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
138
  }
@@ -146,29 +146,29 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
146
  do {
147
  curl_multi_exec($mh, $active);
148
  } while ($active);
149
-
150
  // Error handling and clean up
151
  $errors = array();
152
  foreach ($curlHandlers as $ch) {
153
  $info = curl_getinfo($ch);
154
-
155
  if (curl_errno($ch)) {
156
  $errors[] = "Cannot purge url {$info['url']} due to error" . curl_error($ch);
157
  } else if ($info['http_code'] != 200 && $info['http_code'] != 404) {
158
  $errors[] = "Cannot purge url {$info['url']}, http code: {$info['http_code']}";
159
  }
160
-
161
  curl_multi_remove_handle($mh, $ch);
162
  curl_close($ch);
163
  }
164
  curl_multi_close($mh);
165
-
166
  return $errors;
167
  }
168
-
169
  /**
170
  * Retreive an ESI tag for the specified URL
171
- *
172
  * @param string $url
173
  */
174
  public function getEsiTag($url)
@@ -176,12 +176,12 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
176
  $url = preg_replace('#^/#', '', $url);
177
  return '<esi:include src="' . Mage::getUrl($url) . '"/>';
178
  }
179
-
180
  /**
181
  * Return a hash of the block layout XML in the current configuration,
182
  * this is used to identify a unique rendering of the block as we cache
183
  * all ESI requests
184
- *
185
  * @param Mage_Core_Block_Abstract $block
186
  */
187
  public function getLayoutHash(Mage_Core_Block_Abstract $block)
@@ -205,10 +205,10 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
205
  $this->_callVarnish('/', 'BAN', array('X-Ban-String: req.url ~ madecache/varnish/(esi|messages) && req.http.X-Session-UUID == ' . $sessionId));
206
  }
207
  }
208
-
209
  /**
210
  * Retrieve the TTL for the current request
211
- *
212
  * @param type $request
213
  */
214
  public function getRequestTtl($request)
@@ -217,20 +217,20 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
217
  // Never cache POST
218
  return null;
219
  }
220
-
221
  $noCacheRoutes = Mage::getStoreConfig('cache/varnish/nocache_routes');
222
-
223
  if ($this->_matchRoutesAgainstRequest($noCacheRoutes, $request)
224
  || $this->_matchRoutesAgainstRequest('madecache/varnish/cookie', $request)) {
225
  return null;
226
  }
227
-
228
  // Messages should only be cached if they are empty
229
  if ($this->_matchRoutesAgainstRequest('madecache/varnish/messages', $request)
230
  && Mage::helper('cache')->responseHasMessages()) {
231
- return null;
232
  }
233
-
234
  if ($this->_matchRoutesAgainstRequest('madecache/varnish/esi', $request)) {
235
  // All ESI requests should have the same TTL as the session itself
236
  return intval(Mage::getStoreConfig('web/cookie/cookie_lifetime')) . 's';
@@ -238,35 +238,35 @@ class Made_Cache_Helper_Varnish extends Mage_Core_Helper_Abstract
238
 
239
  return Mage::getStoreConfig('cache/varnish/ttl');
240
  }
241
-
242
  /**
243
  * Match routes against the current request for cache exclusion
244
- *
245
  * @param array|string $routes
246
  * @param object $request
247
- * @return boolean
248
  */
249
  protected function _matchRoutesAgainstRequest($routes, $request)
250
  {
251
  if (!is_array($routes)) {
252
  $routes = explode("\n", $routes);
253
  }
254
-
255
  foreach ($routes as $key => $handle) {
256
  if (($handle = trim($handle)) === '') {
257
  continue;
258
  }
259
  $routes[$key] = $handle;
260
  }
261
-
262
  if (in_array($request->getModuleName(), $routes)
263
  || in_array($request->getModuleName() . '/' . $request->getControllerName(), $routes)
264
  || in_array($request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName(), $routes))
265
  {
266
  return true;
267
  }
268
-
269
  return false;
270
  }
271
-
272
  }
1
  <?php
2
  /**
3
  * Contains functions related to Varnish
4
+ *
5
  * Methods from https://www.varnish-software.com/static/book/Cache_invalidation.html
6
  * are used in order to create a dynamic cache invalidation approach
7
+ *
8
  * @package Made_Cache
9
  * @author info@madepeople.se
10
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
13
  {
14
  /**
15
  * Determine if varnish is in front of Magento
16
+ *
17
  * @return boolean
18
  */
19
  public function isInFront()
22
  ->getRequest()
23
  ->getHeader('X-Varnish');
24
  }
25
+
26
  /**
27
  * Determine if Varnish functions should be used
28
+ *
29
+ * @return boolean
30
  */
31
  public function shouldUse()
32
  {
33
  return Mage::app()->useCache('varnish')
34
  && $this->isInFront();
35
  }
36
+
37
  /**
38
  * Returns an array of defined Varnish servers
39
+ * @return type
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) {
47
  $server = trim($server);
48
+
49
  // Skip new lines
50
  if (empty($server)) {
51
  continue;
52
  }
53
+
54
  $servers[] = $server;
55
  }
56
+
57
  return $servers;
58
  }
59
 
60
  /**
61
+ * Flush varnish cache by banning all content
62
  */
63
  public function flush()
64
  {
65
  return $this->_callVarnish('', 'FLUSH');
66
  }
67
+
68
  /**
69
  * Bans an URL or more from the Varnish cache
70
+ *
71
  * @param string|array $urls
72
  */
73
  public function ban($urls)
83
 
84
  /**
85
  * Purge specific object in varnish cache
86
+ *
87
+ * @param string|array $urls
88
  */
89
  public function purge($urls)
90
  {
91
  return $this->_callVarnish($urls, 'PURGE');
92
  }
93
+
94
  /**
95
  * Refresh specific content in varnish, might be more costly than PURGE
96
  * because backend is called, but also doesn't invalidate cache if the
97
  * backend is acting up
98
+ *
99
+ * @param string|array $urls
100
  */
101
  public function refresh($urls)
102
  {
103
  return $this->_callVarnish($urls, 'REFRESH');
104
  }
105
+
106
  /**
107
  * Send a message to all defined Varnish servers
108
+ *
109
  * Uses code from magneto-varnish.
110
+ *
111
  * @see https://github.com/madalinoprea/magneto-varnish/blob/master/code/Varnish/Helper/Data.php#L48
112
  * @param string|array $urls
113
  * @param string $type
117
  {
118
  $urls = (array)$urls;
119
  $servers = $this->getServers();
120
+
121
  // Init curl handler
122
  $curlHandlers = array(); // keep references for clean up
123
  $mh = curl_multi_init();
124
+
125
  foreach ($servers as $varnishServer) {
126
  foreach ($urls as $url) {
127
  $varnishUrl = "http://" . $varnishServer . $url;
132
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
133
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
134
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
135
+
136
  if (!empty($headers)) {
137
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
138
  }
146
  do {
147
  curl_multi_exec($mh, $active);
148
  } while ($active);
149
+
150
  // Error handling and clean up
151
  $errors = array();
152
  foreach ($curlHandlers as $ch) {
153
  $info = curl_getinfo($ch);
154
+
155
  if (curl_errno($ch)) {
156
  $errors[] = "Cannot purge url {$info['url']} due to error" . curl_error($ch);
157
  } else if ($info['http_code'] != 200 && $info['http_code'] != 404) {
158
  $errors[] = "Cannot purge url {$info['url']}, http code: {$info['http_code']}";
159
  }
160
+
161
  curl_multi_remove_handle($mh, $ch);
162
  curl_close($ch);
163
  }
164
  curl_multi_close($mh);
165
+
166
  return $errors;
167
  }
168
+
169
  /**
170
  * Retreive an ESI tag for the specified URL
171
+ *
172
  * @param string $url
173
  */
174
  public function getEsiTag($url)
176
  $url = preg_replace('#^/#', '', $url);
177
  return '<esi:include src="' . Mage::getUrl($url) . '"/>';
178
  }
179
+
180
  /**
181
  * Return a hash of the block layout XML in the current configuration,
182
  * this is used to identify a unique rendering of the block as we cache
183
  * all ESI requests
184
+ *
185
  * @param Mage_Core_Block_Abstract $block
186
  */
187
  public function getLayoutHash(Mage_Core_Block_Abstract $block)
205
  $this->_callVarnish('/', 'BAN', array('X-Ban-String: req.url ~ madecache/varnish/(esi|messages) && req.http.X-Session-UUID == ' . $sessionId));
206
  }
207
  }
208
+
209
  /**
210
  * Retrieve the TTL for the current request
211
+ *
212
  * @param type $request
213
  */
214
  public function getRequestTtl($request)
217
  // Never cache POST
218
  return null;
219
  }
220
+
221
  $noCacheRoutes = Mage::getStoreConfig('cache/varnish/nocache_routes');
222
+
223
  if ($this->_matchRoutesAgainstRequest($noCacheRoutes, $request)
224
  || $this->_matchRoutesAgainstRequest('madecache/varnish/cookie', $request)) {
225
  return null;
226
  }
227
+
228
  // Messages should only be cached if they are empty
229
  if ($this->_matchRoutesAgainstRequest('madecache/varnish/messages', $request)
230
  && Mage::helper('cache')->responseHasMessages()) {
231
+ return null;
232
  }
233
+
234
  if ($this->_matchRoutesAgainstRequest('madecache/varnish/esi', $request)) {
235
  // All ESI requests should have the same TTL as the session itself
236
  return intval(Mage::getStoreConfig('web/cookie/cookie_lifetime')) . 's';
238
 
239
  return Mage::getStoreConfig('cache/varnish/ttl');
240
  }
241
+
242
  /**
243
  * Match routes against the current request for cache exclusion
244
+ *
245
  * @param array|string $routes
246
  * @param object $request
247
+ * @return boolean
248
  */
249
  protected function _matchRoutesAgainstRequest($routes, $request)
250
  {
251
  if (!is_array($routes)) {
252
  $routes = explode("\n", $routes);
253
  }
254
+
255
  foreach ($routes as $key => $handle) {
256
  if (($handle = trim($handle)) === '') {
257
  continue;
258
  }
259
  $routes[$key] = $handle;
260
  }
261
+
262
  if (in_array($request->getModuleName(), $routes)
263
  || in_array($request->getModuleName() . '/' . $request->getControllerName(), $routes)
264
  || in_array($request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName(), $routes))
265
  {
266
  return true;
267
  }
268
+
269
  return false;
270
  }
271
+
272
  }
app/code/community/Made/Cache/Model/Observer.php CHANGED
@@ -39,6 +39,10 @@ class Made_Cache_Model_Observer
39
  Mage::getSingleton('cache/observer_catalog')
40
  ->applyCategoryView($block);
41
  break;
 
 
 
 
42
  case $block instanceof Mage_Catalog_Block_Product_List:
43
  Mage::getSingleton('cache/observer_catalog')
44
  ->applyProductList($block);
@@ -70,39 +74,32 @@ class Made_Cache_Model_Observer
70
  */
71
  public function reviewSaveAfter(Varien_Event_Observer $observer)
72
  {
73
- $_object = $observer->getObject();
74
- $_productCollection = $_object->getProductCollection()
75
  ->addAttributeToFilter('rt.review_id',
76
- array('eq' => $_object->getId()));
77
 
78
- foreach ($_productCollection as $_product) {
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()
105
- ->cleanType($type);
106
  }
107
  }
108
  }
39
  Mage::getSingleton('cache/observer_catalog')
40
  ->applyCategoryView($block);
41
  break;
42
+ case $block instanceof Mage_Catalog_Block_Layer_View:
43
+ Mage::getSingleton('cache/observer_catalog')
44
+ ->applyCatalogLayerView($block);
45
+ break;
46
  case $block instanceof Mage_Catalog_Block_Product_List:
47
  Mage::getSingleton('cache/observer_catalog')
48
  ->applyProductList($block);
74
  */
75
  public function reviewSaveAfter(Varien_Event_Observer $observer)
76
  {
77
+ $object = $observer->getObject();
78
+ $productCollection = $object->getProductCollection()
79
  ->addAttributeToFilter('rt.review_id',
80
+ array('eq' => $object->getId()));
81
 
82
+ foreach ($productCollection as $product) {
83
+ $product->cleanCache();
84
  }
85
  }
86
 
87
  /**
88
+ * CatalogRule invalidates cache on product save, and we really don't
89
+ * want to clear everything which some blogposts suggest, so instead we
90
+ * just mark the block cache as valid again. Stupid? You decide
91
  *
92
  * @param Varien_Event_Observer $observer
93
  */
94
  public function cleanCacheAfterProductSave(Varien_Event_Observer $observer)
95
  {
96
+ $cacheInstance = Mage::app()->getCacheInstance();
97
+ $types = $cacheInstance->load(Mage_Core_Model_Cache::INVALIDATED_TYPES);
98
+ if ($types) {
99
+ $types = unserialize($types);
100
+ if (!empty($types[Mage_Core_Block_Abstract::CACHE_GROUP])) {
101
+ unset($types[Mage_Core_Block_Abstract::CACHE_GROUP]);
102
+ $cacheInstance->save(serialize($types), Mage_Core_Model_Cache::INVALIDATED_TYPES);
 
 
 
 
 
 
 
 
 
103
  }
104
  }
105
  }
app/code/community/Made/Cache/Model/Observer/Catalog.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Inject cache variables for catalog blocks
4
- *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
7
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
@@ -11,17 +11,17 @@ class Made_Cache_Model_Observer_Catalog
11
  {
12
  /**
13
  * Product view block, don't cache if rendered with cart item update
14
- *
15
  * @param Mage_Catalog_Block_Product_View $block
16
  */
17
  public function applyProductView(Mage_Catalog_Block_Product_View $block)
18
- {
19
  // The "messages" block is session-dependent, don't cache
20
  if (Mage::helper('cache')->responseHasMessages()) {
21
  $block->setData('cache_lifetime', null);
22
  return;
23
  }
24
-
25
  // Cart stuff is session-dependent
26
  $request = $block->getRequest();
27
  if ($request->getModuleName() == 'checkout'
@@ -36,7 +36,7 @@ class Made_Cache_Model_Observer_Catalog
36
  $tags = array(Mage_Catalog_Model_Product::CACHE_TAG . '_'
37
  . $block->getProduct()->getId());
38
  $block->setData('cache_tags', $tags);
39
-
40
  // Set cache keys
41
  $keys = $this->_getBasicKeys($block);
42
 
@@ -54,11 +54,11 @@ class Made_Cache_Model_Observer_Catalog
54
  ));
55
  $block->setData('cache_key', $this->_getCacheKey($keys, $block));
56
  }
57
-
58
  /**
59
  * Depending on block usage, the category id resides in different places
60
- *
61
- * @return int
62
  */
63
  protected function _getCategoryIdForProductList($block)
64
  {
@@ -69,13 +69,13 @@ class Made_Cache_Model_Observer_Catalog
69
  ->getId()
70
  ;
71
  }
72
-
73
  /**
74
  * Make sure that the category view doesn't cache when there are
75
  * messages in the session
76
- *
77
  * @param Mage_Catalog_Block_Category_View $block
78
- * @return type
79
  */
80
  public function applyCategoryView(Mage_Catalog_Block_Category_View $block)
81
  {
@@ -85,11 +85,11 @@ class Made_Cache_Model_Observer_Catalog
85
  return;
86
  }
87
  }
88
-
89
  /**
90
  * Product list cache, needs to clear on displayed products
91
- *
92
- * @param Mage_Catalog_Block_Product_List $block
93
  */
94
  public function applyProductList(Mage_Catalog_Block_Product_List $block)
95
  {
@@ -98,16 +98,16 @@ class Made_Cache_Model_Observer_Catalog
98
  $block->setData('cache_lifetime', null);
99
  return;
100
  }
101
-
102
  // Set cache tags
103
  $tags = array();
104
- $tags[] = Mage_Catalog_Model_Category::CACHE_TAG . '_' .
105
  $this->_getCategoryIdForProductList($block);
106
-
107
  // The toolbar needs to apply sort order etc
108
  $_toolbar = $block->getToolbarBlock();
109
  $productCollection = $block->getLoadedProductCollection();
110
-
111
  /**
112
  * @see Mage_Catalog_Block_Product_List_Toolbar::getCurrentOrder
113
  */
@@ -123,26 +123,26 @@ class Made_Cache_Model_Observer_Catalog
123
  if ($modes = $block->getModes()) {
124
  $_toolbar->setModes($modes);
125
  }
126
-
127
  $_toolbar->setCollection($productCollection);
128
-
129
  foreach ($productCollection as $_product) {
130
  $tags[] = Mage_Catalog_Model_Product::CACHE_TAG."_".$_product->getId();
131
  }
132
  $block->setData('cache_tags', $tags);
133
-
134
  // Set cache key
135
  $keys = $this->_getBasicKeys($block);
136
-
137
  $_taxRateRequest = Mage::getModel('tax/calculation')->getRateRequest();
138
  $_customer = Mage::getSingleton('customer/session')->getCustomer();
139
  $_categoryId = $this->_getCategoryIdForProductList($block);
140
-
141
  foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
142
  $value = Mage::helper('cache')->paramValueToCacheKey($value);
143
  $keys[] = $key . '_' . $value;
144
  }
145
-
146
  $keys = array_merge($keys, array(
147
  $_categoryId,
148
  $_toolbar->getCurrentOrder(),
@@ -159,4 +159,26 @@ class Made_Cache_Model_Observer_Catalog
159
  ));
160
  $block->setData('cache_key', $this->_getCacheKey($keys, $block));
161
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  }
1
  <?php
2
  /**
3
  * Inject cache variables for catalog blocks
4
+ *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
7
  * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
11
  {
12
  /**
13
  * Product view block, don't cache if rendered with cart item update
14
+ *
15
  * @param Mage_Catalog_Block_Product_View $block
16
  */
17
  public function applyProductView(Mage_Catalog_Block_Product_View $block)
18
+ {
19
  // The "messages" block is session-dependent, don't cache
20
  if (Mage::helper('cache')->responseHasMessages()) {
21
  $block->setData('cache_lifetime', null);
22
  return;
23
  }
24
+
25
  // Cart stuff is session-dependent
26
  $request = $block->getRequest();
27
  if ($request->getModuleName() == 'checkout'
36
  $tags = array(Mage_Catalog_Model_Product::CACHE_TAG . '_'
37
  . $block->getProduct()->getId());
38
  $block->setData('cache_tags', $tags);
39
+
40
  // Set cache keys
41
  $keys = $this->_getBasicKeys($block);
42
 
54
  ));
55
  $block->setData('cache_key', $this->_getCacheKey($keys, $block));
56
  }
57
+
58
  /**
59
  * Depending on block usage, the category id resides in different places
60
+ *
61
+ * @return int
62
  */
63
  protected function _getCategoryIdForProductList($block)
64
  {
69
  ->getId()
70
  ;
71
  }
72
+
73
  /**
74
  * Make sure that the category view doesn't cache when there are
75
  * messages in the session
76
+ *
77
  * @param Mage_Catalog_Block_Category_View $block
78
+ * @return type
79
  */
80
  public function applyCategoryView(Mage_Catalog_Block_Category_View $block)
81
  {
85
  return;
86
  }
87
  }
88
+
89
  /**
90
  * Product list cache, needs to clear on displayed products
91
+ *
92
+ * @param Mage_Catalog_Block_Product_List $block
93
  */
94
  public function applyProductList(Mage_Catalog_Block_Product_List $block)
95
  {
98
  $block->setData('cache_lifetime', null);
99
  return;
100
  }
101
+
102
  // Set cache tags
103
  $tags = array();
104
+ $tags[] = Mage_Catalog_Model_Category::CACHE_TAG . '_' .
105
  $this->_getCategoryIdForProductList($block);
106
+
107
  // The toolbar needs to apply sort order etc
108
  $_toolbar = $block->getToolbarBlock();
109
  $productCollection = $block->getLoadedProductCollection();
110
+
111
  /**
112
  * @see Mage_Catalog_Block_Product_List_Toolbar::getCurrentOrder
113
  */
123
  if ($modes = $block->getModes()) {
124
  $_toolbar->setModes($modes);
125
  }
126
+
127
  $_toolbar->setCollection($productCollection);
128
+
129
  foreach ($productCollection as $_product) {
130
  $tags[] = Mage_Catalog_Model_Product::CACHE_TAG."_".$_product->getId();
131
  }
132
  $block->setData('cache_tags', $tags);
133
+
134
  // Set cache key
135
  $keys = $this->_getBasicKeys($block);
136
+
137
  $_taxRateRequest = Mage::getModel('tax/calculation')->getRateRequest();
138
  $_customer = Mage::getSingleton('customer/session')->getCustomer();
139
  $_categoryId = $this->_getCategoryIdForProductList($block);
140
+
141
  foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
142
  $value = Mage::helper('cache')->paramValueToCacheKey($value);
143
  $keys[] = $key . '_' . $value;
144
  }
145
+
146
  $keys = array_merge($keys, array(
147
  $_categoryId,
148
  $_toolbar->getCurrentOrder(),
159
  ));
160
  $block->setData('cache_key', $this->_getCacheKey($keys, $block));
161
  }
162
+
163
+ /**
164
+ * Layered navigation block, clears on changed associated category ID
165
+ *
166
+ * @param Mage_Catalog_Block_Layer_View $block
167
+ */
168
+ public function applyCatalogLayerView(Mage_Catalog_Block_Layer_View $block)
169
+ {
170
+ // Set cache tags
171
+ $tags = $block->getLayer()->getStateTags();
172
+ $block->setData('cache_tags', $tags);
173
+
174
+ $keys = $this->_getBasicKeys($block);
175
+ $keys[] = $block->getLayer()->getStateKey();
176
+
177
+ foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
178
+ $value = Mage::helper('cache')->paramValueToCacheKey($value);
179
+ $keys[] = $key . '_' . $value;
180
+ }
181
+
182
+ $block->setData('cache_key', $this->_getCacheKey($keys));
183
+ }
184
  }
app/code/community/Made/Cache/Model/VarnishObserver.php CHANGED
@@ -142,31 +142,51 @@ class Made_Cache_Model_VarnishObserver
142
  }
143
 
144
  /**
145
- * Purge cache in Varnish including entity cache such as products,
146
- * categories and CMS pages by doing lookups in the rewrite table
147
- *
148
- * Uses code from magneto-varnish.
149
  *
150
- * @see https://github.com/madalinoprea/magneto-varnish/blob/master/code/Varnish/Model/Observer.php#L65
151
- * @param Varien_Event_Observer $observer
152
  */
153
- public function purge(Varien_Event_Observer $observer)
154
  {
155
  if (!Mage::helper('cache/varnish')->shouldUse()) {
156
  return;
157
  }
 
 
 
 
 
 
 
 
158
 
159
- $tags = $observer->getTags();
160
- if (empty($tags)) {
161
  $errors = Mage::helper('cache/varnish')->flush();
162
  if (!empty($errors)) {
163
- Mage::getSingleton('adminhtml/session')->addError("Varnish Purge failed");
164
  } else {
165
  Mage::getSingleton('adminhtml/session')->addSuccess("The Varnish cache storage has been flushed.");
166
  }
167
  return;
168
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
 
170
  $urls = array();
171
 
172
  // Compute the urls for affected entities
142
  }
143
 
144
  /**
145
+ * Flush everything currently in the Varnish cache
 
 
 
146
  *
147
+ * @param Varien_Event_Observer $observer
 
148
  */
149
+ public function flush(Varien_Event_Observer $observer)
150
  {
151
  if (!Mage::helper('cache/varnish')->shouldUse()) {
152
  return;
153
  }
154
+
155
+ $eventName = $observer->getEvent()->getName();
156
+ if ($eventName === 'adminhtml_cache_refresh_type') {
157
+ $flush = $observer->getEvent()->getType() === 'varnish';
158
+ } else {
159
+ $tags = $observer->getEvent()->getTags();
160
+ $flush = empty($tags);
161
+ }
162
 
163
+ if ($flush) {
 
164
  $errors = Mage::helper('cache/varnish')->flush();
165
  if (!empty($errors)) {
166
+ Mage::getSingleton('adminhtml/session')->addError("Varnish Purge failed: " . join(', ', $errors));
167
  } else {
168
  Mage::getSingleton('adminhtml/session')->addSuccess("The Varnish cache storage has been flushed.");
169
  }
170
  return;
171
  }
172
+ }
173
+
174
+ /**
175
+ * Purge cache in Varnish including entity cache such as products,
176
+ * categories and CMS pages by doing lookups in the rewrite table
177
+ *
178
+ * Uses code from magneto-varnish.
179
+ *
180
+ * @see https://github.com/madalinoprea/magneto-varnish/blob/master/code/Varnish/Model/Observer.php#L65
181
+ * @param Varien_Event_Observer $observer
182
+ */
183
+ public function purge(Varien_Event_Observer $observer)
184
+ {
185
+ if (!Mage::helper('cache/varnish')->shouldUse()) {
186
+ return;
187
+ }
188
 
189
+ $tags = $observer->getEvent()->getTags();
190
  $urls = array();
191
 
192
  // Compute the urls for affected entities
app/code/community/Made/Cache/etc/config.xml CHANGED
@@ -9,7 +9,7 @@
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
- <version>1.4.4</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
@@ -191,15 +191,33 @@
191
  </clean_cache_after_product_save>
192
  </observers>
193
  </controller_action_postdispatch_adminhtml_catalog_product_save>
 
 
 
 
 
 
 
 
 
194
  <adminhtml_cache_flush_all>
195
  <observers>
196
  <varnish_purge>
197
  <type>singleton</type>
198
  <class>cache/varnishObserver</class>
199
- <method>purge</method>
200
  </varnish_purge>
201
  </observers>
202
  </adminhtml_cache_flush_all>
 
 
 
 
 
 
 
 
 
203
  <review_save_after>
204
  <observers>
205
  <cache_review_save_after>
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
+ <version>1.4.5</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
191
  </clean_cache_after_product_save>
192
  </observers>
193
  </controller_action_postdispatch_adminhtml_catalog_product_save>
194
+ <adminhtml_cache_flush_system>
195
+ <observers>
196
+ <varnish_purge>
197
+ <type>singleton</type>
198
+ <class>cache/varnishObserver</class>
199
+ <method>flush</method>
200
+ </varnish_purge>
201
+ </observers>
202
+ </adminhtml_cache_flush_system>
203
  <adminhtml_cache_flush_all>
204
  <observers>
205
  <varnish_purge>
206
  <type>singleton</type>
207
  <class>cache/varnishObserver</class>
208
+ <method>flush</method>
209
  </varnish_purge>
210
  </observers>
211
  </adminhtml_cache_flush_all>
212
+ <adminhtml_cache_refresh_type>
213
+ <observers>
214
+ <varnish_purge>
215
+ <type>singleton</type>
216
+ <class>cache/varnishObserver</class>
217
+ <method>flush</method>
218
+ </varnish_purge>
219
+ </observers>
220
+ </adminhtml_cache_refresh_type>
221
  <review_save_after>
222
  <observers>
223
  <cache_review_save_after>
app/code/community/Made/Cache/etc/magento.vcl CHANGED
@@ -1,5 +1,10 @@
1
  import std;
2
 
 
 
 
 
 
3
  backend default {
4
  .host = "127.0.0.1";
5
  .port = "9000";
@@ -20,7 +25,7 @@ acl purge {
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.";
@@ -50,9 +55,7 @@ sub vcl_recv {
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
 
@@ -73,12 +76,30 @@ sub vcl_recv {
73
  # Keep track of logged in users
74
  if (req.http.Cookie ~ "frontend=") {
75
  set req.http.X-Session-UUID =
76
- regsub(req.http.Cookie, "^.*?frontend=([^;]*);*.*$", "\1");
77
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Pass anything other than GET and HEAD directly.
80
  if (req.request != "GET" && req.request != "HEAD") {
81
- /* We only deal with GET and HEAD by default */
82
  return (pass);
83
  }
84
 
@@ -102,12 +123,12 @@ sub vcl_recv {
102
  }
103
 
104
  sub vcl_hash {
105
- # ESI request
106
  if (req.url ~ "/madecache/varnish/(esi|messages)") {
107
  hash_data(regsub(req.url, "(/hash/[^\/]+/).*", "\1"));
108
 
109
  # Logged in user, cache on UUID level
110
- if (req.http.X-Session-UUID) {
111
  hash_data(req.http.X-Session-UUID);
112
  }
113
  } else {
@@ -142,6 +163,7 @@ sub vcl_miss {
142
  sub vcl_fetch {
143
  # Pass the cookie requests directly to the backend, without caching
144
  if (req.url ~ "/madecache/varnish/cookie") {
 
145
  return (hit_for_pass);
146
  }
147
 
@@ -159,9 +181,14 @@ sub vcl_fetch {
159
  # Don't cache expire headers, we maintain those differently
160
  unset beresp.http.expires;
161
 
 
 
 
162
  # Caching the cookie header would make multiple clients share session
163
- set req.http.tempCookie = beresp.http.Set-Cookie;
164
- unset beresp.http.Set-Cookie;
 
 
165
 
166
  # Cache (if positive TTL)
167
  return (deliver);
@@ -173,15 +200,11 @@ sub vcl_fetch {
173
 
174
  sub vcl_deliver {
175
  # To debug if it's a hit or a miss
176
- if (req.http.X-Made-Cache-Debug) {
177
- set resp.http.X-Cache-Hits = obj.hits;
178
- }
179
 
180
  if (req.http.tempCookie) {
181
- # We saved the cookie to give the user that cached the page a session
182
- set resp.http.Set-Cookie = req.http.tempCookie;
183
-
184
  # Version of https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching
 
185
  set resp.http.age = "0";
186
  }
187
 
1
  import std;
2
 
3
+ # Uncomment this is you've compiled the libvmod-curl extension and have
4
+ # CouchDB session management set up. Also, search for "curl" in this file
5
+ # and set the rest up
6
+ #import curl;
7
+
8
  backend default {
9
  .host = "127.0.0.1";
10
  .port = "9000";
25
  }
26
 
27
  sub vcl_recv {
28
+ # Purge specific object from the cache
29
  if (req.request == "PURGE") {
30
  if (!client.ip ~ purge) {
31
  error 405 "Not allowed.";
55
  if (!client.ip ~ purge) {
56
  error 405 "Not allowed.";
57
  }
58
+ ban("req.url ~ /");
 
 
59
  error 200 "Flushed";
60
  }
61
 
76
  # Keep track of logged in users
77
  if (req.http.Cookie ~ "frontend=") {
78
  set req.http.X-Session-UUID =
79
+ regsub(req.http.Cookie, ".*frontend=([^;]+).*", "\1");
80
+ }
81
+
82
+ # This is where we have to check for session validity. Needs the curl vmod
83
+ # to be installed and imported at the top of this file. If the session is
84
+ # invalid we pass the user to the backend. Your CouchDB URL has to be
85
+ # defined manually here, in the form:
86
+ #
87
+ # http://couchdb.url.or.ip:port/magento_session/_design/misc/_show/is_session_valid/SESSION_ID_FROM_REQUEST
88
+ #
89
+ # The following show function needs to be defined in CouchDB as well:
90
+ #
91
+ # https://gist.github.com/jonathanselander/1c71f413911116ecba11
92
+ #
93
+ #curl.fetch("http://127.0.0.1:5984/magento_session/_design/misc/_show/is_session_valid/" + req.http.X-Session-UUID);
94
+ #if (curl.body() != "true") {
95
+ # curl.free();
96
+ # return(pass);
97
+ #}
98
+ #curl.free();
99
 
100
  # Pass anything other than GET and HEAD directly.
101
  if (req.request != "GET" && req.request != "HEAD") {
102
+ # We only deal with GET and HEAD by default
103
  return (pass);
104
  }
105
 
123
  }
124
 
125
  sub vcl_hash {
126
+ # ESI Request
127
  if (req.url ~ "/madecache/varnish/(esi|messages)") {
128
  hash_data(regsub(req.url, "(/hash/[^\/]+/).*", "\1"));
129
 
130
  # Logged in user, cache on UUID level
131
+ if (req.http.X-Session-UUID && req.http.X-Session-UUID != "") {
132
  hash_data(req.http.X-Session-UUID);
133
  }
134
  } else {
163
  sub vcl_fetch {
164
  # Pass the cookie requests directly to the backend, without caching
165
  if (req.url ~ "/madecache/varnish/cookie") {
166
+ # Cache not to cache
167
  return (hit_for_pass);
168
  }
169
 
181
  # Don't cache expire headers, we maintain those differently
182
  unset beresp.http.expires;
183
 
184
+ # Hold down object variations by removing the referer header
185
+ unset beresp.http.referer;
186
+
187
  # Caching the cookie header would make multiple clients share session
188
+ if (beresp.ttl > 0s) {
189
+ set req.http.tempCookie = beresp.http.Set-Cookie;
190
+ unset beresp.http.Set-Cookie;
191
+ }
192
 
193
  # Cache (if positive TTL)
194
  return (deliver);
200
 
201
  sub vcl_deliver {
202
  # To debug if it's a hit or a miss
203
+ set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
 
 
204
 
205
  if (req.http.tempCookie) {
 
 
 
206
  # Version of https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching
207
+ set resp.http.Set-Cookie = req.http.tempCookie;
208
  set resp.http.age = "0";
209
  }
210
 
app/code/community/Made/Cache/etc/system.xml CHANGED
@@ -10,7 +10,6 @@
10
  <cache>
11
  <label>Cache</label>
12
  <tab>made</tab>
13
- <frontend_type>text</frontend_type>
14
  <sort_order>1000</sort_order>
15
  <show_in_default>1</show_in_default>
16
  <show_in_website>1</show_in_website>
@@ -18,7 +17,6 @@
18
  <groups>
19
  <general translate="label">
20
  <label>General Settings</label>
21
- <frontend_type>text</frontend_type>
22
  <sort_order>0</sort_order>
23
  <show_in_default>1</show_in_default>
24
  <show_in_website>1</show_in_website>
@@ -48,7 +46,6 @@
48
  </general>
49
  <varnish translate="label">
50
  <label>Varnish Settings (Enabled in Cache Management)</label>
51
- <frontend_type>text</frontend_type>
52
  <sort_order>1</sort_order>
53
  <show_in_default>1</show_in_default>
54
  <show_in_website>1</show_in_website>
@@ -61,7 +58,7 @@
61
  <sort_order>1</sort_order>
62
  <show_in_default>1</show_in_default>
63
  <show_in_website>1</show_in_website>
64
- <show_in_store>1</show_in_store>
65
  </ttl>
66
  <nocache_routes>
67
  <label>Routes to exclude from cache</label>
@@ -70,7 +67,7 @@
70
  <sort_order>2</sort_order>
71
  <show_in_default>1</show_in_default>
72
  <show_in_website>1</show_in_website>
73
- <show_in_store>1</show_in_store>
74
  </nocache_routes>
75
  <servers>
76
  <label>IPs to Varnish servers</label>
@@ -79,7 +76,7 @@
79
  <sort_order>3</sort_order>
80
  <show_in_default>1</show_in_default>
81
  <show_in_website>1</show_in_website>
82
- <show_in_store>1</show_in_store>
83
  </servers>
84
  <debug>
85
  <label>Debug Mode</label>
@@ -89,7 +86,7 @@
89
  <sort_order>3</sort_order>
90
  <show_in_default>1</show_in_default>
91
  <show_in_website>1</show_in_website>
92
- <show_in_store>1</show_in_store>
93
  </debug>
94
  </fields>
95
  </varnish>
10
  <cache>
11
  <label>Cache</label>
12
  <tab>made</tab>
 
13
  <sort_order>1000</sort_order>
14
  <show_in_default>1</show_in_default>
15
  <show_in_website>1</show_in_website>
17
  <groups>
18
  <general translate="label">
19
  <label>General Settings</label>
 
20
  <sort_order>0</sort_order>
21
  <show_in_default>1</show_in_default>
22
  <show_in_website>1</show_in_website>
46
  </general>
47
  <varnish translate="label">
48
  <label>Varnish Settings (Enabled in Cache Management)</label>
 
49
  <sort_order>1</sort_order>
50
  <show_in_default>1</show_in_default>
51
  <show_in_website>1</show_in_website>
58
  <sort_order>1</sort_order>
59
  <show_in_default>1</show_in_default>
60
  <show_in_website>1</show_in_website>
61
+ <show_in_store>1</show_in_store>
62
  </ttl>
63
  <nocache_routes>
64
  <label>Routes to exclude from cache</label>
67
  <sort_order>2</sort_order>
68
  <show_in_default>1</show_in_default>
69
  <show_in_website>1</show_in_website>
70
+ <show_in_store>1</show_in_store>
71
  </nocache_routes>
72
  <servers>
73
  <label>IPs to Varnish servers</label>
76
  <sort_order>3</sort_order>
77
  <show_in_default>1</show_in_default>
78
  <show_in_website>1</show_in_website>
79
+ <show_in_store>1</show_in_store>
80
  </servers>
81
  <debug>
82
  <label>Debug Mode</label>
86
  <sort_order>3</sort_order>
87
  <show_in_default>1</show_in_default>
88
  <show_in_website>1</show_in_website>
89
+ <show_in_store>1</show_in_store>
90
  </debug>
91
  </fields>
92
  </varnish>
app/code/community/Made/Cache/varnish/magento.vcl DELETED
@@ -1,186 +0,0 @@
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 DELETED
@@ -1,47 +0,0 @@
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
@@ -10,17 +10,15 @@
10
  <default>
11
  <cache>
12
  <!--
13
- Render "left" with a custom lifetime, the default is
14
  9999999999 seconds
15
  -->
16
- <name lifetime="86400">left</name>
17
- <name>cms_page</name>
18
- <name>head</name>
19
  <name>breadcrumbs</name>
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
@@ -28,7 +26,7 @@
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
34
  the toolbar block to switch listing types
@@ -42,12 +40,12 @@
42
  <block type="cache/profiler" name="cache_profiler" output="toHtml" ifconfig="cache/general/enable_profiler"/>
43
  </reference>
44
  </default>
45
-
46
  <varnish_enabled>
47
  <!--
48
  Example of blocks that typically should render using ESI, this
49
  is only used if Varnish is actually in front of Magento.
50
-
51
  It's best to keep these blocks at a minimum, and if possible,
52
  bundled together as single big blocks. At the moment there is
53
  no good way to serve the product view cached while recording
@@ -64,13 +62,13 @@
64
 
65
  <!--
66
  Messages are fetched using ESI outside of the normal layout.
67
-
68
  Stupid? Unsure. We must take care of all messages at every stage,
69
  keeping the standard distributed way of rendering messages fails
70
  here. The block is replaced under the hood with a special ESI tag.
71
-
72
  Keep global_messages as a combined renderer
73
-
74
  @see Made_Cache_Block_Messages
75
  -->
76
  <remove name="messages"/>
@@ -78,24 +76,24 @@
78
  <!-- There is no good way to manage the recently viewed block -->
79
  <remove name="right.reports.product.viewed"/>
80
  <remove name="left.reports.product.viewed"/>
81
-
82
  <!--
83
  The lazy session validation block must be added at the end to not
84
- block the page rendering. People generally don't remove the
85
  "content" block
86
  -->
87
  <reference name="content">
88
  <block type="cache/varnish_footer" name="varnish_cache_footer" after="-"/>
89
  </reference>
90
  </varnish_enabled>
91
-
92
  <checkout_onepage_index>
93
  <nocache>
94
  <name>root</name>
95
  <name>content</name>
96
  </nocache>
97
  </checkout_onepage_index>
98
-
99
  <!-- For use with Made_Streamcheckout, http://streamcheckout.com -->
100
  <streamcheckout_index_index>
101
  <nocache>
10
  <default>
11
  <cache>
12
  <!--
13
+ Render "cms_page" with a custom lifetime, the default is
14
  9999999999 seconds
15
  -->
16
+ <name lifetime="86400">cms_page</name>
 
 
17
  <name>breadcrumbs</name>
18
  <name>before_body_end</name>
19
  <name>product.info</name>
20
  <name>product_list</name>
21
+
22
  <!--
23
  Cache the cart sidebar with a 7200 second lifetime. Also
24
  force the cache key to be "cart_sidebar", since the cart
26
  etc. If this isn't true in your case, remove it!
27
  -->
28
  <name lifetime="7200" key="cart_sidebar">cart_sidebar</name>
29
+
30
  <!--
31
  People want to cache this one, but it works badly if you use
32
  the toolbar block to switch listing types
40
  <block type="cache/profiler" name="cache_profiler" output="toHtml" ifconfig="cache/general/enable_profiler"/>
41
  </reference>
42
  </default>
43
+
44
  <varnish_enabled>
45
  <!--
46
  Example of blocks that typically should render using ESI, this
47
  is only used if Varnish is actually in front of Magento.
48
+
49
  It's best to keep these blocks at a minimum, and if possible,
50
  bundled together as single big blocks. At the moment there is
51
  no good way to serve the product view cached while recording
62
 
63
  <!--
64
  Messages are fetched using ESI outside of the normal layout.
65
+
66
  Stupid? Unsure. We must take care of all messages at every stage,
67
  keeping the standard distributed way of rendering messages fails
68
  here. The block is replaced under the hood with a special ESI tag.
69
+
70
  Keep global_messages as a combined renderer
71
+
72
  @see Made_Cache_Block_Messages
73
  -->
74
  <remove name="messages"/>
76
  <!-- There is no good way to manage the recently viewed block -->
77
  <remove name="right.reports.product.viewed"/>
78
  <remove name="left.reports.product.viewed"/>
79
+
80
  <!--
81
  The lazy session validation block must be added at the end to not
82
+ block the page rendering. People generally don't remove the
83
  "content" block
84
  -->
85
  <reference name="content">
86
  <block type="cache/varnish_footer" name="varnish_cache_footer" after="-"/>
87
  </reference>
88
  </varnish_enabled>
89
+
90
  <checkout_onepage_index>
91
  <nocache>
92
  <name>root</name>
93
  <name>content</name>
94
  </nocache>
95
  </checkout_onepage_index>
96
+
97
  <!-- For use with Made_Streamcheckout, http://streamcheckout.com -->
98
  <streamcheckout_index_index>
99
  <nocache>
package.xml CHANGED
@@ -1,7 +1,7 @@
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,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-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>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Made_Cache</name>
4
+ <version>1.4.5</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-10-06</date>
22
+ <time>10:45:44</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="dade25416d387bac91900e1104cc34d1"/></dir><file name="List.php" hash="9088631b67b82bec56230659bf208b4a"/></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="1f2d12e8ace4daf36471704c899063ac"/></dir><dir name="Model"><file name="Layout.php" hash="b6f87785635a108884cd42147e5a2212"/><dir name="Observer"><file name="Abstract.php" hash="1d440b111ce0632a55ced2d5d48c6921"/><file name="Catalog.php" hash="468854697cbc082f390a00c80666c14d"/><file name="Checkout.php" hash="b4e2e2079b497960083f6d77ada268da"/><file name="Cms.php" hash="bccdb30163fe823d622d4f26edd61974"/></dir><file name="Observer.php" hash="ed544946e9842f1ffaed144b96a82274"/><file name="Profiler.php" hash="888d3e38e212d872a4420a3556ca4b08"/><file name="VarnishObserver.php" hash="945d41d8bf3aa4bd814660c7471ebf07"/></dir><dir name="controllers"><file name="VarnishController.php" hash="4c73b9c5b91679120a649432459759c9"/></dir><dir name="etc"><file name="config.xml" hash="25ca587aea908a12cdce0b51e95ad452"/><file name="magento.vcl" hash="c9abbe58020550890de31423b7ed08ff"/><file name="system.xml" hash="ce4148b8c255e877acc7f0f394d4caa5"/></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="fa9ab44e1d413d0969ba31f731ba3987"/></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>