Skip to content

Commit

Permalink
#88 Feat: 좋아요 API 캐시 로직 추가
Browse files Browse the repository at this point in the history
캐시 존재 시 캐시, DB 모두 업데이트
  • Loading branch information
fnzksxl committed Aug 28, 2024
1 parent 76ca0ef commit 4c5e609
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions wtnt/team/like/service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.core.cache import cache

import core.exception.notfound as notfound_exception
import core.exception.team as team_exception
from core.service import BaseService
Expand All @@ -12,11 +14,21 @@ def like(self):
team_id = self.kwargs.get("team_id")
version = self.request.data.get("version")

cache_key = f"team_detail_{team_id}"
team_cache = cache.get(cache_key)

if team_cache is not None:
cache_update = True

try:
team = Team.objects.get(id=team_id)
like = Likes.objects.get(team_id=team_id, user_id=user_id)
if version == team.version:
like.delete()
if cache_update:
team_cache["like"] -= 1
team_cache["version"] += 1
cache.set(cache_key, team_cache, timeout=60 * 10)
team.like -= 1
team.version += 1
team.save()
Expand All @@ -33,6 +45,10 @@ def like(self):
serializer = TeamLikeSerializer(data={"team_id": team_id, "user_id": user_id})
if serializer.is_valid():
serializer.save()
if cache_update:
team_cache["like"] += 1
team_cache["version"] += 1
cache.set(cache_key, team_cache, timeout=60 * 10)
team.like += 1
team.version += 1
team.save()
Expand Down

0 comments on commit 4c5e609

Please sign in to comment.