Change the frontpage to use cache and increase performance

Started by gboul, May 31, 2012, 17:22:49 PM

Previous topic - Next topic

gboul

We have developed a new front page for the virtuemart where we present 12-15 products for specific categories. Initially the loading it was too slow and the number of queries executed were around 1500. Even with cache on, there was no change, since the frontpage is not cached (I figure that out looking at its controller).

I changed the frontpage to use cache and we saved 1/10 of the loading time and  at more than 1000 queries.

If anyone is interested, I can post the code with the performance improvement.


sadrem

p.s. Do not forget to change message icon to Solved.

Milbo

Hmmm strange.

I have a testshop with 1500 products and only 230 queries and with cache 30 queries or so.

The amount of products in the store is not really important. More important is the actual displayed number of products and the activated modules and other extensions. but of course post it, we are always eager to learn.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

gboul

Milbo,
I was not very clear in my post, I was not suggesting that the current implementation was producing high number queries, but our revised version of the view. We have developed a new front page where we load 15 featured books from 5 categories. You can see the result in www.myebooks.gr.
I changed the components/com_virtuemart/views/virtuemart/view.html.php in order to have these books and their relative info stored in the $this->products.

Here is our revised version of the view, changed to our requirements.

public function display($tpl = null) {

/* MULTI-X
* $this->loadHelper('vendorHelper');
* $vendorModel = new Vendor;
* $vendor = $vendorModel->getVendor();
* $this->assignRef('vendor', $vendor);
*/

$vendorId = JRequest::getInt('vendorid', 1);

$vendorModel = VmModel::getModel('vendor');

$vendorModel->setId(1);
$vendor = $vendorModel->getVendor();
$this->assignRef('vendor',$vendor);

if(!VmConfig::get('shop_is_offline',0)){
$layout = VmConfig::get('vmlayout','default');

if ($layout == "default") {
$categoryModel = VmModel::getModel('category');
$productModel = VmModel::getModel('product');
$products = array();
$categoryId = JRequest::getInt('catid', 0);
$cache = JFactory::getCache('com_virtuemart','callback');

$categoryChildren = $cache->call( array( 'VirtueMartModelCategory', 'getChildCategoryList' ),$vendorId, $categoryId );
// self::$categoryTree = self::categoryListTreeLoop($selectedCategories, $cid, $level, $disabledFields);

//$categoryChildren = $categoryModel->getChildCategoryList($vendorId, $categoryId);

//$categoryChildren = $categoryModel->getChildCategoryList($vendorId, $categoryId);
$categoryModel->addImages($categoryChildren,1);

$this->assignRef('categories', $categoryChildren);

if(!class_exists('CurrencyDisplay'))require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'currencydisplay.php');
$currency = CurrencyDisplay::getInstance( );
$this->assignRef('currency', $currency);

if (VmConfig::get('show_featured', 1)) {
$products['featured'] = $productModel->getProductListing('featured', 5);
$productModel->addImages($products['featured'],1);
}

if (VmConfig::get('show_latest', 1)) {
$products['latest']= $productModel->getProductListing('latest', 5);
$productModel->addImages($products['latest'],1);
}

if (VmConfig::get('show_topTen', 1)) {
$products['topten']= $productModel->getProductListing('topten', 5);
$productModel->addImages($products['topten'],1);
}
$this->assignRef('products', $products);

if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
$showBasePrice = Permissions::getInstance()->check('admin'); //todo add config settings
$this->assignRef('showBasePrice', $showBasePrice);
} else {
//QNR Overriding Shopfront - Remove Categories
# load categories from front_categories if exist
# if ($this->categories) echo $this->loadTemplate('categories');
//QNR Read product list for promo categories
$productModel = VmModel::getModel('product');
$products = array();

//echo "<p>". $cache->getCaching()."</p>";
if(!class_exists('CurrencyDisplay'))require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'currencydisplay.php');
$currency = CurrencyDisplay::getInstance( );
$this->assignRef('currency', $currency);

$lang = JFactory::getLanguage();
$cache = JFactory::getCache('com_virtuemart_promo', 'output');
//$cache->setCaching(true);
//$profiler = new JProfiler();

$products = $cache->get('cache_promo_products_id'.$lang, 'com_virtuemart_promo');
if (!$products) {
//echo "uncached";
$products['NOVELS'] = $productModel->getProductListing('featured', 15, true, true, false, true, 2);
$productModel->addImages($products['NOVELS'],1);
$products['PROOF'] = $productModel->getProductListing('featured', 15, true, true, false, true, 3);
$productModel->addImages($products['PROOF'],1);
$products['CHILDREN'] = $productModel->getProductListing('featured', 15, true, true, false, true, 8);
$productModel->addImages($products['CHILDREN'],1);
$products['GENERAL'] = $productModel->getProductListing('featured', 15, true, true, false, true, 11);
$productModel->addImages($products['GENERAL'],1);
$products['EDUCATIONAL'] = $productModel->getProductListing('featured', 15, true, true, false, true, 49);
$productModel->addImages($products['EDUCATIONAL'],1);

$cache->store($products, 'cache_promo_products_id'.$lang, 'com_virtuemart_promo');
}
//echo $profiler->mark( ' with caching' );

$this->assignRef('products', $products);
}
// $layoutName = VmConfig::get('vmlayout','default');

$this->setLayout($layout);

} else {
$this->setLayout('off_line');
}

# Set the titles
$document = JFactory::getDocument();

$error = JRequest::getInt('error',0);

//Todo this may not work everytime as expected, because the error must be set in the redirect links.
if(!empty($error)){
/* $head = $document->getHeadData();
$head['title'] = JText::_('COM_VIRTUEMART_PRODUCT_NOT_FOUND');
$document->setHeadData($head);*/
$document->setTitle(JText::_('COM_VIRTUEMART_PRODUCT_NOT_FOUND').JText::sprintf('COM_VIRTUEMART_HOME',$vendor->vendor_store_name));
} else {
$document->setTitle(JText::sprintf('COM_VIRTUEMART_HOME',$vendor->vendor_store_name));
}

$template = VmConfig::get('vmtemplate','default');
if (is_dir(JPATH_THEMES.DS.$template)) {
$mainframe = JFactory::getApplication();
$mainframe->set('setTemplate', $template);
}



parent::display($tpl);

}


We added an else in the layout check, since we are not using the default layout, where we get the fetured products from 5 categories that we display in our front page.