Skip to content

Commit

Permalink
추가: cache 프레임워크 적용
Browse files Browse the repository at this point in the history
- 변동성 적거나 쿼리시간이 상대적으로 오래 걸리는 경우 적용
- 친구, 키워드 추천과 같은 AI 모델을 사용하는 경우 적용
  • Loading branch information
Sahayana committed Nov 9, 2023
1 parent 6e525df commit 1855a9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions alaltalk/cache_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""캐싱에 필요한 Key 관리"""

# Friend
RECOMMEND_FRIEND = "recommend_friend_{user_id}"
USER_LIKE_DATA = "user_like_data_{user_id}"
15 changes: 13 additions & 2 deletions apps/friend/v1/apis/friend_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json

from django.core.cache import cache
from rest_framework import permissions, renderers, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

from alaltalk import cache_key
from apps.account.services.user_service import UserService
from apps.account.v1.serializers.user_serializer import (
UserLikeKeywordSerilaizer,
Expand Down Expand Up @@ -31,10 +33,15 @@ def get_renderers(self):
def list(self, request, *args, **kwargs):
"""현재 친구 목록과 추천 친구를 렌더링합니다."""
user = request.user
recommend_friend = cache.get_or_set(
cache_key.RECOMMEND_FRIEND.format(user_id=user.id),
FriendService.recommend_friend(user_id=user.id),
timeout=86400,
)
context = {
"user": user,
"friends": FriendSelector.get_friends_list(user_id=user.id),
"recommend_friend": FriendService.recommend_friend(user=user),
"recommend_friend": recommend_friend,
}

return Response(
Expand Down Expand Up @@ -97,7 +104,11 @@ def recommend_keywords(self, request, *args, **kwargs):
@action(detail=False, methods=["post"])
def get_user_like(self, request, *args, **kwargs):
user = request.user
like_sentence = FriendService.get_user_like_data(user_id=user.id)
like_sentence = cache.get_or_set(
cache_key.USER_LIKE_DATA.format(user_id=user.id),
FriendService.get_user_like_data(user_id=user.id),
timeout=86400,
)
data = {"like_sentence": like_sentence}

return Response(data=data, status=status.HTTP_200_OK)

0 comments on commit 1855a9f

Please sign in to comment.