Skip to content

Commit

Permalink
develop: redis 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
baekhangyeol committed Aug 3, 2023
1 parent 9f0f760 commit 7472820
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions backend/pet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,17 @@ class PetListView(APIView):
@swagger_auto_schema()
@transaction.atomic
def get(self, requset, user_id):
cache_key = f'get_data:{user_id}'
cached_data = cache.get(cache_key)

if cached_data:
return Response(cached_data)

try:
user = User.objects.get(id=user_id)
pets = Pet.objects.filter(user=user)
serializer = PetSerializer(pets, many=True)
cache.set(cache_key, serializer.data, timeout=86400)
return Response(serializer.data, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response({"message": "해당 사용자를 찾을 수 없습니다."})

0 comments on commit 7472820

Please sign in to comment.