diff --git a/gameserver/urls.py b/gameserver/urls.py index ec29784..8f1ce8e 100644 --- a/gameserver/urls.py +++ b/gameserver/urls.py @@ -1,5 +1,4 @@ from django.urls import path -from django.views.decorators.cache import cache_page from . import views urlpatterns = [ @@ -85,7 +84,7 @@ ), path( "contest//scoreboard", - cache_page(60 * 5)(views.ContestScoreboard.as_view()), # cache for 5m + views.ContestScoreboard.as_view(), # cache for 5m name="contest_scoreboard", ), path( diff --git a/gameserver/views/contest.py b/gameserver/views/contest.py index e30d58e..5ae8ade 100644 --- a/gameserver/views/contest.py +++ b/gameserver/views/contest.py @@ -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):