Made_Cache - Version 1.1.0

Version Notes

N/A

Download this release

Release Info

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


Code changes from version 1.0.8 to 1.1.0

app/code/community/Made/Cache/Block/Catalog/Product/List.php CHANGED
@@ -1,6 +1,6 @@
1
  <?php
2
  /**
3
- * Granular product list cache
4
  *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
@@ -8,82 +8,6 @@
8
  */
9
  class Made_Cache_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
10
  {
11
- /**
12
- * Depending on block usage, the category id resides in different places
13
- *
14
- * @return int
15
- */
16
- protected function _getCategoryIdForCache()
17
- {
18
- return $this->getCategoryId() ?
19
- $this->getCategoryId() :
20
- Mage::getSingleton('catalog/layer')
21
- ->getCurrentCategory()
22
- ->getId()
23
- ;
24
- }
25
-
26
- /**
27
- * Clear on the specific category id and it's child products - when
28
- * a product is saved, the price can have changed and the cache must
29
- * be cleared
30
- *
31
- * @return string
32
- */
33
- public function getCacheTags()
34
- {
35
- $tags = array();
36
- $tags[] = Mage_Catalog_Model_Category::CACHE_TAG . '_' .
37
- $this->_getCategoryIdForCache();
38
-
39
- foreach ($this->_getProductCollection() as $_product) {
40
- $tags[] = Mage_Catalog_Model_Product::CACHE_TAG."_".$_product->getId();
41
- }
42
-
43
- return $tags;
44
- }
45
-
46
- /**
47
- * Take into concern all variables that can affect the product listing
48
- *
49
- * @return array
50
- */
51
- public function getCacheKeyInfo()
52
- {
53
- $keys = parent::getCacheKeyInfo();
54
-
55
- if (!is_array($keys)) {
56
- $keys = array();
57
- }
58
-
59
- $_taxRateRequest = Mage::getModel('tax/calculation')->getRateRequest();
60
- $_customer = Mage::getSingleton('customer/session')->getCustomer();
61
- $_categoryId = $this->_getCategoryIdForCache();
62
- $_toolbar = new Mage_Catalog_Block_Product_List_Toolbar();
63
-
64
- foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
65
- if (is_array($value)) {
66
- $value = implode('_', $value);
67
- }
68
- $keys[] = $key . $value;
69
- }
70
-
71
- return array_merge($keys, array(
72
- $_categoryId,
73
- $_toolbar->getCurrentOrder(),
74
- $_toolbar->getCurrentDirection(),
75
- $_toolbar->getCurrentMode(),
76
- $_toolbar->getCurrentPage(),
77
- $_toolbar->getLimit(),
78
- $_customer->getGroupId(),
79
- $_taxRateRequest->getCountryId(),
80
- $_taxRateRequest->getRegionId(),
81
- $_taxRateRequest->getPostcode(),
82
- $_taxRateRequest->getCustomerClassId(),
83
- Mage::registry('current_tag')
84
- ));
85
- }
86
-
87
  /**
88
  * For granular caching of product list blocks. Requires the markup
89
  * of a single product to be broken out of list.phtml into
1
  <?php
2
  /**
3
+ * Use this for granular product list cache
4
  *
5
  * @package Made_Cache
6
  * @author info@madepeople.se
8
  */
9
  class Made_Cache_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
