add cache for bestsellers

This commit is contained in:
Marion Muszynski 2017-11-21 14:10:00 +01:00
parent acbc72a32c
commit 15108dcd34
2 changed files with 168 additions and 108 deletions

View File

@ -104,8 +104,24 @@ class BlockBestSellers extends Module
$step = Tools::getValue('step');
if(!$step || ($step && $step != 3 && $step != 4)) {
$id_lang = $cookie->id_lang;
$id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency));
if (class_exists('CacheRedis')) {
$query_cache_name = 'blockbestsellers_'.(int) $cookie->id_lang;
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
return;
}
$best_sellers = array();
if($bestsellers){
foreach ($bestsellers AS $bestseller){
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
}
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_'.$id_lang.'.txt';
$expire = time() -3600;
@ -114,7 +130,6 @@ class BlockBestSellers extends Module
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
return;
@ -130,6 +145,7 @@ class BlockBestSellers extends Module
fwrite($file, json_encode($best_sellers));
fclose($file);
}
}
$smarty->assign(array(
'best_sellers' => $best_sellers,
@ -145,10 +161,29 @@ class BlockBestSellers extends Module
return false;
}
global $smarty;
global $cookie;
global $smarty, $cookie;
$id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency));
if (class_exists('CacheRedis')) {
$query_cache_name = 'blockbestsellers_cart_'.(int) $cookie->id_lang;
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
return;
}
$best_sellers = array();
if($bestsellers){
foreach ($bestsellers AS $bestseller){
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
}
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_cart_'.$id_lang.'.txt';
$expire = time() -3600;
@ -157,7 +192,6 @@ class BlockBestSellers extends Module
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
@ -174,6 +208,7 @@ class BlockBestSellers extends Module
fwrite($file, json_encode($best_sellers));
fclose($file);
}
}
$smarty->assign(
array(
'best_sellers' => $best_sellers,
@ -193,6 +228,24 @@ class BlockBestSellers extends Module
global $cookie;
$id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency));
if (class_exists('CacheRedis')) {
$query_cache_name = 'blockbestsellers_home_'.(int) $cookie->id_lang;
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
return;
}
$best_sellers = array();
if($bestsellers){
foreach ($bestsellers AS $bestseller){
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
}
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_home_'.$id_lang.'.txt';
$expire = time() -3600;
@ -201,7 +254,6 @@ class BlockBestSellers extends Module
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
return;
@ -217,6 +269,7 @@ class BlockBestSellers extends Module
fwrite($file, json_encode($best_sellers));
fclose($file);
}
}
$smarty->assign(
array(
'best_sellers' => $best_sellers,
@ -236,7 +289,24 @@ class BlockBestSellers extends Module
global $cookie;
$id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency));
if (class_exists('CacheRedis')) {
$query_cache_name = 'blockbestsellers_productfooter_'.(int) $cookie->id_lang;
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
return;
}
$best_sellers = array();
if($bestsellers){
foreach ($bestsellers AS $bestseller){
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
}
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_productfooter_'.$id_lang.'.txt';
$expire = time() -3600;
@ -245,7 +315,6 @@ class BlockBestSellers extends Module
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
return;

View File

@ -21,25 +21,17 @@ class Privatesales_Family_Menu extends Module {
}
public function hooktopMenu($params) {
global $cookie, $smarty;
global $cookie, $smarty, $site_version_front;
$id_lang = (int)$cookie->id_lang;
/**
* @Override cache file
*/
if (class_exists('CacheRedis')) {
$tpl_cache_name = 'sale_privatesales_family_menu_'.(int) $cookie->id_lang.'.html';
if (false === ($result = CacheRedis::getInstance()->get($tpl_cache_name, 'sale_privatesales_family_menu'))) {
$query_cache_name = 'sale_privatesales_family_menu_'.(int) $cookie->id_lang.'_'.$site_version_front;
if (false === ($menus = CacheRedis::getInstance()->get($query_cache_name, 'sale_privatesales_family_menu'))) {
$menus = $this->getTree($cookie->id_lang);
if (!$menus) {
CacheRedis::getInstance()->set($tpl_cache_name, 'sale_privatesales_family_menu', '', CacheRedis::MEDIUM_TTL);
return "";
}
$smarty->assign(array(
'menus' => $menus
));
$result = $this->display(__FILE__, 'menu.tpl');
CacheRedis::getInstance()->set($tpl_cache_name, 'sale_privatesales_family_menu', $result, CacheRedis::MEDIUM_TTL);
CacheRedis::getInstance()->set($query_cache_name, 'sale_privatesales_family_menu', $menus, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt';
@ -54,13 +46,12 @@ class Privatesales_Family_Menu extends Module {
fwrite($file, json_encode($menus));
fclose($file);
}
}
$smarty->assign(array(
'menus' => $menus
));
$result = $this->display(__FILE__, 'menu.tpl');
}
return $result;
return $this->display(__FILE__, 'menu.tpl');
}
public function hookPrivateSales_update($params)