From f5a42172acf1123d08ea4c7fe44c15d0e7f3505a Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 12 Mar 2024 14:09:28 +1100 Subject: [PATCH] Cache exception handling (#6675) * Improve cache loading for setting - Handle generic exception - Do not cache if importing data * More generic exception handling * Handle more cache exceptions --- InvenTree/common/models.py | 13 +++++++------ InvenTree/common/settings.py | 5 ++++- InvenTree/users/models.py | 5 ++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 9ca83c361a..f061b327eb 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -290,8 +290,7 @@ class BaseInvenTreeSetting(models.Model): try: cache.set(ckey, self, timeout=3600) - except TypeError: - # Some characters cause issues with caching; ignore and move on + except Exception: pass @classmethod @@ -554,16 +553,18 @@ class BaseInvenTreeSetting(models.Model): # Unless otherwise specified, attempt to create the setting create = kwargs.pop('create', True) + # Perform cache lookup by default + do_cache = kwargs.pop('cache', True) + # Prevent saving to the database during data import if InvenTree.ready.isImportingData(): create = False + do_cache = False # Prevent saving to the database during migrations if InvenTree.ready.isRunningMigrations(): create = False - - # Perform cache lookup by default - do_cache = kwargs.pop('cache', True) + do_cache = False ckey = cls.create_cache_key(key, **kwargs) @@ -575,7 +576,7 @@ class BaseInvenTreeSetting(models.Model): if cached_setting is not None: return cached_setting - except AppRegistryNotReady: + except Exception: # Cache is not ready yet do_cache = False diff --git a/InvenTree/common/settings.py b/InvenTree/common/settings.py index 03601ad1f8..a0e888fd8b 100644 --- a/InvenTree/common/settings.py +++ b/InvenTree/common/settings.py @@ -14,7 +14,10 @@ def currency_code_default(): """Returns the default currency code (or USD if not specified).""" from common.models import InvenTreeSetting - cached_value = cache.get('currency_code_default', '') + try: + cached_value = cache.get('currency_code_default', '') + except Exception: + cached_value = None if cached_value: return cached_value diff --git a/InvenTree/users/models.py b/InvenTree/users/models.py index 806f39586d..97723613db 100644 --- a/InvenTree/users/models.py +++ b/InvenTree/users/models.py @@ -717,7 +717,10 @@ def check_user_role(user, role, permission): # First, check the cache key = f'role_{user}_{role}_{permission}' - result = cache.get(key) + try: + result = cache.get(key) + except Exception: + result = None if result is not None: return result