Made_Cache - Version 1.0.6

Version Notes

N/A

Download this release

Release Info

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


Code changes from version 1.0.4 to 1.0.6

app/code/community/Made/Cache/Block/Checkout/Cart/Sidebar.php ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * The cart sidebar block is widely used and often put on top of a layout. As
4
+ * its loading time is usually around ~300ms, caching it is a good idea
5
+ *
6
+ * @package Made_Cache
7
+ * @author info@madepeople.se
8
+ * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
9
+ */
10
+ class Made_Cache_Block_Checkout_Cart_Sidebar
11
+ extends Mage_Checkout_Block_Cart_Sidebar
12
+ {
13
+ /**
14
+ * Return the quote item id
15
+ *
16
+ * @return int
17
+ */
18
+ protected function _getQuoteId()
19
+ {
20
+ $quote = $this->getCustomQuote() ? $this->getCustomQuote() : $this->getQuote();
21
+ return $quote->getId();
22
+ }
23
+
24
+ /**
25
+ * Only cache if there actually is a quote in the session. hasItems() is
26
+ * costly, so we don't want to use it. This means an empty cart block
27
+ * that previously had items is fetched from cache.
28
+ *
29
+ * @return int|null
30
+ */
31
+ public function getCacheLifetime()
32
+ {
33
+ if (!$this->getQuote()->getId()) {
34
+ return null;
35
+ }
36
+
37
+ return $this->getData('cache_lifetime');
38
+ }
39
+
40
+ /**
41
+ * Clear on custom cache tag maintained by observer
42
+ *
43
+ * @return array
44
+ */
45
+ public function getCacheTags()
46
+ {
47
+ $tags = array('SALES_QUOTE_' . $this->_getQuoteId());
48
+ return $tags;
49
+ }
50
+
51
+ /**
52
+ * Clear on custom cache tag maintained by observer
53
+ *
54
+ * @return array
55
+ */
56
+ public function getCacheKey()
57
+ {
58
+ $keys = array(
59
+ 'BLOCK_TPL',
60
+ Mage::app()->getStore()->getCode(),
61
+ $this->getTemplateFile(),
62
+ 'template' => $this->getTemplate(),
63
+ $this->_getQuoteId()
64
+ );
65
+
66
+ $key = array_values($keys);
67
+ $key = implode('|', $key);
68
+ $key = sha1($key);
69
+
70
+ return $key;
71
+ }
72
+ }
app/code/community/Made/Cache/Model/Layout.php CHANGED
@@ -16,6 +16,13 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
16
  */
17
  protected $_cacheBlocks = array();
18
 
 
 
 
 
 
 
 
19
  /**
20
  * Default cache lifetime
21
  *
@@ -81,6 +88,17 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
81
  }
82
  }
83
  }
 
 
 
 
 
 
 
 
 
 
 
84
  return $this;
85
  }
86
 
@@ -94,7 +112,7 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
94
  {
95
  // Generate parent for single block definitions
96
  if ($parentIsMain !== false) {
97
- $this->_generateBlock($parent, null);
98
  }
99
  if (empty($parent)) {
100
  $parent = $this->getNode();
@@ -157,7 +175,7 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
157
  }
158
 
159
  /**
160
- * Add block object to layout based on xml node data
161
  *
162
  * @param Varien_Simplexml_Element $node
163
  * @param Varien_Simplexml_Element $parent
@@ -165,24 +183,13 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
165
  */
166
  protected function _generateBlock($node, $parent)
167
  {
168
- if (!empty($node['class'])) {
169
- $className = (string)$node['class'];
170
- } else {
171
- $className = (string)$node['type'];
172
- }
173
-
174
  $blockName = (string)$node['name'];
175
- $_profilerKey = 'BLOCK: '.$blockName;
176
- Varien_Profiler::start($_profilerKey);
177
-
178
- $block = $this->addBlock($className, $blockName);
179
  if (!$block) {
180
  return $this;
181
  }
182
-
183
- if (!empty($node['esi'])) {
184
- $block->setData('esi', (int)$node['esi']);
185
- }
186
 
187
  if (in_array($blockName, array_keys($this->_cacheBlocks))) {
188
  $block->setData('cache_lifetime', $this->_cacheBlocks[$blockName]);
@@ -191,43 +198,11 @@ class Made_Cache_Model_Layout extends Mage_Core_Model_Layout
191
  $block->setData('cache_tags', (string)$node['cache_tags']);
192
  }
193
  }
