├── modman ├── composer.json ├── app ├── etc │ └── modules │ │ └── Rkt_SbCache.xml └── code │ └── community │ └── Rkt │ └── SbCache │ ├── etc │ ├── system.xml │ └── config.xml │ ├── Helper │ └── Data.php │ └── Model │ └── Observer.php ├── LICENSE.txt └── README.md /modman: -------------------------------------------------------------------------------- 1 | #Rkt_SbCache 2 | app/code/community/Rkt/SbCache app/code/community/Rkt/SbCache 3 | app/etc/modules/Rkt_SbCache.xml app/etc/modules/Rkt_SbCache.xml -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "progammer-rkt/Rkt_SbCache", 3 | "type": "magento-module", 4 | "description": "Magento Extension that will allow to cache CMS Blocks aka Static Blocks", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Rajeev K Tomy", 9 | "email": "programmer.rkt@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /app/etc/modules/Rkt_SbCache.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | true 24 | community 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Rajeev K Tomy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rkt_SbCache - Magento Extension 2 | ================================= 3 | 4 | Magento Extension that will allow to cache CMS Blocks aka Static Blocks 5 | 6 | Description 7 | ------------ 8 | 9 | By default, Magento is not cacheing CMS Blocks. But in almost all stores, there is a huge chance to use a bunch of CMS Blocks. So cacheing static blocks become important in that case for better page load performance. 10 | 11 | This module will fill this gap. This module enable us to cache static blocks uniquely. That is for each static blocks, this will use a unique cache key. Default cache time is set to the default cache life time of Magento (ie 2 hrs). 12 | 13 | Theory 14 | ------- 15 | 16 | This module register an observer for the event `core_block_abstract_to_html_before` and through this observer, if a static block is present, it will generate a unique cahche key for that block. 17 | 18 | Advantages 19 | ------------ 20 | 21 | - No core hack 22 | - No rewrites 23 | 24 | Support 25 | ------- 26 | If you encounter any problems or bugs, please create an issue on 27 | [GitHub](https://github.com/progammer-rkt/Rkt_SbCache/issues). 28 | 29 | Contribution 30 | ------------ 31 | Any contribution is highly welcome. The best possibility to provide any code is to open 32 | a [pull request on GitHub](https://help.github.com/articles/using-pull-requests). -------------------------------------------------------------------------------- /app/code/community/Rkt/SbCache/etc/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 1 9 | 1 10 | 1 11 | 800 12 | 13 | 14 | 15 | select 16 | adminhtml/system_config_source_yesno 17 | 10 18 | 1 19 | 1 20 | 1 21 | 22 | 23 | 24 | text 25 | 20 26 | 1 27 | 0 28 | 0 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/code/community/Rkt/SbCache/etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 20 | 21 | 22 | 23 | 1.0.0 24 | 25 | 26 | 27 | 28 | 29 | Rkt_SbCache_Model 30 | 31 | 32 | 33 | 34 | Rkt_SbCache_Helper 35 | 36 | 37 | 38 | 39 | 40 | 41 | rkt_sbcache/observer 42 | enableCmsBlockCaching 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 1 52 | 7200 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /app/code/community/Rkt/SbCache/Helper/Data.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright Copyright (c) 2015 16 | * @license http://opensource.org/licenses/mit-license.php MIT License 17 | */ 18 | 19 | /** 20 | * Helper Class 21 | * 22 | * Default Helper class. 23 | * 24 | * @category Rkt 25 | * @package Rkt_Config 26 | * @author Rajeev K Tomy 27 | */ 28 | class Rkt_SbCache_Helper_Data extends Mage_Core_Helper_Abstract 29 | { 30 | /** 31 | * Module configs 32 | */ 33 | const XML_PATH_RKT_CACHE_ENABLED = 'system/rkt_sbcache/enabled'; 34 | const XML_PATH_RKT_CACHE_TIMEOUT = 'system/rkt_sbcache/timeout'; 35 | 36 | /** 37 | * constants. 38 | * 39 | * @const NUM_LETTERS Number + Letters. 40 | */ 41 | const NUM_LETTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 42 | 43 | /** 44 | * Use to generate a random string 45 | * 46 | * @access public 47 | * @param integer $length 48 | * @return string 49 | */ 50 | public function randomString($length = 5) 51 | { 52 | $characters = self::NUM_LETTERS; 53 | $charactersLength = strlen($characters); 54 | $randomString = ''; 55 | 56 | for ($i = 0; $i < $length; $i++) { 57 | $randomString .= $characters[rand(0, $charactersLength - 1)]; 58 | } 59 | 60 | return $randomString; 61 | } 62 | 63 | /** 64 | * @return bool 65 | */ 66 | public function isEnabled() 67 | { 68 | return Mage::getStoreConfig(self::XML_PATH_RKT_CACHE_ENABLED); 69 | } 70 | 71 | /** 72 | * @return int 73 | */ 74 | public function getCacheTimeout() 75 | { 76 | return Mage::getStoreConfig(self::XML_PATH_RKT_CACHE_TIMEOUT); 77 | } 78 | 79 | } -------------------------------------------------------------------------------- /app/code/community/Rkt/SbCache/Model/Observer.php: -------------------------------------------------------------------------------- 1 | 15 | * @copyright Copyright (c) 2015 16 | * @license http://opensource.org/licenses/mit-license.php MIT License 17 | */ 18 | 19 | /** 20 | * Observer Class 21 | * 22 | * Listen To : core_block_abstract_to_html_before 23 | * 24 | * Use to apply cacheing for CMS Blocks, which lacks in core Magento. 25 | * 26 | * @category Rkt 27 | * @package Rkt_Config 28 | * @author Rajeev K Tomy 29 | */ 30 | class Rkt_SbCache_Model_Observer 31 | { 32 | 33 | /** 34 | * Use to apply cacheing for CMS Blocks in Magento. 35 | * 36 | * By default, Magento is not applying cacheing for CMS blocks. This function 37 | * will apply cache for CMS Blocks and thus help us to overcome this difficulty. 38 | * 39 | * @access public 40 | * @param Varien_Event_Observer $observer 41 | * @return Rkt_SbCache_Model_Observer 42 | */ 43 | public function enableCmsBlockCaching(Varien_Event_Observer $observer) 44 | { 45 | if (!$this->_getHelper()->isEnabled()){ 46 | return; 47 | } 48 | 49 | $block = $observer->getBlock(); 50 | 51 | //make sure cache is going to apply for a cms block. 52 | if ($block instanceof Mage_Cms_Block_Widget_Block 53 | || $block instanceof Mage_Cms_Block_Block 54 | ) { 55 | 56 | //making a unique cache key for each cms blocks so that cached HTML 57 | //content will be unique per each static block 58 | $cacheKeyData = array( 59 | Mage_Cms_Model_Block::CACHE_TAG, 60 | $block->getBlockId(), 61 | Mage::app()->getStore()->getId(), 62 | intval(Mage::app()->getStore()->isCurrentlySecure()), 63 | Mage::getDesign()->getPackageName(), 64 | Mage::getDesign()->getTheme('template') 65 | //Mage::helper('rkt_sbcache')->randomString() // UNCOMMENT IF IT IS NECESSARY 66 | ); 67 | $block->setCacheKey(implode('_', $cacheKeyData)); 68 | 69 | //set cache tags. This will help us to clear the cache related to 70 | //a static block based on store, CMS cache, or by identifier. 71 | $block->setCacheTags(array( 72 | Mage_Core_Model_Store::CACHE_TAG, 73 | Mage_Cms_Model_Block::CACHE_TAG, 74 | (string)$block->getBlockId() 75 | )); 76 | 77 | //setting cache life time to default. ie 7200 seconds(2 hrs). 78 | //an integer value in seconds. eg : 86400 for one day cache 79 | $block->setCacheLifetime($this->_getHelper()->getCacheTimeout()); 80 | } 81 | 82 | } 83 | 84 | /** 85 | * @return Rkt_SbCache_Helper_Data 86 | */ 87 | protected function _getHelper() 88 | { 89 | return Mage::helper('rkt_sbcache'); 90 | } 91 | 92 | } 93 | --------------------------------------------------------------------------------