diff --git a/policy/models.py b/policy/models.py index 2e13faa..b98cb3b 100644 --- a/policy/models.py +++ b/policy/models.py @@ -4,7 +4,9 @@ from core import models as core_models from core.utils import filter_validity from core.models import Officer -from django.core.cache import cache +from django.core.cache import caches +cache = caches['coverage'] +from django_redis.cache import RedisCache from django.conf import settings from django.db import models @@ -205,23 +207,26 @@ class Meta: @receiver(post_delete, sender=Claim) def clean_enquire_cache_claim(sender, instance, *args, **kwagrs): cache.delete( - f"eligibility_{instance.insuree.family_id or instance.insuree.id}_*" + f"eligibility_{instance.insuree.family_id or instance.insuree.id}" ) @receiver(post_save, sender=Product) @receiver(post_delete, sender=Product) def clean_all_enquire_cache_product(sender, instance, *args, **kwagrs): - cache.delete("eligibility_*") + if isinstance(cache, RedisCache): + cache.delete("eligibility_*") + else: + cache.clear() @receiver(post_save, sender=Policy) @receiver(post_delete, sender=Policy) def clean_all_enquire_cache_policy(sender, instance, *args, **kwagrs): - cache.delete(f"eligibility_{instance.family_id}_*") + cache.delete(f"eligibility_{instance.family_id}") @receiver(post_save, sender=Family) @receiver(post_delete, sender=Family) def clean_all_enquire_cache_family(sender, instance, *args, **kwagrs): - cache.delete(f"eligibility_{instance.id}_*") + cache.delete(f"eligibility_{instance.id}") diff --git a/policy/services.py b/policy/services.py index da9e7f5..fff682a 100644 --- a/policy/services.py +++ b/policy/services.py @@ -1,7 +1,7 @@ import logging from dataclasses import dataclass from datetime import datetime as py_datetime, date as py_date -from django.core.cache import cache +from django.core.cache import caches import core from claim.models import Claim, ClaimItem @@ -40,6 +40,7 @@ def reset_policy_before_update(policy): policy.family_id = None policy.officer_id = None +cache = caches['coverage'] class PolicyService: def __init__(self, user): @@ -916,10 +917,12 @@ def get_total_filter(category): eligibility.item_left = items_left # InsPol -> Policy -> Product -> dedrem - result = cache.get( - f"eligibility_{insuree.family_id or insuree.id}_{insuree.id}" + cached_data = cache.get( + f"eligibility_{insuree.family_id or insuree.id}" ) - if not result: + if cached_data and str(insuree.id) in cached_data: + result = cached_data[str(insuree.id)] + else: result = ( InsureePolicy.objects.filter( insuree=insuree, @@ -1023,8 +1026,11 @@ def get_total_filter(category): .order_by("-expiry_date") .first() ) + if not cached_data: + cached_data = {} + cached_data[str(insuree.id)] = result cache.set( - f"eligibility_{insuree.family_id or insuree.id}_{insuree.id}", + f"eligibility_{insuree.family_id or insuree.id}", result, None, )