コード例 #1
0
 /**
  * Apply cache settings to block
  * @param Mage_Core_Block_Abstract $block
  */
 function applyCacheSettings(&$block)
 {
     $store = Mage::app()->getStore();
     $filterUrl = $this->getFilterUrl();
     if (!Mage::getStoreConfigFlag(self::CONFIG_SECTION . '/general/cache_when_url_param') && false !== strpos($filterUrl, '?')) {
         // Caching of page with url param disabled in config, and this page has them.
         return;
     }
     if (!Mage::getStoreConfigFlag(self::CONFIG_SECTION . '/general/cache_when_customer_logged_in') && Mage::getSingleton('customer/session')->isLoggedIn()) {
         // Caching when customer logged in disabled in config, and customer is logged in.
         return;
     }
     $blockGroup = false;
     /** @noinspection PhpUndefinedClassInspection */
     if ($block instanceof Mage_Catalog_Block_Category_View || $block instanceof JoomlArt_JmProducts_Block_List) {
         $blockGroup = self::BLOCK_GROUP_CATEGORY;
     } elseif ($block instanceof Mage_Catalog_Block_Product_View) {
         $blockGroup = self::BLOCK_GROUP_PRODUCT;
     } elseif ($block instanceof Mage_Catalog_Block_Layer_View || $block instanceof Emico_Tweakwise_Block_Catalog_Layer_View || $block instanceof Mana_Filters_Block_View) {
         $blockGroup = self::BLOCK_GROUP_LAYERED_NAV;
     } elseif ($block instanceof Mage_Cms_Block_Page) {
         $blockGroup = self::BLOCK_GROUP_CMS_PAGE;
     } elseif ($block instanceof Mage_Cms_Block_Block || $block instanceof Mage_Cms_Block_Widget_Block) {
         $blockGroup = self::BLOCK_GROUP_CMS_BLOCK;
     }
     for ($c = 1; $c <= 5; $c++) {
         $matches = trim(Mage::getStoreConfig(self::CONFIG_SECTION . '/custom_' . $c . '/instanceof'));
         if ($matches) {
             $matches = explode("\n", $matches);
             foreach ($matches as $match) {
                 $match = trim($match);
                 if (empty($match)) {
                     continue;
                 }
                 if (false !== strpos($match, '/')) {
                     $match = Mage::getConfig()->getBlockClassName($match);
                 }
                 if (!empty($match) && $block instanceof $match) {
                     $blockGroup = constant('self::BLOCK_GROUP_CUSTOM_' . $c);
                 }
             }
         }
     }
     if (false === $blockGroup) {
         // It is a block group we don't change caching for.
         return;
     }
     $cacheLifeTime = null;
     if (Mage::getStoreConfigFlag(self::CONFIG_SECTION . '/' . $blockGroup . '/enable_cache')) {
         $cacheLifeTime = intval(Mage::getStoreConfig(self::CONFIG_SECTION . '/' . $blockGroup . '/lifetime'));
     }
     /** @noinspection PhpUndefinedMethodInspection */
     $block->setCacheLifetime($cacheLifeTime);
     if ($cacheLifeTime) {
         $currentCategory = null;
         $currentProduct = null;
         $cacheKey = self::CACHEKEY_START;
         $cacheKeyData = '';
         $cacheTags = array();
         switch ($blockGroup) {
             case self::BLOCK_GROUP_CATEGORY:
                 $cacheKey .= self::CACHEKEY_GROUP_CATEGORY;
                 $currentCategory = Mage::registry('current_category');
                 // Add sorting & paging to cache key
                 $catalogSession = Mage::getSingleton('catalog/session');
                 if ($catalogSession) {
                     /** @noinspection PhpUndefinedMethodInspection */
                     $cacheKeyData .= '|so' . strval($catalogSession->getSortOrder());
                     /** @noinspection PhpUndefinedMethodInspection */
                     $cacheKeyData .= '|sd' . strval($catalogSession->getSortDirection());
                     /** @noinspection PhpUndefinedMethodInspection */
                     $cacheKeyData .= '|dm' . strval($catalogSession->getDisplayMode());
                     /** @noinspection PhpUndefinedMethodInspection */
                     $cacheKeyData .= '|lp' . strval($catalogSession->getLimitPage());
                 }
                 break;
             case self::BLOCK_GROUP_PRODUCT:
                 $cacheKey .= self::CACHEKEY_GROUP_PRODUCT;
                 $currentCategory = Mage::registry('current_category');
                 $currentProduct = Mage::registry('current_product');
                 break;
             case self::BLOCK_GROUP_LAYERED_NAV:
                 $cacheKey .= self::CACHEKEY_GROUP_LAYERED_NAV;
                 $currentCategory = Mage::registry('current_category');
                 break;
             case self::BLOCK_GROUP_CMS_PAGE:
                 $cacheKey .= self::CACHEKEY_GROUP_CMS_PAGE;
                 $cmsPage = Mage::getSingleton('cms/page');
                 if ($cmsPage instanceof Mage_Cms_Model_Page) {
                     $cacheTags[] = Mage_Cms_Model_Page::CACHE_TAG . '_' . $cmsPage->getId();
                     $cacheKey .= 'P' . $cmsPage->getId() . '_';
                 }
                 break;
             case self::BLOCK_GROUP_CMS_BLOCK:
                 /** @noinspection PhpUndefinedMethodInspection */
                 $cacheKey .= self::CACHEKEY_GROUP_CMS_BLOCK;
                 $cacheKeyData .= '|b' . $block->getBlockId();
                 // Example block_id: 'after_body_start'
                 $cacheTags[] = Mage_Cms_Model_Block::CACHE_TAG;
                 break;
             case self::BLOCK_GROUP_CUSTOM_1:
             case self::BLOCK_GROUP_CUSTOM_2:
             case self::BLOCK_GROUP_CUSTOM_3:
             case self::BLOCK_GROUP_CUSTOM_4:
             case self::BLOCK_GROUP_CUSTOM_5:
                 /** @noinspection PhpUndefinedMethodInspection */
                 // We don't know what it exactly is the user configured, so we throw everything in
                 $cacheKey .= constant('self::CACHEKEY_GROUP_' . strtoupper($blockGroup));
                 $currentCategory = Mage::registry('current_category');
                 $currentProduct = Mage::registry('current_product');
                 $allData = $block->getData();
                 unset($allData['cache_lifetime']);
                 $cacheKeyData .= '|D' . json_encode($allData, 0, 3);
                 // All block data, 3 levels max
                 break;
             default:
                 Mage::log("ERROR: %s->%s: Unknown block group '%s'", __CLASS__, __FUNCTION__, $blockGroup);
         }
         if ($currentCategory instanceof Mage_Catalog_Model_Category) {
             $cacheKey .= 'C' . $currentCategory->getId();
         }
         if ($currentProduct instanceof Mage_Catalog_Model_Product) {
             $cacheKey .= 'P' . $currentProduct->getId();
         }
         $cacheKeyData .= $this->getBlockCacheKeyData($block, $store, $currentCategory, $currentProduct);
         $cacheKey .= '_' . md5($cacheKeyData);
         $this->addBlockCacheTags($cacheTags, $currentCategory, $currentProduct);
         $cacheTags = array_unique($cacheTags);
         /** @noinspection PhpUndefinedMethodInspection */
         $block->setCacheKey($cacheKey);
         /** @noinspection PhpUndefinedMethodInspection */
         $block->setCacheTags($cacheTags);
         if (Mage::getStoreConfigFlag(self::CONFIG_SECTION . '/logging/tags')) {
             $message = sprintf('Block %s Tags %s', $cacheKey, $this->logTags($cacheTags));
             $message .= $this->getLogSuffix();
             Mage::log($message, Zend_Log::INFO, self::TAGS_LOG_FILE);
         }
     }
 }
コード例 #2
0
ファイル: AbstractTest.php プロジェクト: nemphys/magento2
 public function testGetCacheLifetime()
 {
     $this->assertNull($this->_block->getCacheLifetime());
     $this->_block->setCacheLifetime(1800);
     $this->assertEquals(1800, $this->_block->getCacheLifetime());
 }