Skip to content

Commit

Permalink
fix sad broken cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Mar 5, 2024
1 parent 898a1bf commit ad40fef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 1 addition & 2 deletions gameserver/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.urls import path
from django.views.decorators.cache import cache_page
from . import views

urlpatterns = [
Expand Down Expand Up @@ -85,7 +84,7 @@
),
path(
"contest/<str:slug>/scoreboard",
cache_page(60 * 5)(views.ContestScoreboard.as_view()), # cache for 5m
views.ContestScoreboard.as_view(), # cache for 5m
name="contest_scoreboard",
),
path(
Expand Down
18 changes: 16 additions & 2 deletions gameserver/views/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,24 @@ def get_title(self):
return "Scoreboard for " + self.object.name

def get_queryset(self):
return self.object.ranks().prefetch_related('team', 'submissions__problem')
cache_key = f"contest_{self.kwargs['slug']}_scoreboard"
queryset = cache.get(cache_key)
if not queryset or request.GET.get('cache_reset', '').casefold() == "yaaaa":
queryset = self.object.ranks().prefetch_related('team', 'submissions__problem')
cache.set(cache_key, queryset, 5 * 5) # Cache for 5 minutes (300 seconds)
return queryset

@staticmethod
def _get_contest(slug):
cache_key = f"contest_{slug}_scoreboard_contest"
contest = cache.get(cache_key)
if not contest or request.GET.get('cache_reset', ''.casefold()) == "yaaaa":
contest = get_object_or_404(models.Contest, slug=slug)
cache.set(cache_key, contest, 5 * 5) # Cache for 5 minutes (300 seconds)
return contest

def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=models.Contest.objects.all())
self.object = self._get_contest(self.kwargs["slug"])
return super().get(request, *args, **kwargs)

def get_context_data(self, **kwargs):
Expand Down

0 comments on commit ad40fef

Please sign in to comment.