10
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  /**
12
  * For granular caching of product list blocks. Requires the markup
13
  * of a single product to be broken out of list.phtml into
app/code/community/Made/Cache/Block/Catalog/Product/View.php DELETED
@@ -1,70 +0,0 @@
1
- <?php
2
- /**
3
- * Granular product view cache. Currently the product object is fetched
4
- * in the controller/helper regardless of existing cache or not. As this
5
- * is a block-level cache module, we choose to ignore that.
6
- *
7
- * @package Made_Cache
8
- * @author info@madepeople.se
9
- * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
10
- */
11
- class Made_Cache_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
12
- {
13
- /**
14
- * We don't want to cache an item that's being edited
15
- *
16
- * @return null|int
17
- */
18
- public function getCacheLifetime()
19
- {
20
- $request = $this->getRequest();
21
- if ($request->getModuleName() == 'checkout'
22
- && $request->getControllerName() == 'cart'
23
- && $request->getActionName() == 'configure'
24
- && $request->getParam('id')) {
25
- return null;
26
- }
27
- return $this->getData('cache_lifetime');
28
- }
29
-
30
- /**
31
- * Only clear on the specific product id - otherwise one product save
32
- * invalidates cache for all products
33
- *
34
- * @return string
35
- */
36
- public function getCacheTags()
37
- {
38
- $tags = array();
39
- $tags[] = Mage_Catalog_Model_Product::CACHE_TAG . '_'
40
- . $this->getProduct()->getId();
41
- return $tags;
42
- }
43
-
44
- /**
45
- * Take into concern all variables that can affect the product view
46
- *
47
- * @return array
48
- */
49
- public function getCacheKeyInfo()
50
- {
51
- $keys = parent::getCacheKeyInfo();
52
-
53
- if (!is_array($keys)) {
54
- $keys = array();
55
- }
56
-
57
- $_taxCalculator = Mage::getModel('tax/calculation');
58
- $_customer = Mage::getSingleton('customer/session')->getCustomer();
59
- $_product = $this->getProduct();
60
-
61
- return array_merge($keys, array(
62
- $_product->getId(),
63
- $_customer->getGroupId(),
64
- $_taxCalculator->getRate(
65
- $_taxCalculator->getRateRequest()
66
- ->setProductClassId($_product->getTaxClassId())
67
- )
68
- ));
69
- }
70
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Made/Cache/Block/Checkout/Cart/Item/Renderer.php DELETED
@@ -1,25 +0,0 @@
1
- <?php
2
- /**
3
- * @package Made_Cache
4
- * @author info@madepeople.se
5
- * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
6
- */
7
- class Made_Cache_Block_Checkout_Cart_Item_Renderer
8
- extends Mage_Checkout_Block_Cart_Item_Renderer
9
- {
10
- /**
11
- * Get item delete url without the redirect info as we don't know
12
- * where the item has been cached
13
- *
14
- * @return string
15
- */
16
- public function getDeleteUrl()
17
- {
18
- return $this->getUrl(
19
- 'checkout/cart/delete',
20
- array(
21
- 'id'=>$this->getItem()->getId(),
22
- )
23
- );
24
- }
25
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Made/Cache/Block/Checkout/Cart/Sidebar.php DELETED
@@ -1,72 +0,0 @@
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
- * Save 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/Block/Cms/Block.php DELETED
@@ -1,64 +0,0 @@
1
- <?php
2
- /**
3
- * Cache the CMS block, the issue here is that it doesn't necessarily exist
4
- * so we must take this into concern and not cache if there is nothing
5
- * fetched from the database
6
- *
7
- * @package Made_Cache
8
- * @author info@madepeople.se
9
- * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
10
- */
11
- class Made_Cache_Block_Cms_Block extends Mage_Cms_Block_Block
12
- {
13
- /**
14
- * Clear on the specific CMS block id
15
- *
16
- * @return string
17
- */
18
- public function getCacheTags()
19
- {
20
- $blockId = $this->getData('block_id');
21
- if ($blockId) {
22
- $block = Mage::getModel('cms/block')
23
- ->setStoreId(Mage::app()->getStore()->getId())
24
- ->load($blockId);
25
- if ($block->getIsActive()) {
26
- $tags = parent::getCacheTags();
27
- $tags[] = Mage_Cms_Model_Block::CACHE_TAG . '_' .
28
- $block->getId();
29
-
30
- return $tags;
31
- }
32
- }
33
-
34
- return array();
35
- }
36
-
37
- /**
38
- * Take into concern all variables that can affect the CMS block view
39
- *
40
- * @return array
41
- */
42
- public function getCacheKeyInfo()
43
- {
44
- $blockId = $this->getData('block_id');
45
- if ($blockId) {
46
- $block = Mage::getModel('cms/block')
47
- ->setStoreId(Mage::app()->getStore()->getId())
48
- ->load($blockId);
49
- if ($block->getIsActive()) {
50
- $keys = parent::getCacheKeyInfo();
51
-
52
- if (!is_array($keys)) {
53
- $keys = array();
54
- }
55
-
56
- $keys[] = $blockId;
57
- $keys[] = $this->getLayout()->getUpdate()->getCacheId();
58
- return $keys;
59
- }
60
- }
61
- return array();
62
- }
63
-
64
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Made/Cache/Block/Cms/Page.php DELETED
@@ -1,53 +0,0 @@
1
- <?php
2
- /**
3
- * Automatic CMS page cache
4
- *
5
- * @package Made_Cache
6
- * @author info@madepeople.se
7
- * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
8
- */
9
- class Made_Cache_Block_Cms_Page extends Mage_Cms_Block_Page
10
- {
11
- public function getCacheLifetime()
12
- {
13
- // If overriden by user
14
- if ($this->hasData('cache_lifetime')) {
15
- return $this->getData('lifetime');
16
- }
17
-
18
- return Made_Cache_Model_Layout::DEFAULT_CACHE_LIFETIME;
19
- }
20
-
21
- /**
22
- * Clear on the specific CMS page id
23
- *
24
- * @return string
25
- */
26
- public function getCacheTags()
27
- {
28
- $tags = parent::getCacheTags();
29
- $tags[] = Mage_Cms_Model_Page::CACHE_TAG . '_' .
30
- $this->getPage()->getId();
31
-
32
- return $tags;
33
- }
34
-
35
- /**
36
- * Take into concern all variables that can affect the CMS page view
37
- *
38
- * @return array
39
- */
40
- public function getCacheKeyInfo()
41
- {
42
- $keys = parent::getCacheKeyInfo();
43
-
44
- if (!is_array($keys)) {
45
- $keys = array();
46
- }
47
-
48
- $keys[] = $this->getPage()->getId();
49
- $keys[] = $this->getLayout()->getUpdate()->getCacheId();
50
-
51
- return $keys;
52
- }
53
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Made/Cache/Block/Cms/Widget/Block.php DELETED
@@ -1,61 +0,0 @@
1
- <?php
2
- /**
3
- * As with the standard CMS block, for widget rendering
4
- *
5
- * @package Made_Cache
6
- * @author info@madepeople.se
7
- * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
8
- */
9
- class Made_Cache_Block_Cms_Widget_Block extends Mage_Cms_Block_Widget_Block
10
- {
11
- /**
12
- * Clear on the specific CMS block id
13
- *
14
- * @return string
15
- */
16
- public function getCacheTags()
17
- {
18
- $blockId = $this->getData('block_id');;
19
- if ($blockId) {
20
- $block = Mage::getModel('cms/block')
21
- ->setStoreId(Mage::app()->getStore()->getId())
22
- ->load($blockId);
23
- if ($block->getIsActive()) {
24
- $tags = parent::getCacheTags();
25
- $tags[] = Mage_Cms_Model_Block::CACHE_TAG . '_' .
26
- $block->getId();
27
-
28
- return $tags;
29
- }
30
- }
31
-
32
- return array();
33
- }
34
-
35
- /**
36
- * Take into concern all variables that can affect the CMS block view
37
- *
38
- * @return array
39
- */
40
- public function getCacheKeyInfo()
41
- {
42
- $blockId = $this->getData('block_id');;
43
- if ($blockId) {
44
- $block = Mage::getModel('cms/block')
45
- ->setStoreId(Mage::app()->getStore()->getId())
46
- ->load($blockId);
47
- if ($block->getIsActive()) {
48
- $keys = parent::getCacheKeyInfo();
49
-
50
- if (!is_array($keys)) {
51
- $keys = array();
52
- }
53
-
54
- $keys[] = $blockId;
55
- $keys[] = $this->getLayout()->getUpdate()->getCacheId();
56
- return $keys;
57
- }
58
- }
59
- return array();
60
- }
61
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/code/community/Made/Cache/Model/Observer.php CHANGED
@@ -8,6 +8,46 @@
8
  */
9
  class Made_Cache_Model_Observer
10
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  /**
12
  * Clear product cache after a review is saved
13
  *
@@ -81,4 +121,24 @@ class Made_Cache_Model_Observer
81
  $object = $observer->getEvent()->getQuote();
82
  Mage::app()->cleanCache(array('SALES_QUOTE_' . $object->getId()));
83
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
8
  */
9
  class Made_Cache_Model_Observer
10
  {
11
+ /**
12
+ * Observer that injects cache values into specific blocks, we want
13
+ * to do it like this instead of block rewrites to prevent other
14
+ * third-party modules to work
15
+ *
16
+ * @param Varien_Event_Observer $observer
17
+ */
18
+ public function setupBlock(Varien_Event_Observer $observer)
19
+ {
20
+ $block = $observer->getEvent()->getBlock();
21
+ if ($block->getCacheLifetime() === null) {
22
+ // null lifetime means don't use cache
23
+ return;
24
+ }
25
+
26
+ switch (true) {
27
+ case $block instanceof Mage_Catalog_Block_Product_View:
28
+ Mage::getSingleton('cache/observer_catalog')
29
+ ->applyProductView($block);
30
+ break;
31
+ case $block instanceof Mage_Catalog_Block_Product_List:
32
+ Mage::getSingleton('cache/observer_catalog')
33
+ ->applyProductList($block);
34
+ break;
35
+ case $block instanceof Mage_Cms_Block_Page:
36
+ Mage::getSingleton('cache/observer_cms')
37
+ ->applyCmsPage($block);
38
+ break;
39
+ case $block instanceof Mage_Cms_Block_Block:
40
+ case $block instanceof Mage_Cms_Block_Widget_Block:
41
+ Mage::getSingleton('cache/observer_cms')
42
+ ->applyCmsBlock($block);
43
+ break;
44
+ case $block instanceof Mage_Checkout_Block_Cart_Sidebar:
45
+ Mage::getSingleton('cache/observer_checkout')
46
+ ->applyCartSidebar($block);
47
+ break;
48
+ }
49
+ }
50
+
51
  /**
52
  * Clear product cache after a review is saved
53
  *
121
  $object = $observer->getEvent()->getQuote();
122
  Mage::app()->cleanCache(array('SALES_QUOTE_' . $object->getId()));
123
  }
124
+
125
+ /**
126
+ * Unset the uenc param for redirection of blocks that have cached links.
127
+ * If we don't do this, links redirects to where user X came from when
128
+ * the block was cached. This also means that cached return links for
129
+ * ESI blocks return a visitor to the ESI-URL, and we can't have that.
130
+ *
131
+ * @param Varien_Event_Observer $observer
132
+ */
133
+ public function removeUencParam(Varien_Event_Observer $observer)
134
+ {
135
+ $request = $observer->getEvent()->getControllerAction()
136
+ ->getRequest();
137
+
138
+ // To this day, the only used encoding type is PARAM_NAME_URL_ENCODED
139
+ $key = Mage_Core_Controller_Varien_Action::PARAM_NAME_URL_ENCODED;
140
+ if (($param = $request->getParam($key)) !== null) {
141
+ $request->setParam($key, null);
142
+ }
143
+ }
144
  }
app/code/community/Made/Cache/Model/Observer/Abstract.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package Made_Cache
4
+ * @author info@madepeople.se
5
+ * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
6
+ */
7
+ abstract class Made_Cache_Model_Observer_Abstract
8
+ {
9
+ /**
10
+ * Get Key for caching block content
11
+ *
12
+ * @see Mage_Core_Block_Abstract
13
+ * @return string
14
+ */
15
+ protected function _getCacheKey(array $keyInfo)
16
+ {
17
+ $keyInfo = array_values($keyInfo); // ignore array keys
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
+ {
31
+ $keys = $block->getCacheKeyInfo();
32
+ if (!is_array($keys)) {
33
+ $keys = array();
34
+ }
35
+ $keys[] = $block->getLayout()->getUpdate()->getCacheId();
36
+ return $keys;
37
+ }
38
+ }
app/code/community/Made/Cache/Model/Observer/Catalog.php ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/)
8
+ */
9
+ class Made_Cache_Model_Observer_Catalog
10
+ extends Made_Cache_Model_Observer_Abstract
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 (($messagesBlock = $block->getLayout()->getBlock('messages')) !== null) {
21
+ if (count($messagesBlock->getMessageCollection())) {
22
+ $block->setData('cache_lifetime', null);
23
+ return;
24
+ }
25
+ }
26
+
27
+ // Cart stuff is session-dependent
28
+ $request = $block->getRequest();
29
+ if ($request->getModuleName() == 'checkout'
30
+ && $request->getControllerName() == 'cart'
31
+ && $request->getActionName() == 'configure'
32
+ && $request->getParam('id')) {
33
+ $block->setData('cache_lifetime', null);
34
+ return;
35
+ }
36
+
37
+ // Set cache tags
38
+ $tags = array(Mage_Catalog_Model_Product::CACHE_TAG . '_'
39
+ . $block->getProduct()->getId());
40
+ $block->setData('cache_tags', $tags);
41
+
42
+ // Set cache keys
43
+ $keys = $this->_getBasicKeys($block);
44
+
45
+ $_taxCalculator = Mage::getModel('tax/calculation');
46
+ $_customer = Mage::getSingleton('customer/session')->getCustomer();
47
+ $_product = $block->getProduct();
48
+
49
+ $keys = array_merge($keys, array(
50
+ $_product->getId(),
51
+ $_customer->getGroupId(),
52
+ $_taxCalculator->getRate(
53
+ $_taxCalculator->getRateRequest()
54
+ ->setProductClassId($_product->getTaxClassId())
55
+ )
56
+ ));
57
+ $block->setData('cache_key', $this->_getCacheKey($keys));
58
+ }
59
+
60
+ /**
61
+ * Depending on block usage, the category id resides in different places
62
+ *
63
+ * @return int
64
+ */
65
+ protected function _getCategoryIdForProductList($block)
66
+ {
67
+ return $block->getCategoryId() ?
68
+ $block->getCategoryId() :
69
+ Mage::getSingleton('catalog/layer')
70
+ ->getCurrentCategory()
71
+ ->getId()
72
+ ;
73
+ }
74
+
75
+ /**
76
+ * Product list cache, needs to clear on displayed products
77
+ *
78
+ * @param Mage_Catalog_Block_Product_List $block
79
+ */
80
+ public function applyProductList(Mage_Catalog_Block_Product_List $block)
81
+ {
82
+ // The "messages" block is session-dependent, don't cache
83
+ if (($messagesBlock = $block->getLayout()->getBlock('messages')) !== null) {
84
+ if (count($messagesBlock->getMessageCollection())) {
85
+ $block->setData('cache_lifetime', null);
86
+ return;
87
+ }
88
+ }
89
+
90
+ // Set cache tags
91
+ $tags = array();
92
+ $tags[] = Mage_Catalog_Model_Category::CACHE_TAG . '_' .
93
+ $this->_getCategoryIdForProductList($block);
94
+
95
+ // The toolbar needs to apply sort order etc
96
+ $productCollection = $block->getLoadedProductCollection();
97
+ $_toolbar = new Mage_Catalog_Block_Product_List_Toolbar();
98
+ $_toolbar->setCollection($productCollection);
99
+
100
+ foreach ($productCollection as $_product) {
101
+ $tags[] = Mage_Catalog_Model_Product::CACHE_TAG."_".$_product->getId();
102
+ }
103
+ $block->setData('cache_tags', $tags);
104
+
105
+ // Set cache key
106
+ $keys = $this->_getBasicKeys($block);
107
+
108
+ $_taxRateRequest = Mage::getModel('tax/calculation')->getRateRequest();
109
+ $_customer = Mage::getSingleton('customer/session')->getCustomer();
110
+ $_categoryId = $this->_getCategoryIdForProductList($block);
111
+
112
+ foreach (Mage::app()->getRequest()->getParams() as $key => $value) {
113
+ if (is_array($value)) {
114
+ $value = implode('_', $value);
115
+ }
116
+ $keys[] = $key . '_' . $value;
117
+ }
118
+
119
+ $keys = array_merge($keys, array(
120
+ $_categoryId,
121
+ $_toolbar->getCurrentOrder(),
122
+ $_toolbar->getCurrentDirection(),
123
+ $_toolbar->getCurrentMode(),
124
+ $_toolbar->getCurrentPage(),
125
+ $_toolbar->getLimit(),
126
+ $_customer->getGroupId(),
127
+ $_taxRateRequest->getCountryId(),
128
+ $_taxRateRequest->getRegionId(),
129
+ $_taxRateRequest->getPostcode(),
130
+ $_taxRateRequest->getCustomerClassId(),
131
+ Mage::registry('current_tag')
132
+ ));
133
+ $block->setData('cache_key', $this->_getCacheKey($keys));
134
+ }
135
+ }
app/code/community/Made/Cache/Model/Observer/Checkout.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Inject cache variables for checkout blocks
4
+ *
5
+ * @package Made_Cache
6
+ * @author info@madepeople.se
7
+ * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
8
+ */
9
+ class Made_Cache_Model_Observer_Checkout
10
+ extends Made_Cache_Model_Observer_Abstract
11
+ {
12
+ /**
13
+ * Return the quote item id
14
+ *
15
+ * @param Mage_Checkout_Block_Cart_Sidebar $block
16
+ * @return int
17
+ */
18
+ protected function _getQuoteId(Mage_Checkout_Block_Cart_Sidebar $block)
19
+ {
20
+ $quote = $block->getCustomQuote() ? $block->getCustomQuote() : $block->getQuote();
21
+ return $quote->getId();
22
+ }
23
+
24
+ /**
25
+ * Don't cache this block if there is no quote item set. We can't call
26
+ * hasItems because it's too costly
27
+ *
28
+ * @param Mage_Checkout_Block_Cart_Sidebar $block
29
+ */
30
+ public function applyCartSidebar(Mage_Checkout_Block_Cart_Sidebar $block)
31
+ {
32
+ if (!$block->getQuote()->getId()) {
33
+ $block->setData('cache_lifetime', null);
34
+ return;
35
+ }
36
+
37
+ // Set cache tags
38
+ $tags = array('SALES_QUOTE_' . $this->_getQuoteId($block));
39
+ $block->setData('cache_tags', $tags);
40
+
41
+ // Set cache keys
42
+ $keys = array(
43
+ 'BLOCK_TPL',
44
+ Mage::app()->getStore()->getCode(),
45
+ $block->getTemplateFile(),
46
+ 'template' => $block->getTemplate(),
47
+ $this->_getQuoteId($block)
48
+ );
49
+ $block->setData('cache_key', $this->_getCacheKey($keys));
50
+ }
51
+ }
app/code/community/Made/Cache/Model/Observer/Cms.php ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Inject cache variables for cms blocks
4
+ *
5
+ * @package Made_Cache
6
+ * @author info@madepeople.se
7
+ * @copyright Copyright (c) 2012 Made People AB. (http://www.madepeople.se/)
8
+ */
9
+ class Made_Cache_Model_Observer_Cms
10
+ extends Made_Cache_Model_Observer_Abstract
11
+ {
12
+ /**
13
+ * CMS Page cache
14
+ *
15
+ * @param Mage_Cms_Block_Page $block
16
+ */
17
+ public function applyCmsPage(Mage_Cms_Block_Page $block)
18
+ {
19
+ // The "messages" block is session-dependent, don't cache
20
+ if (($messagesBlock = $block->getLayout()->getBlock('messages')) !== null) {
21
+ if (count($messagesBlock->getMessageCollection())) {
22
+ $block->setData('cache_lifetime', null);
23
+ return;
24
+ }
25
+ }
26
+
27
+ // Set cache tags
28
+ $tags = $block->getCacheTags();
29
+ $tags[] = Mage_Cms_Model_Page::CACHE_TAG . '_' .
30
+ $block->getPage()->getId();
31
+ $block->setData('cache_tags', $tags);
32
+
33
+ // Set cache keys
34
+ $keys = $this->_getBasicKeys($block);
35
+
36
+ $keys[] = $block->getPage()->getId();
37
+ $keys[] = $block->getLayout()->getUpdate()->getCacheId();
38
+ $block->setData('cache_key', $this->_getCacheKey($keys));
39
+ }
40
+
41
+ /**
42
+ * CMS block cache, must use the block id from the database
43
+ *
44
+ * @param type $block
45
+ */
46
+ public function applyCmsBlock($block)
47
+ {
48
+ // The "messages" block is session-dependent, don't cache
49
+ if (($messagesBlock = $block->getLayout()->getBlock('messages')) !== null) {
50
+ if (count($messagesBlock->getMessageCollection())) {
51
+ $block->setData('cache_lifetime', null);
52
+ return;
53
+ }
54
+ }
55
+
56
+ // Set cache tags
57
+ $tags = array();
58
+
59
+ $blockId = $block->getData('block_id');;
60
+ if ($blockId) {
61
+ $cmsBlock = Mage::getModel('cms/block')
62
+ ->setStoreId(Mage::app()->getStore()->getId())
63
+ ->load($blockId);
64
+ if ($cmsBlock->getIsActive()) {
65
+ $tags = $block->getCacheTags();
66
+ $tags[] = Mage_Cms_Model_Block::CACHE_TAG . '_' .
67
+ $cmsBlock->getId();
68
+ }
69
+ }
70
+ $block->setData('cache_tags', $tags);
71
+
72
+ // Set cache key
73
+ $keys = $this->_getBasicKeys($block);
74
+
75
+ $blockId = $block->getData('block_id');;
76
+ if ($blockId) {
77
+ $cmsBlock = Mage::getModel('cms/block')
78
+ ->setStoreId(Mage::app()->getStore()->getId())
79
+ ->load($blockId);
80
+ if ($cmsBlock->getIsActive()) {
81
+ $keys = $block->getCacheKeyInfo();
82
+
83
+ if (!is_array($keys)) {
84
+ $keys = array();
85
+ }
86
+
87
+ $keys[] = $blockId;
88
+ $keys[] = $block->getLayout()->getUpdate()->getCacheId();
89
+ }
90
+ }
91
+ $block->setData('cache_key', $this->_getCacheKey($keys));
92
+ }
93
+ }
app/code/community/Made/Cache/etc/config.xml CHANGED
@@ -9,7 +9,7 @@
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
- <version>1.0.6</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
@@ -27,27 +27,35 @@
27
  <cache>
28
  <class>Made_Cache_Block</class>
29
  </cache>
30
- <catalog>
31
- <rewrite>
32
- <product_view>Made_Cache_Block_Catalog_Product_View</product_view>
33
- <product_list>Made_Cache_Block_Catalog_Product_List</product_list>
34
- </rewrite>
35
- </catalog>
36
- <cms>
37
- <rewrite>
38
- <page>Made_Cache_Block_Cms_Page</page>
39
- <block>Made_Cache_Block_Cms_Block</block>
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
- <cart_item_renderer>Made_Cache_Block_Checkout_Cart_Item_Renderer</cart_item_renderer>
47
- </rewrite>
48
- </checkout>
49
  </blocks>
50
  <events>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  <sales_quote_save_after>
52
  <observers>
53
  <quote_save_after>
@@ -66,15 +74,6 @@
66
  </cache_review_save_after>
67
  </observers>
68
  </review_save_after>
69
- <core_block_abstract_to_html_after>
70
- <observers>
71
- <cache_add_esi_tags>
72
- <type>singleton</type>
73
- <class>cache/observer</class>
74
- <method>addEsiTag</method>
75
- </cache_add_esi_tags>
76
- </observers>
77
- </core_block_abstract_to_html_after>
78
  <controller_action_postdispatch_adminhtml_catalog_product_save>
79
  <observers>
80
  <clean_cache_after_product_save>
@@ -103,7 +102,7 @@
103
  </madecache>
104
  </updates>
105
  </layout>
106
- <!-- We rewrite catalog blocks - let's use its translations -->
107
  <translate>
108
  <modules>
109
  <Made_Cache>
9
  <config>
10
  <modules>
11
  <Made_Cache>
12
+ <version>1.1.0</version>
13
  </Made_Cache>
14
  </modules>
15
  <global>
27
  <cache>
28
  <class>Made_Cache_Block</class>
29
  </cache>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  </blocks>
31
  <events>
32
+ <controller_action_predispatch>
33
+ <observers>
34
+ <remove_uenc_param>
35
+ <type>singleton</type>
36
+ <class>cache/observer</class>
37
+ <method>removeUencParam</method>
38
+ </remove_uenc_param>
39
+ </observers>
40
+ </controller_action_predispatch>
41
+ <core_block_abstract_to_html_before>
42
+ <observers>
43
+ <cache_setup_block>
44
+ <type>singleton</type>
45
+ <class>cache/observer</class>
46
+ <method>setupBlock</method>
47
+ </cache_setup_block>
48
+ </observers>
49
+ </core_block_abstract_to_html_before>
50
+ <core_block_abstract_to_html_after>
51
+ <observers>
52
+ <cache_add_esi_tags>
53
+ <type>singleton</type>
54
+ <class>cache/observer</class>
55
+ <method>addEsiTag</method>
56
+ </cache_add_esi_tags>
57
+ </observers>
58
+ </core_block_abstract_to_html_after>
59
  <sales_quote_save_after>
60
  <observers>
61
  <quote_save_after>
74
  </cache_review_save_after>
75
  </observers>
76
  </review_save_after>
 
 
 
 
 
 
 
 
 
77
  <controller_action_postdispatch_adminhtml_catalog_product_save>
78
  <observers>
79
  <clean_cache_after_product_save>
102
  </madecache>
103
  </updates>
104
  </layout>
105
+ <!-- We have catalog blocks - let's use its translations -->
106
  <translate>
107
  <modules>
108
  <Made_Cache>
app/code/community/Made/Cache/etc/magento.vcl CHANGED
@@ -86,6 +86,8 @@ sub vcl_recv {
86
  }
87
 
88
  # as soon as we have a NO_CACHE cookie pass request
 
 
89
  #if (req.http.cookie ~ "NO_CACHE=") {
90
  # return (pass);
91
  #}
86
  }
87
 
88
  # as soon as we have a NO_CACHE cookie pass request
89
+ # Modification for Made_Cache, ideally we should only disable cache per
90
+ # specific ruote
91
  #if (req.http.cookie ~ "NO_CACHE=") {
92
  # return (pass);
93
  #}
package.xml CHANGED
@@ -1,26 +1,26 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Made_Cache</name>
4
- <version>1.0.8</version>
5
  <stability>stable</stability>
6
  <license>OSL 3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
- <summary>Advanced Block Cache module that enhances performance by implementing arbitrary block cache with support for Varnish ESI tags.</summary>
10
  <description>Few know that Magento out of the box doesn't actually cache anything other than the Navigation and Footer blocks, which are basically static as it is.&#xD;
11
  &#xD;
12
  This module enhances performance by allowing developers to cache any block they want via simple layout xml changes - per layout handle.&#xD;
13
  &#xD;
14
  There is also support for disabling cache for specific handles such as checkout, etc, where you don't want to load the main content block from cache.&#xD;
15
  &#xD;
16
- ESI is supported in conjunction with Phoenix_VarnishCache, and allows for super-fast page views. Simply add esi="1" to any block XML definition, and the rest is automatic.&#xD;
17
  &#xD;
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>2012-05-14</date>
22
- <time>13:27:45</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="377798f55bcbe90fda0d3dd582bbea77"/><file name="View.php" hash="14a2b6a99872a0fbbfc9e5390f56642b"/></dir></dir><dir name="Checkout"><dir name="Cart"><dir name="Item"><file name="Renderer.php" hash="4cc697ffc616bae331d584296a91da1b"/></dir><file name="Sidebar.php" hash="f2c2c2c53e791b94e23ac635894213a0"/></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="f3bf85d778680fb1246416ad5b63e961"/><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="a1121d761b3ec8713e2bc6537dcfd0f8"/><file name="magento.vcl" hash="d97e5b6df11bf7e6fb47a4fcaa4e81ae"/></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="e7431217f4b0ddc25b405c26530f9161"/></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.1.0</version>
5
  <stability>stable</stability>
6
  <license>OSL 3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
+ <summary>Advanced non-intrusive Block Cache module that enhances performance by implementing arbitrary block cache with support for Varnish ESI tags.</summary>
10
  <description>Few know that Magento out of the box doesn't actually cache anything other than the Navigation and Footer blocks, which are basically static as it is.&#xD;
11
  &#xD;
12
  This module enhances performance by allowing developers to cache any block they want via simple layout xml changes - per layout handle.&#xD;
13
  &#xD;
14
  There is also support for disabling cache for specific handles such as checkout, etc, where you don't want to load the main content block from cache.&#xD;
15
  &#xD;
16
+ ESI is supported in conjunction with Phoenix_VarnishCache, and allows for super-fast page views. Check out layout/madecache.xml for examples.&#xD;
17
  &#xD;
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>2012-05-23</date>
22
+ <time>13:26:54</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="06ab53286ff19e5cf057b6f3da90088f"/></dir></dir></dir><dir name="Model"><file name="Layout.php" hash="f3bf85d778680fb1246416ad5b63e961"/><dir name="Observer"><file name="Abstract.php" hash="e8e501179fdbca2596b4d8b5291ec95c"/><file name="Catalog.php" hash="c58a254f967aba975cb8351e0ce5eb90"/><file name="Checkout.php" hash="4c1361159d91da2452c1d50e1fc52505"/><file name="Cms.php" hash="dac87d4f7e6720136fc84ba3ac32ff7d"/></dir><file name="Observer.php" hash="72c108a3656b1e813bcb3dcb16f0b3de"/></dir><dir name="controllers"><file name="VarnishController.php" hash="6ae7446eef83e106c56b0f05711c3ffa"/></dir><dir name="etc"><file name="config.xml" hash="6eea279bba8f9e4c0c4bf3c44e6a5817"/><file name="magento.vcl" hash="dd4f36661f4da1563fb383aa23e872d1"/></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="e7431217f4b0ddc25b405c26530f9161"/></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>