fix cache

This commit is contained in:
2026-03-30 09:39:35 -06:00
parent 24ff67ec0c
commit c31145f76e
4 changed files with 40 additions and 12 deletions

View File

@@ -16,7 +16,8 @@ from otpauth import OtpAuth
from problem.models import Problem from problem.models import Problem
from submission.models import Submission, JudgeStatus from submission.models import Submission, JudgeStatus
from utils.constants import ContestRuleType from django.core.cache import cache
from utils.constants import ContestRuleType, CacheKey
from options.options import SysOptions from options.options import SysOptions
from utils.api import APIView, validate_serializer, CSRFExemptAPIView from utils.api import APIView, validate_serializer, CSRFExemptAPIView
from utils.captcha import Captcha from utils.captcha import Captcha
@@ -464,6 +465,11 @@ class UserActivityRankAPI(APIView):
start = request.GET.get("start") start = request.GET.get("start")
if not start: if not start:
return self.error("start time is required") return self.error("start time is required")
cache_key = f"{CacheKey.user_activity_rank}:{start}"
cached = cache.get(cache_key)
if cached is not None:
return self.success(cached)
hidden_names = User.objects.filter( hidden_names = User.objects.filter(
Q(admin_type=AdminType.SUPER_ADMIN) | Q(is_disabled=True) Q(admin_type=AdminType.SUPER_ADMIN) | Q(is_disabled=True)
).values_list("username", flat=True) ).values_list("username", flat=True)
@@ -477,6 +483,7 @@ class UserActivityRankAPI(APIView):
.annotate(count=Count("problem_id", distinct=True)) .annotate(count=Count("problem_id", distinct=True))
.order_by("-count")[:10] .order_by("-count")[:10]
) )
cache.set(cache_key, data, 600)
return self.success(data) return self.success(data)

View File

@@ -1,8 +1,10 @@
from django.db.models import Avg from django.core.cache import cache
from django.db.models import Avg, Count
from django.db.models.functions import Round from django.db.models.functions import Round
from comment.models import Comment from comment.models import Comment
from problem.models import Problem from problem.models import Problem
from utils.api import APIView from utils.api import APIView
from utils.constants import CacheKey
from account.decorators import login_required from account.decorators import login_required
from utils.api.api import validate_serializer from utils.api.api import validate_serializer
from comment.serializers import CreateCommentSerializer, CommentSerializer from comment.serializers import CreateCommentSerializer, CommentSerializer
@@ -46,6 +48,7 @@ class CommentAPI(APIView):
comprehensive_rating=data["comprehensive_rating"], comprehensive_rating=data["comprehensive_rating"],
content=data["content"], content=data["content"],
) )
cache.delete(f"{CacheKey.comment_stats}:{problem.id}")
return self.success() return self.success()
@login_required @login_required
@@ -65,16 +68,24 @@ class CommentAPI(APIView):
class CommentStatisticsAPI(APIView): class CommentStatisticsAPI(APIView):
def get(self, request): def get(self, request):
problem_id = request.GET.get("problem_id") problem_id = request.GET.get("problem_id")
comments = Comment.objects.select_related("problem").filter( cache_key = f"{CacheKey.comment_stats}:{problem_id}"
problem_id=problem_id cached = cache.get(cache_key)
) if cached is not None:
if comments.count() == 0: return self.success(cached)
return self.success()
count = comments.count() agg = Comment.objects.filter(problem_id=problem_id).aggregate(
rating = comments.aggregate( count=Count("id"),
description=Round(Avg("description_rating"), 2), description=Round(Avg("description_rating"), 2),
difficulty=Round(Avg("difficulty_rating"), 2), difficulty=Round(Avg("difficulty_rating"), 2),
comprehensive=Round(Avg("comprehensive_rating"), 2), comprehensive=Round(Avg("comprehensive_rating"), 2),
) )
return self.success({"count": count, "rating": rating}) if not agg["count"]:
return self.success()
data = {"count": agg["count"], "rating": {
"description": agg["description"],
"difficulty": agg["difficulty"],
"comprehensive": agg["comprehensive"],
}}
cache.set(cache_key, data, 3600)
return self.success(data)

View File

@@ -19,12 +19,19 @@ from contest.models import ContestRuleType
class ProblemTagAPI(APIView): class ProblemTagAPI(APIView):
def get(self, request): def get(self, request):
keyword = request.GET.get("keyword", "")
cache_key = f"{CacheKey.problem_tags}:{keyword}"
cached = cache.get(cache_key)
if cached is not None:
return self.success(cached)
qs = ProblemTag.objects qs = ProblemTag.objects
keyword = request.GET.get("keyword")
if keyword: if keyword:
qs = ProblemTag.objects.filter(name__icontains=keyword) qs = ProblemTag.objects.filter(name__icontains=keyword)
tags = qs.annotate(problem_count=Count("problem")).filter(problem_count__gt=0) tags = qs.annotate(problem_count=Count("problem")).filter(problem_count__gt=0)
return self.success(TagSerializer(tags, many=True).data) data = TagSerializer(tags, many=True).data
cache.set(cache_key, data, 3600)
return self.success(data)
class PickOneAPI(APIView): class PickOneAPI(APIView):

View File

@@ -26,6 +26,9 @@ class CacheKey:
contest_rank_cache = "contest_rank_cache" contest_rank_cache = "contest_rank_cache"
website_config = "website_config" website_config = "website_config"
problem_authors = "problem_authors" problem_authors = "problem_authors"
problem_tags = "problem_tags"
comment_stats = "comment_stats"
user_activity_rank = "user_activity_rank"
class Difficulty(Choices): class Difficulty(Choices):