Magento如何缓存productCollection [英] Magento how to cache a productCollection
问题描述
我注意到我的主页加载时间很长-根据site24x7.com的情况,实际上已超过6秒,因此我一直在关闭元素以尝试确定原因,并且归结为2个产品收集文件已经展示了新产品和最畅销的产品.
Ive noticed my home page is taking a long time to load - over 6 seconds infact according site24x7.com, so ive been switching elements off to try and determine what is the cause, and it is down to 2 product collection files I have made to show new products and best selling products.
我从首页中删除这些内容后,该页面的加载时间不到0.5秒.
As soon as i remove these from the home page, the page loads in less than .5 seconds.
那么,有人可以帮助优化和缓存productCollection吗?我已经在服务器上安装并运行了APC,但是我不确定它是否正在缓存位于app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml
So, can anyone help with optimising and caching a productCollection? I have APC installed and running on the server, but Im not sure it is caching the files located in app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml
因此,我的畅销书征集活动(实际上是观看最多的人)看起来像这样;
So, my collection call for best selling (most viewed actually) looks like this;
<?php $storeId = Mage::app()->getStore()->getId(); // return current store id ?>
<?php $_productCollection= Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addStoreFilter($storeId)
->addViewsCount()
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$_productCollection->getSelect()->limit(8)
?>
如何进一步优化呢?
推荐答案
尝试
$storeId = Mage::app()->getStore()->getId();
$cache = Mage::getSingleton('core/cache');
$key = 'homepage-most-view-' . $storeId;
if(! $data = $cache->load($key)){
$_productCollection= Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addStoreFilter($storeId)
->addViewsCount()
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$_productCollection->getSelect()->limit(8)
// get the element you need from $_productCollection and store in $array
$data = serialize($array);
$cache->save(urlencode($data), $key, array("homepage_cache"), 60*60*24);
}
else{
$data = unserialize(urldecode($data));
}
请参见
- http://www.nicksays.co.uk/developers-guide -magento-cache/
- http://inchoo.net/ecommerce/magento/magento-block-caching /
- http://www.nicksays.co.uk/developers-guide-magento-cache/
- http://inchoo.net/ecommerce/magento/magento-block-caching/
这篇关于Magento如何缓存productCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!