Skip to content

Commit

Permalink
Fix caching behavior and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julianweng committed Sep 30, 2024
1 parent f9af3c3 commit 56c76c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion backend/clubs/management/commands/deactivate.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def handle(self, *args, **kwargs):
num_ghosted += 1

club.save()
cache.delete(f"clubs:{club.id}") # clear cache
cache.delete(f"clubs:{club.id}-authed") # clear cache
cache.delete(f"clubs:{club.id}-anon")

self.stdout.write(
f"{clubs.count()} clubs deactivated! {num_ghosted} clubs ghosted!"
Expand Down
15 changes: 8 additions & 7 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,8 @@ def upload(self, request, *args, **kwargs):
"""
# ensure user is allowed to upload image
club = self.get_object()
key = f"clubs:{club.id}"
cache.delete(key)
cache.delete(f"clubs:{club.id}-authed")
cache.delete(f"clubs:{club.id}-anon")

# reset approval status after upload
resp = upload_endpoint_helper(request, club, "file", "image", save=False)
Expand Down Expand Up @@ -2039,17 +2039,17 @@ def update(self, request, *args, **kwargs):
Invalidate caches
"""
self.check_approval_permission(request)
key = f"clubs:{self.get_object().id}"
cache.delete(key)
cache.delete(f"clubs:{self.get_object().id}-authed")
cache.delete(f"clubs:{self.get_object().id}-anon")
return super().update(request, *args, **kwargs)

def partial_update(self, request, *args, **kwargs):
"""
Invalidate caches
"""
self.check_approval_permission(request)
key = f"clubs:{self.get_object().id}"
cache.delete(key)
cache.delete(f"clubs:{self.get_object().id}-authed")
cache.delete(f"clubs:{self.get_object().id}-anon")
return super().partial_update(request, *args, **kwargs)

def perform_destroy(self, instance):
Expand Down Expand Up @@ -2819,7 +2819,8 @@ def create_tickets(self, request, *args, **kwargs):
event.ticket_drop_time = drop_time
event.save()

cache.delete(f"clubs:{event.club.id}")
cache.delete(f"clubs:{event.club.id}-authed")
cache.delete(f"clubs:{event.club.id}-anon")
return Response({"detail": "Successfully created tickets"})

@action(detail=True, methods=["post"])
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/clubs/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ def test_deactivate_invalidates_cache(self):
self.client.get(reverse("clubs-detail", args=(club.code,)))

# club should now be cached
cache_key = f"clubs:{club.id}"
cache_key = f"clubs:{club.id}-anon"
self.assertIsNotNone(caches["default"].get(cache_key))

call_command("deactivate", "all", "--force")
Expand Down

0 comments on commit 56c76c5

Please sign in to comment.