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');
if(!$step || ($step && $step != 3 && $step != 4)) {
$id_lang = $cookie->id_lang;
$id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_'.$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+');
$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;
$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;
fwrite($file, json_encode($best_sellers));
fclose($file);
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, 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(
@ -145,34 +161,53 @@ class BlockBestSellers extends Module
return false;
}
global $smarty;
global $cookie;
global $smarty, $cookie;
$id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_cart_'.$id_lang.'.txt';
$expire = time() -3600;
$currency = new Currency((int)($params['cookie']->id_currency));
if(file_exists($filename) && filemtime($filename) > $expire) {
$data = file_get_contents($filename);
$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'))
return;
$best_sellers = array();
if($bestsellers)
foreach ($bestsellers AS $bestseller)
{
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
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;
}
fwrite($file, json_encode($best_sellers));
fclose($file);
$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;
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(
array(
@ -193,29 +228,47 @@ class BlockBestSellers extends Module
global $cookie;
$id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_home_'.$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+');
$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;
$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;
fwrite($file, json_encode($best_sellers));
fclose($file);
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, 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(
@ -236,31 +289,47 @@ class BlockBestSellers extends Module
global $cookie;
$id_lang = $cookie->id_lang;
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_productfooter_'.$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+');
$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;
$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;
fwrite($file, json_encode($best_sellers));
fclose($file);
}
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, 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(
array(

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;
$smarty->assign(array(
'menus' => $menus
));
return $this->display(__FILE__, 'menu.tpl');
}
public function hookPrivateSales_update($params)