From 32d38bcce36924fc77a44317ff3dfba285049fc5 Mon Sep 17 00:00:00 2001
From: Taha Cherfia <taha.cherfia@holi.team>
Date: Wed, 26 Feb 2025 13:04:16 +0100
Subject: [PATCH] NOISSUE: invalidate feed_insights query cache

---
 openbook_insights/models/insight.py | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/openbook_insights/models/insight.py b/openbook_insights/models/insight.py
index c06598711..cfcbb4633 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)
-- 
GitLab