diff --git a/openbook_insights/models/insight.py b/openbook_insights/models/insight.py index c065987111024e0f0c368736d34a60e3f64b43a1..cfcbb4633aa079ac331816645914485961f1e158 100644 --- a/openbook_insights/models/insight.py +++ b/openbook_insights/models/insight.py @@ -1,3 +1,5 @@ +import structlog +from django.core.cache import cache from django.conf import settings from django.core.files.storage import default_storage from django.db import models @@ -15,6 +17,8 @@ from openbook_polls.models import Poll insight_image_storage = default_storage +logger = structlog.get_logger(__name__) + class Insight(ModelWithUUID): title = models.CharField(max_length=settings.INSIGHT_TITLE_MAX_LENGTH, blank=False) @@ -62,6 +66,12 @@ class Insight(ModelWithUUID): ] def save(self, *args, **kwargs): + # Attempt to delete the cache matching the pattern + try: + cache.delete_pattern("*feed_insights*") + except Exception as e: + # Log the error but don't raise an exception + logger.warning(f"Insights cache invalidation failed: {e}") if self.header_image: self.header_image_blurhash = generate_blurhash(self.header_image) super().save(*args, **kwargs)