From 5a7d2e35af6b15e06d18b35218ff1f7fe5bf923c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 1 Mar 2018 18:29:46 +0100 Subject: Fix cache menu for all users (refs #3746) --- ishtar_common/menus.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'ishtar_common/menus.py') diff --git a/ishtar_common/menus.py b/ishtar_common/menus.py index e00c6fbef..7cc7b751e 100644 --- a/ishtar_common/menus.py +++ b/ishtar_common/menus.py @@ -72,22 +72,43 @@ class Menu: self.session = session self.items_by_idx = {} - def set_menu_updated_key(self): - cache_key = u"{}-{}".format( - settings.PROJECT_SLUG, 'menu_updated') + def reinit_menu_for_all_user(self): + lst_cache_key = u"{}-{}".format( + settings.PROJECT_SLUG, 'menu_updated_list', + ) + lst_ids = cache.get(lst_cache_key) + if not lst_ids: + return + for idx in lst_ids: + self.init(idx, force=True) + + def set_menu_updated_key(self, cache_key, user_id): + lst_cache_key = u"{}-{}".format( + settings.PROJECT_SLUG, 'menu_updated_list', + ) + lst_ids = cache.get(lst_cache_key) + if not lst_ids: + lst_ids = [] + if not lst_ids or user_id not in lst_ids: + lst_ids.append(user_id) + cache.set(lst_cache_key, lst_ids, settings.CACHE_TIMEOUT) + time = unicode(datetime.datetime.now().isoformat()) cache.set(cache_key, time, settings.CACHE_TIMEOUT) self.initialized = time - def init(self, force=False): - cache_key = u"{}-{}".format( - settings.PROJECT_SLUG, 'menu_updated') + def init(self, user_id=0, force=False): + if not user_id and self.user: + user_id = self.user.pk + cache_key = u"{}-{}-{}".format( + settings.PROJECT_SLUG, 'menu_updated', + user_id + ) menu_updated = cache.get(cache_key) if not force and menu_updated and self.initialized \ and self.initialized == menu_updated: return - menu_updated = cache.get(cache_key) - self.set_menu_updated_key() + self.set_menu_updated_key(cache_key, user_id) self.items = {} self.items_by_idx = {} for idx, main_menu in enumerate(self.childs): -- cgit v1.2.3