194
-
195
- if (!empty($node['parent'])) {
196
- $parentBlock = $this->getBlock((string)$node['parent']);
197
- } elseif ($parent) {
198
- $parentName = $parent->getBlockName();
199
- if (!empty($parentName)) {
200
- $parentBlock = $this->getBlock($parentName);
201
- }
202
- }
203
- if (!empty($parentBlock)) {
204
- $alias = isset($node['as']) ? (string)$node['as'] : '';
205
- if (isset($node['before'])) {
206
- $sibling = (string)$node['before'];
207
- if ('-'===$sibling) {
208
- $sibling = '';
209
- }
210
- $parentBlock->insert($block, $sibling, false, $alias);
211
- } elseif (isset($node['after'])) {
212
- $sibling = (string)$node['after'];
213
- if ('-'===$sibling) {
214
- $sibling = '';
215
- }
216
- $parentBlock->insert($block, $sibling, true, $alias);
217
- } else {
218
- $parentBlock->append($block, $alias);
219
- }
220
- }
221
- if (!empty($node['template'])) {
222
- $block->setTemplate((string)$node['template']);
223
- }
224
-
225
- if (!empty($node['output'])) {
226
- $method = (string)$node['output'];
227
- $this->addOutputBlock($blockName, $method);
228
  }
229
- Varien_Profiler::stop($_profilerKey);
230
-
231
  return $this;
232
  }
233
  }
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
  *
88
  }
89
  }
90
  }
91
+
92
+ // Find blocks that should be represented by ESI tags
93
+ $esiList = $xml->xpath("//esi/*");
94
+ if (count($esiList)) {
95
+ foreach ($esiList as $node) {
96
+ $blockName = trim((string)$node);
97
+ $this->_esiBlocks[] = $blockName;
98
+ }
99
+ array_unique($this->_esiBlocks);
100
+ }
101
+
102
  return $this;
103
  }
104
 
112
  {
113
  // Generate parent for single block definitions
114
  if ($parentIsMain !== false) {
115
+ $this->_generateBlock($parent, new Varien_Object);
116
  }
117
  if (empty($parent)) {
118
  $parent = $this->getNode();
175
  }
176
 
177
  /**
178
+ * Add block object to layout based on XML node data
179
  *
180
  * @param Varien_Simplexml_Element $node
181
  * @param Varien_Simplexml_Element $parent
183
  */
184
  protected function _generateBlock($node, $parent)
185
  {
186
+ parent::_generateBlock($node, $parent);
187
+
 
 
 
 
188
  $blockName = (string)$node['name'];
189
+ $block = $this->getBlock($blockName);
 
 
 
190
  if (!$block) {
191
  return $this;
192
  }
 
 
 
 
193
 
194
  if (in_array($blockName, array_keys($this->_cacheBlocks))) {
195
  $block->setData('cache_lifetime', $this->_cacheBlocks[$blockName]);
198
  $block->setData('cache_tags', (string)$node['cache_tags']);
199
  }
200
  }
201
+
202
+ if (in_array($blockName, $this->_esiBlocks)) {
203
+ $block->setData('esi', 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  }
205
+
 
206
  return $this;
207
  }
208
  }
app/code/community/Made/Cache/Model/Observer.php CHANGED
@@ -24,9 +24,8 @@ class Made_Cache_Model_Observer
24
  }
25
 
26
  /**
27
- * ESI tags for Varnish, needs the block XML definition to have the
28
- * attribute esi="1", as well as Varnish configured with for instance
29
- * Phoenix_VarnishCache
30
  *
31
  * @param Varien_Event_Observer $observer
32
  */
@@ -70,4 +69,16 @@ class Made_Cache_Model_Observer
70
  ->cleanType(Mage_Core_Block_Abstract::CACHE_GROUP);
71
  }
72
  }
 
 
 
 
 
 
 
 
 
 
 
 
73
  }
24
  }
25
 
26
  /**
27
+ * ESI tags for Varnish, needs the block to have the attribute esi === 1
28
+ * as well as Varnish configured with for instance Phoenix_VarnishCache
 
29
  *
30
  * @param Varien_Event_Observer $observer
31
  */
69
  ->cleanType(Mage_Core_Block_Abstract::CACHE_GROUP);
70
  }
71
  }
72
+
73
+ /**
74
+ * Clear quote cache on custom tag
75
+ *
76
+ * @param Varien_Event_Observer $observer
77
+ */
78
+ public function clearQuoteCache(Varien_Event_Observer $observer)
79
+ {
80
+ // Only work when there is an active quote in the session
81
+ $object = $observer->getEvent()->getQuote();
82
+ Mage::app()->cleanCache(array('SALES_QUOTE_' . $object->getId()));
83
+ }
84
  }
app/code/community/Made/Cache/controllers/VarnishController.php CHANGED
@@ -32,7 +32,7 @@ class Made_Cache_VarnishController extends Mage_Core_Controller_Front_Action
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);
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);
app/code/community/Made/Cache/etc/config.xml CHANGED
@@ -9,7 +9,7 @@
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
- <version>1.0.4</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
@@ -40,8 +40,22 @@
40
  <widget_block>Made_Cache_Block_Cms_Widget_Block</widget_block>
