我注意到我的主页花了很长时间来加载--实际上根据site24x7.com,超过6秒,所以我一直在关闭元素,试图确定原因是什么,这是我制作的两个产品集合文件,以显示新产品和畅销产品。
一旦我从主页中删除这些内容,页面就会在不到.5秒的时间内加载。
那么,有谁可以帮助优化和缓存productCollection呢?我已经在服务器上安装并运行了APC,但我不确定它是否缓存了位于app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml中的文件。
所以,我的收藏号召畅销(实际上观看次数最多)是这样的;
<?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)
?>
我怎样才能进一步优化它呢?
发布于 2013-03-14 20:52:44
试一试
$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));
}
看见
发布于 2017-01-11 16:12:41
如果你想缓存$collection,Magento已经内置了集合缓存的可能性。
$_productCollection= Mage::getResourceModel('reports/product_collection');
$cache = Mage::app()->getCache(); //Let's get cache instance
$cache->setLifetime(86400); //Here we set collection cache lifetime
$_productCollection->initCache(
$cache,
'Bestsellers_', //this is just custom prefix
array('collections')
);
}
以上代码的作者: apiworks.net (http://www.apiworks.net/2015/01/magento-collection-caching.html)
https://stackoverflow.com/questions/15408003
复制相似问题