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.
39 lines
1.0 KiB
39 lines
1.0 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 (
|
|
Article,
|
|
ArticleCategory,
|
|
ArticleCollection,
|
|
ArticleContent,
|
|
ArticleInCollection,
|
|
)
|
|
|
|
TARGET_MODELS = [
|
|
ArticleCategory,
|
|
ArticleCollection,
|
|
Article,
|
|
ArticleContent,
|
|
ArticleInCollection,
|
|
]
|
|
|
|
|
|
def invalidate_article_cache():
|
|
"""
|
|
Invalidates all Redis cache keys for article API views.
|
|
"""
|
|
return invalidate_cache_patterns("*article_api*", "*article_list*")
|
|
|
|
|
|
@receiver(post_save)
|
|
@receiver(post_delete)
|
|
def clear_article_cache_on_save_or_delete(sender, instance, **kwargs):
|
|
if sender in TARGET_MODELS:
|
|
invalidate_article_cache()
|
|
|
|
|
|
@receiver(m2m_changed)
|
|
def clear_article_cache_on_m2m(sender, instance, action, **kwargs):
|
|
if action in ('post_add', 'post_remove', 'post_clear'):
|
|
if isinstance(instance, (Article, ArticleCollection, ArticleCategory)):
|
|
invalidate_article_cache()
|