Hi,
we can cache the block and also we can cache the particular HTML block code
1st time it will cache and from next time it will call from cache
for eg:- in product view page your list a information in for each loop so each time it will kill your performance so 1st time cache and next time load it from cache
we can cache in 2 ways
1. Cache a Block
in block php add below code ex List.php
public function __construct()
{
$this->addData(array(
'cache_lifetime' => 1800,
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
'cache_key' => $this->getCacheKey()
));
}
public function getCacheKey()
{
return $this->getRequest()->getRequestUri().$this->getCacheCurrencyCode();
}
//retreive current currency code
public function getCacheCurrencyCode()
{
return Mage::app()->getStore()->getCurrentCurrencyCode();
}
2. cache a particular html block (only html code will cache it will not cache any objects )
you can do below logic even by creating new block file and can call it in product view page if you dont want to create new block for this small things you can cache the information
<?php
$cache = Mage::getSingleton('core/cache');
$cacheTag = array(
Mage_Catalog_Model_Product::CACHE_TAG,
);
$currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$storeId = Mage::app()->getStore()->getId();
$plantationkey=str_replace(' ', '_', $plantation);
$cacheKey = 'Origin_' .$plantationkey . $storeId .$currentCurrencyCode;
$orginhtml = $cache->load($cacheKey);
if(!empty($orginhtml)){
echo $orginhtml;
}else{
$theProductBlock = new Mage_Catalog_Block_Product;
$Bestsellerproducts = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addFieldToFilter(array(array('attribute'=>'bestseller','in'=>1)))
->addAttributeToFilter('visibility', $this->visibility)
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addCategoryFilter($category); ?>
<?php if(count($Bestsellerproducts)):?>
<?php
$orginhtml ='<ul>';
foreach($Bestsellerproducts as $bestseller){
$orginhtml .='<li>
<h2>'.$bestseller->getName().'</h2>
<a href="'.$bestseller->getProductUrl().'" class="product-img"><img src="'.$this->helper('catalog/image')->init($bestseller, 'small_image')->resize(229).'" width="229" height="229" alt="'.$this->stripTags($this->getImageLabel($bestseller, 'small_image'), null, true).'" title="'.$bestseller->getName().'" /></a>
<h2 class="product-name"><a href="'.$bestseller->getProductUrl().'">'.$bestseller->getName().'</a>
</li>';
}
endif; ?>
<?php echo $orginhtml; ?>
<?php $cache->save($orginhtml, $cacheKey, $cacheTag, 3600); ?>
<?php } ?>
http://www.magentocommerce.com/boards/viewthread/700444/
No comments:
Post a Comment