You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

41 lines
1.1 KiB

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,
Video,
VideoCategory,
VideoCollection,
VideoPlaylist,
VideoPlaylistInCollection,
)
TARGET_MODELS = [
VideoCategory,
VideoCollection,
Video,
VideoPlaylist,
VideoPlaylistInCollection,
PlaylistItem,
]
def invalidate_video_cache():
"""
Invalidates all Redis cache keys for video API views.
"""
return invalidate_cache_patterns("*video_api*", "*video_playlists*", "*video_list*")
@receiver(post_save)
@receiver(post_delete)
def clear_video_cache_on_save_or_delete(sender, instance, **kwargs):
if sender in TARGET_MODELS:
invalidate_video_cache()
@receiver(m2m_changed)
def clear_video_cache_on_m2m(sender, instance, action, **kwargs):
if action in ('post_add', 'post_remove', 'post_clear'):
if isinstance(instance, (Video, VideoPlaylist, VideoCollection, VideoCategory)):
invalidate_video_cache()