Skip to content

Commit

Permalink
use cov cache
Browse files Browse the repository at this point in the history
  • Loading branch information
delcroip committed Oct 21, 2024
1 parent 216549a commit c439a64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 10 additions & 5 deletions policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
16 changes: 11 additions & 5 deletions policy/services.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit c439a64

Please sign in to comment.