Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from zxcvbn import zxcvbn
import datetime
from django.core.cache import caches
from contextlib import contextmanager

logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -216,6 +217,13 @@ def patient_category_mask(insuree, target_date):



@contextmanager
def suppress_with_log(*exceptions):
try:
yield
except exceptions as e:
logger.warning(f"Suppressed {type(e).__name__}: {e}")

class CachedManager(models.Manager):
UNIQUE_FIELDS = {'pk', 'id', 'uuid'}
CACHED_FK = {}
Expand Down Expand Up @@ -288,7 +296,8 @@ def _handle_cache_lookup(self, field, value, lookup):
"""Handle cache lookup for exact or in queries."""
if lookup == 'exact':
cache_key = get_cache_key(self.model, self._normalize_value(value))
cached_data = cache.get(cache_key)
with suppress_with_log(Exception):
cached_data = cache.get(cache_key)
if cached_data:
if isinstance(cached_data, dict):
# Instantiate from dict to ensure proper __init__ and dirtyfields state
Expand All @@ -313,7 +322,8 @@ def _handle_cache_lookup(self, field, value, lookup):
return None
values = [self._normalize_value(v) for v in value]
cache_keys = [get_cache_key(self.model, v) for v in values]
cached_results = cache.get_many(cache_keys)
with suppress_with_log(Exception):
cached_results = cache.get_many(cache_keys)
cached_instances = []
uncached_values = []

Expand Down Expand Up @@ -479,7 +489,8 @@ def update_cache(self):
Updates the cache for this object after saving.
"""
if self.USE_CACHE:
cache.set(get_cache_key(self.__class__, self.pk), clean_fk(self), timeout=settings.CACHE_OBJECT_TTL)
with suppress_with_log(Exception):
cache.set(get_cache_key(self.__class__, self.pk), clean_fk(self), timeout=settings.CACHE_OBJECT_TTL)

unique_fields = getattr(self.__class__.objects, 'UNIQUE_FIELDS', {'id', 'uuid', 'pk'})
for f in unique_fields:
Expand All @@ -494,7 +505,8 @@ def delete_cache(self):
"""
if self.USE_CACHE:
cache_key = f"{self.__class__.__name__}:{self.pk}"
cache.delete(cache_key)
with suppress_with_log(Exception):
cache.delete(cache_key)
logger.debug(f"Removed instance from cache: {cache_key}")

class ExtendedConnection(graphene.Connection):
Expand Down Expand Up @@ -776,7 +788,8 @@ def _load_config_function(cls, function_name, path):


def clear_cache(instance):
cache.delete(get_cache_key(instance.__class__, instance.pk))
with suppress_with_log(Exception):
cache.delete(get_cache_key(instance.__class__, instance.pk))


def get_cache_key(model, id):
Expand Down
Loading