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:
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

View File

@ -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

View File

@ -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