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,31 +104,47 @@ class BlockBestSellers extends Module
$step = Tools::getValue('step'); $step = Tools::getValue('step');
if(!$step || ($step && $step != 3 && $step != 4)) { 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));
$id_lang = $cookie->id_lang; if (class_exists('CacheRedis')) {
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_'.$id_lang.'.txt'; $query_cache_name = 'blockbestsellers_'.(int) $cookie->id_lang;
$expire = time() -3600; 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(file_exists($filename) && filemtime($filename) > $expire) { if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
$data = file_get_contents($filename); return;
$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;
$best_sellers = array();
if($bestsellers)
foreach ($bestsellers AS $bestseller)
{
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
} }
$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;
fwrite($file, json_encode($best_sellers)); if(file_exists($filename) && filemtime($filename) > $expire) {
fclose($file); $data = file_get_contents($filename);
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$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;
}
fwrite($file, json_encode($best_sellers));
fclose($file);
}
} }
$smarty->assign(array( $smarty->assign(array(
@ -145,34 +161,53 @@ class BlockBestSellers extends Module
return false; return false;
} }
global $smarty; global $smarty, $cookie;
global $cookie;
$id_lang = $cookie->id_lang; $id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_cart_'.$id_lang.'.txt'; $currency = new Currency((int)($params['cookie']->id_currency));
$expire = time() -3600;
if(file_exists($filename) && filemtime($filename) > $expire) { if (class_exists('CacheRedis')) {
$data = file_get_contents($filename); $query_cache_name = 'blockbestsellers_cart_'.(int) $cookie->id_lang;
$best_sellers = json_decode($data, TRUE); if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
} else { $bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
$file = fopen($filename, 'w+'); if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
$currency = new Currency((int)($params['cookie']->id_currency)); return;
$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;
} }
fwrite($file, json_encode($best_sellers)); $best_sellers = array();
fclose($file); 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;
if(file_exists($filename) && filemtime($filename) > $expire) {
$data = file_get_contents($filename);
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$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;
}
fwrite($file, json_encode($best_sellers));
fclose($file);
}
} }
$smarty->assign( $smarty->assign(
array( array(
@ -193,29 +228,47 @@ class BlockBestSellers extends Module
global $cookie; global $cookie;
$id_lang = $cookie->id_lang; $id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_home_'.$id_lang.'.txt'; $currency = new Currency((int)($params['cookie']->id_currency));
$expire = time() -3600; if (class_exists('CacheRedis')) {
$query_cache_name = 'blockbestsellers_home_'.(int) $cookie->id_lang;
if(file_exists($filename) && filemtime($filename) > $expire) { if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
$data = file_get_contents($filename); $bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
$best_sellers = json_decode($data, TRUE); if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
} else { return;
$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;
$best_sellers = array();
if($bestsellers)
foreach ($bestsellers AS $bestseller)
{
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
} }
$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;
fwrite($file, json_encode($best_sellers)); if(file_exists($filename) && filemtime($filename) > $expire) {
fclose($file); $data = file_get_contents($filename);
$best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$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;
}
fwrite($file, json_encode($best_sellers));
fclose($file);
}
} }
$smarty->assign( $smarty->assign(
array( array(
@ -236,31 +289,47 @@ class BlockBestSellers extends Module
global $cookie; global $cookie;
$id_lang = $cookie->id_lang; $id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency));
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_productfooter_'.$id_lang.'.txt'; if (class_exists('CacheRedis')) {
$expire = time() -3600; $query_cache_name = 'blockbestsellers_productfooter_'.(int) $cookie->id_lang;
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
if(file_exists($filename) && filemtime($filename) > $expire) { $bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
$data = file_get_contents($filename); if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
$best_sellers = json_decode($data, TRUE); return;
} 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;
$best_sellers = array();
if($bestsellers)
foreach ($bestsellers AS $bestseller)
{
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
} }
$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;
fwrite($file, json_encode($best_sellers)); if(file_exists($filename) && filemtime($filename) > $expire) {
fclose($file); $data = file_get_contents($filename);
} $best_sellers = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$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;
}
fwrite($file, json_encode($best_sellers));
fclose($file);
}
$smarty->assign( $smarty->assign(
array( array(

View File

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