Skip to content

Commit

Permalink
local approach for cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-smith committed Sep 21, 2024
1 parent c47733b commit 5060674
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,18 @@ def _process_request_data(self, request: Request) -> None:

def create(self, request: Request, *args, **kwargs) -> Response:
self._process_request_data(request)
cache.clear()
return super().create(request, *args, **kwargs)

def update(self, request: Request, *args, **kwargs) -> Response:
self._process_request_data(request)
cache.clear()
return super().update(request, *args, **kwargs)

def partial_update(self, request: Request, *args, **kwargs) -> Response:
self._process_request_data(request)
cache.clear()
return super().partial_update(request, *args, **kwargs)

def destroy(self, request: Request, *args, **kwargs) -> Response:
"""
Invalidate cache when an object is deleted.
"""
cache.clear() # Clear cache after deletion
self._process_request_data(request)
return super().destroy(request, *args, **kwargs)

class Meta:
Expand Down Expand Up @@ -268,6 +262,30 @@ class ThreatViewSet(BaseModelViewSet):
filterset_fields = ["folder", "risk_scenarios"]
search_fields = ["name", "provider", "description"]

def perform_create(self, serializer):
"""
Invalidate cache when a new threat is created.
"""
serializer.save()
# Invalidate the list and threats_count caches after creating a new threat
cache.clear()

def perform_update(self, serializer):
"""
Invalidate cache when a threat is updated.
"""
serializer.save()
# Invalidate the cache for the updated threat and the list view
cache.clear()

def perform_destroy(self, instance):
"""
Invalidate cache when a threat is deleted.
"""
super().perform_destroy(instance)
# Invalidate the cache for the deleted threat and the list view
cache.clear()

@method_decorator(cache_page(60 * MED_CACHE_TTL))
@method_decorator(vary_on_cookie)
def list(self, request, *args, **kwargs):
Expand Down

0 comments on commit 5060674

Please sign in to comment.