From 446308d40c16e67fb83dbaadabf5f7f5ddc1ce65 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Mon, 20 Nov 2017 17:12:58 +0100 Subject: [PATCH] cache privatesale menu --- .../privatesales_family_menu.php | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/modules/privatesales_family_menu/privatesales_family_menu.php b/modules/privatesales_family_menu/privatesales_family_menu.php index c533a762..da847619 100644 --- a/modules/privatesales_family_menu/privatesales_family_menu.php +++ b/modules/privatesales_family_menu/privatesales_family_menu.php @@ -24,30 +24,51 @@ class Privatesales_Family_Menu extends Module { global $cookie, $smarty; $id_lang = (int)$cookie->id_lang; - - $filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt'; - $expire = time() -3600; - - if(file_exists($filename) && filemtime($filename) > $expire) { - $data = file_get_contents($filename); - $menus = json_decode($data, TRUE); + /** + * @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, 'privatesales_family_menu'))) { + $menus = $this->getTree($cookie->id_lang); + if (!$menus) { + CacheRedis::getInstance()->set($tpl_cache_name, 'privatesales_family_menu', '', CacheRedis::MEDIUM_TTL); + return ""; + } + $smarty->assign(array( + 'menus' => $menus + )); + $result = $this->display(__FILE__, 'menu.tpl'); + CacheRedis::getInstance()->set($tpl_cache_name, 'privatesales_family_menu', $result, CacheRedis::MEDIUM_TTL); + } } else { - $file = fopen($filename, 'w+'); - $menus = $this->getTree($cookie->id_lang); - fwrite($file, json_encode($menus)); - fclose($file); - } + $filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt'; + $expire = time() -3600; - $smarty->assign(array( - 'menus' => $menus - )); - return $this->display(__FILE__, 'menu.tpl'); + if(file_exists($filename) && filemtime($filename) > $expire) { + $data = file_get_contents($filename); + $menus = json_decode($data, TRUE); + } else { + $file = fopen($filename, 'w+'); + $menus = $this->getTree($cookie->id_lang); + fwrite($file, json_encode($menus)); + fclose($file); + } + + $smarty->assign(array( + 'menus' => $menus + )); + $result = $this->display(__FILE__, 'menu.tpl'); + } + return $result; } public function hookPrivateSales_update($params) { + if (class_exists('CacheRedis')) { + CacheRedis::getInstance()->clear('sale_privatesales*'); + } $id_langs = array(2,3); - foreach ($id_langs as $key => $id_lang) { $filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt';