@ -8,10 +8,30 @@ from .models import *
# 1. Define all models that affect the list
# 1. Define all models that affect the list
# If a Category title changes, the list must update.
# If a Category title changes, the list must update.
# If a Status changes, the list must update.
# If a Status changes, the list must update.
TARGET_MODELS = [ Hadis , HadisCategory , HadisCollection , HadisCorrection , HadisInCollection , HadisReference ,
HadisSect , HadisStatus , HadisTag , HadisTransmitter , Transmitters , TransmitterOpinion , TransmitterReliability ,
TransmitterOriginalText , ReferenceImage , BookReference , BookAttribute , BookAuthor , BookReferenceImage ,
NarratorLayer , OpinionStatus ]
TARGET_MODELS = [
Hadis , HadisCategory , HadisCollection , HadisCorrection , HadisInCollection , HadisReference ,
HadisSect , HadisStatus , HadisTag , HadisTransmitter , Transmitters , TransmitterOpinion , TransmitterReliability ,
TransmitterOriginalText , ReferenceImage , BookReference , BookAttribute , BookAuthor , BookReferenceImage ,
NarratorLayer , OpinionStatus , ContentRelease
]
def invalidate_hadis_cache ( ) :
"""
Deletes all Redis cache keys matching ' *hadis_api* ' .
Falls back to cache . clear ( ) if cache backend doesn ' t support delete_pattern.
"""
try :
if hasattr ( cache , ' delete_pattern ' ) :
deleted = cache . delete_pattern ( " *hadis_api* " )
print ( f " Cache cleared for hadis_api (deleted {deleted} keys)! " )
else :
cache . clear ( )
print ( " Cache cleared for hadis_api (fallback clear)! " )
except Exception as e :
# Fail silently or log error, don't crash the transaction
print ( f " Cache clear failed: {e} " )
@receiver ( post_save )
@receiver ( post_save )
@receiver ( post_delete )
@receiver ( post_delete )
@ -20,14 +40,5 @@ def clear_hadis_cache(sender, instance, **kwargs):
Clears the API cache whenever a Hadith or related model is saved / deleted .
Clears the API cache whenever a Hadith or related model is saved / deleted .
"""
"""
if sender in TARGET_MODELS :
if sender in TARGET_MODELS :
# This is the magic command from django-redis
# It finds ALL keys starting with the prefix and deletes them
# *:1: is the default django version prefix
try :
# Delete any key that contains our prefix "hadis_api"
# The pattern "*hadis_api*" ensures we catch all variations (headers, pages, etc)
cache . delete_pattern ( " *hadis_api* " )
print ( f " Cache cleared for {sender.__name__} update! " )
except Exception as e :
# Fail silently or log error, don't crash the save transaction
print ( f " Cache clear failed: {e} " )
invalidate_hadis_cache ( )
print ( f " Cache cleared for {sender.__name__} update! " )