from django.db.models.signals import post_save, post_delete, m2m_changed from django.dispatch import receiver from utils.cache import invalidate_cache_patterns from .models import ( PlaylistItem, Podcast, PodcastCategory, PodcastCollection, PodcastPlaylist, PodcastPlaylistInCollection, ) TARGET_MODELS = [ PodcastCategory, PodcastCollection, Podcast, PodcastPlaylist, PodcastPlaylistInCollection, PlaylistItem, ] def invalidate_podcast_cache(): """ Invalidates all Redis cache keys for podcast API views. """ return invalidate_cache_patterns("*podcast_api*", "*podcast_list*") @receiver(post_save) @receiver(post_delete) def clear_podcast_cache_on_save_or_delete(sender, instance, **kwargs): if sender in TARGET_MODELS: invalidate_podcast_cache() @receiver(m2m_changed) def clear_podcast_cache_on_m2m(sender, instance, action, **kwargs): if action in ('post_add', 'post_remove', 'post_clear'): if isinstance(instance, (Podcast, PodcastPlaylist, PodcastCollection, PodcastCategory)): invalidate_podcast_cache()