Magento如何缓存productCollection
我注意到我的主页需要很长时间才能加载-根据site24x7.com,事实上超过6秒,所以我一直在关闭元素以尝试确定原因,我制作了两个产品集合文件来展示新产品和畅销产品 一旦我从主页上删除这些内容,页面将在不到0.5秒的时间内加载 那么,有人能帮助优化和缓存productCollection吗?我已经在服务器上安装并运行了APC,但我不确定它是否正在缓存app/design/frontend/default/MY_THEME/catalog/product/newproducts.phtml中的文件 因此,我对畅销书(实际上是浏览量最多的)的征集要求如下:Magento如何缓存productCollection,magento,caching,apc,Magento,Caching,Apc,我注意到我的主页需要很长时间才能加载-根据site24x7.com,事实上超过6秒,所以我一直在关闭元素以尝试确定原因,我制作了两个产品集合文件来展示新产品和畅销产品 一旦我从主页上删除这些内容,页面将在不到0.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)
?>
如何进一步优化此功能?试试看
$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));
}
看
$_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()@R.S为什么要使用
urlencode
两次?这是必要的还是一个错误?这不应该是:$data=urlencode(序列化($array))$缓存->保存($data,$key,数组(“主页缓存”),60*60*24)代码>?这是一个打字错误。。修复了nowMage::app()->getCache()代替Mage::getSingleton('core/cache')可以节省大量时间…为什么要进行url编码?我还发现我无法缓存产品集合。我想这就是为什么你要把它存储到一个数组中。magento是否因为产品集合太大而阻止序列化产品集合?在本例中,我了解如何设置它。但是,我们是否需要执行第二次运行来检查缓存是否存在,然后才将其保存到缓存中。。。或者“缓存知道/自己做这件事”thhx!你好,@snh_nl。正如您在以下代码中所看到的,这是Varien_Data_Collection_Db类的一部分:Magento首先尝试从缓存加载,如果失败,则从数据库加载并保存缓存版本。因此,答案是:对于同一个$collection对象,使用上面的代码初始化缓存后,不需要执行任何其他操作。下次会自动加载。我们可以在html模板文件中使用这样的代码吗?或者如何在phtml文件(例如产品列表)中缓存集合调用