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
This commit is contained in:
Oliver 2024-03-12 14:09:28 +11:00 committed by GitHub
parent 8719dd7e1e
commit f5a42172ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 8 deletions

View File

@ -290,8 +290,7 @@ class BaseInvenTreeSetting(models.Model):
try: try:
cache.set(ckey, self, timeout=3600) cache.set(ckey, self, timeout=3600)
except TypeError: except Exception:
# Some characters cause issues with caching; ignore and move on
pass pass
@classmethod @classmethod
@ -554,16 +553,18 @@ class BaseInvenTreeSetting(models.Model):
# Unless otherwise specified, attempt to create the setting # Unless otherwise specified, attempt to create the setting
create = kwargs.pop('create', True) create = kwargs.pop('create', True)
# Perform cache lookup by default
do_cache = kwargs.pop('cache', True)
# Prevent saving to the database during data import # Prevent saving to the database during data import
if InvenTree.ready.isImportingData(): if InvenTree.ready.isImportingData():
create = False create = False
do_cache = False
# Prevent saving to the database during migrations # Prevent saving to the database during migrations
if InvenTree.ready.isRunningMigrations(): if InvenTree.ready.isRunningMigrations():
create = False create = False
do_cache = False
# Perform cache lookup by default
do_cache = kwargs.pop('cache', True)
ckey = cls.create_cache_key(key, **kwargs) ckey = cls.create_cache_key(key, **kwargs)
@ -575,7 +576,7 @@ class BaseInvenTreeSetting(models.Model):
if cached_setting is not None: if cached_setting is not None:
return cached_setting return cached_setting
except AppRegistryNotReady: except Exception:
# Cache is not ready yet # Cache is not ready yet
do_cache = False do_cache = False

View File

@ -14,7 +14,10 @@ def currency_code_default():
"""Returns the default currency code (or USD if not specified).""" """Returns the default currency code (or USD if not specified)."""
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
try:
cached_value = cache.get('currency_code_default', '') cached_value = cache.get('currency_code_default', '')
except Exception:
cached_value = None
if cached_value: if cached_value:
return cached_value return cached_value

View File

@ -717,7 +717,10 @@ def check_user_role(user, role, permission):
# First, check the cache # First, check the cache
key = f'role_{user}_{role}_{permission}' key = f'role_{user}_{role}_{permission}'
try:
result = cache.get(key) result = cache.get(key)
except Exception:
result = None
if result is not None: if result is not None:
return result return result