41
  </rewrite>
42
  </cms>
 
 
 
 
 
43
  </blocks>
44
  <events>
 
 
 
 
 
 
 
 
 
45
  <review_save_after>
46
  <observers>
47
  <cache_review_save_after>
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
+ <version>1.0.6</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
40
  <widget_block>Made_Cache_Block_Cms_Widget_Block</widget_block>
41
  </rewrite>
42
  </cms>
43
+ <checkout>
44
+ <rewrite>
45
+ <cart_sidebar>Made_Cache_Block_Checkout_Cart_Sidebar</cart_sidebar>
46
+ </rewrite>
47
+ </checkout>
48
  </blocks>
49
  <events>
50
+ <sales_quote_save_after>
51
+ <observers>
52
+ <quote_save_after>
53
+ <type>singleton</type>
54
+ <class>cache/observer</class>
55
+ <method>clearQuoteCache</method>
56
+ </quote_save_after>
57
+ </observers>
58
+ </sales_quote_save_after>
59
  <review_save_after>
60
  <observers>
61
  <cache_review_save_after>
app/design/frontend/base/default/layout/madecache.xml CHANGED
@@ -21,7 +21,15 @@
21
  <name>product.info</name>
22
  <name>product_list</name>
23
  <name>category.products</name>
 
24
  </cache>
 
 
 
 
 
 
 
25
  </default>
26
 
27
  <checkout_onepage_index>
21
  <name>product.info</name>
22
  <name>product_list</name>
23
  <name>category.products</name>
24
+ <name lifetime="7200">cart_sidebar</name>
25
  </cache>
26
+
27
+ <!-- Example of rendering the cart using ESI -->
28
+ <!--
29
+ <esi>
30
+ <name>cart_sidebar</name>
31
+ </esi>
32
+ -->
33
  </default>
34
 
35
  <checkout_onepage_index>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Made_Cache</name>
4
- <version>1.0.4</version>
5
  <stability>stable</stability>
6
  <license>OSL 3.0</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_monday</user><email>jonathan@mondaycreative.se</email></author></authors>
21
- <date>2012-05-10</date>
22
- <time>12:03: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="ba8d5fba2fa01d571333e72146d80d40"/></dir><file name="List.php" hash="377798f55bcbe90fda0d3dd582bbea77"/><file name="View.php" hash="14a2b6a99872a0fbbfc9e5390f56642b"/></dir></dir><dir name="Cms"><file name="Block.php" hash="e99bc96a3a6cd1bee4658b163b1ee63e"/><file name="Page.php" hash="156f4eb0d9dfd6f8dd0d3fce40f0c356"/><dir name="Widget"><file name="Block.php" hash="3e74a85404ab796bf46db6c36e479d49"/></dir></dir></dir><dir name="Model"><file name="Layout.php" hash="e85e4af8ebe74d2a9a5c5635373eed7d"/><file name="Observer.php" hash="876dd58121c8554ef885316a8126c5c4"/></dir><dir name="controllers"><file name="VarnishController.php" hash="6e7207e07c5385568061b853eeca96fd"/></dir><dir name="etc"><file name="config.xml" hash="0536d9290ef7d8999801ff106497698f"/></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="3679ea4ed47b428e4c05314bf6bba3ee"/></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.0.6</version>
5
  <stability>stable</stability>
6
  <license>OSL 3.0</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_monday</user><email>jonathan@mondaycreative.se</email></author></authors>
21
+ <date>2012-05-12</date>
22
+ <time>15:45:42</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="ba8d5fba2fa01d571333e72146d80d40"/></dir><file name="List.php" hash="377798f55bcbe90fda0d3dd582bbea77"/><file name="View.php" hash="14a2b6a99872a0fbbfc9e5390f56642b"/></dir></dir><dir name="Checkout"><dir name="Cart"><file name="Sidebar.php" hash="93fc87f013fe054993383034c96c23b5"/></dir></dir><dir name="Cms"><file name="Block.php" hash="e99bc96a3a6cd1bee4658b163b1ee63e"/><file name="Page.php" hash="156f4eb0d9dfd6f8dd0d3fce40f0c356"/><dir name="Widget"><file name="Block.php" hash="3e74a85404ab796bf46db6c36e479d49"/></dir></dir></dir><dir name="Model"><file name="Layout.php" hash="94475af1640d47763b17914c4af73846"/><file name="Observer.php" hash="3d8aaad098e61bfd20c9701234ec7266"/></dir><dir name="controllers"><file name="VarnishController.php" hash="6ae7446eef83e106c56b0f05711c3ffa"/></dir><dir name="etc"><file name="config.xml" hash="30195c4aad4535bae9f47079c7d851c8"/></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="6a0f95410f1aed85851bcab8437b53e8"/></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>