Skip to content

Commit

Permalink
Merge pull request #81 from whatTeamNaeTeam/feat-redis_cache_2
Browse files Browse the repository at this point in the history
Feat redis cache 2
  • Loading branch information
fnzksxl authored Aug 20, 2024
2 parents d11aa0a + 3b6fabb commit 47f3742
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
13 changes: 10 additions & 3 deletions wtnt/admin/team/service.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib.auth import get_user_model
from django.core.cache import cache

import core.exception.notfound as notfound_exception
import core.exception.team as team_exception
from admin.serializers import ApproveTeamSerializer, AdminTeamManageDetailSerializer
from core.pagenations import ListPagenationSize10
from core.pagenations import TeamListPagenationSize10
from core.service import BaseService
from core.utils.s3 import S3Utils
from core.utils.admin import TeamResponse
Expand All @@ -12,7 +13,7 @@
User = get_user_model()


class AdminTeamService(BaseService, ListPagenationSize10):
class AdminTeamService(BaseService, TeamListPagenationSize10):
def get_not_approved_team(self):
queryset = Team.objects.filter(is_approved=False).select_related("leader")
if queryset:
Expand All @@ -24,7 +25,13 @@ def get_not_approved_team(self):
def approve_teams(self):
team_ids = [int(id) for id in self.request.data.get("ids").split(",")]
cnt = Team.objects.filter(id__in=team_ids).update(is_approved=True)

if cnt:
cache_count = cache.get("cache_count_team")
if cache_count:
cache_count += cnt
cache.set("cache_count_team", cache_count, timeout=60 * 5)

return {"detail": "Success to update teams"}
else:
raise notfound_exception.TeamNotFoundError()
Expand Down Expand Up @@ -65,7 +72,7 @@ def serach_teams(self):
if not queryset:
raise notfound_exception.TeamNotFoundError()

paginated = self.paginate_queryset(queryset, self.request, view=self)
paginated = self.paginate_queryset(queryset, self.request, view=self, is_search=True)
serializer = ApproveTeamSerializer(paginated, many=True)

return self.get_paginated_response(serializer.data)
Expand Down
11 changes: 8 additions & 3 deletions wtnt/admin/user/service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from django.contrib.auth import get_user_model
from django.core.cache import cache

import core.exception.notfound as notfound_exception
import core.exception.team as team_exception
from core.pagenations import ListPagenationSize10
from core.pagenations import UserListPagenationSize10
from core.service import BaseService
from core.utils.s3 import S3Utils
from admin.serializers import ApproveUserSerializer

User = get_user_model()


class AdminUserService(BaseService, ListPagenationSize10):
class AdminUserService(BaseService, UserListPagenationSize10):
def get_not_approved_users(self):
queryset = User.objects.filter(is_approved=False, is_superuser=False)
if queryset:
Expand All @@ -23,6 +24,10 @@ def approve_users(self):
user_ids = [int(id) for id in self.request.data.get("ids").split(",")]
cnt = User.objects.filter(id__in=user_ids).update(is_approved=True)
if cnt:
cache_count = cache.get("cache_count_user")
if cache_count:
cache_count += cnt
cache.set("cache_count_user", cache_count, timeout=60 * 5)
return {"detail": "Success to update users"}
else:
raise notfound_exception.UserNotFoundError()
Expand Down Expand Up @@ -61,7 +66,7 @@ def search_users(self):
if not queryset:
raise notfound_exception.UserNotFoundError()

paginated = self.paginate_queryset(queryset, self.request, view=self)
paginated = self.paginate_queryset(queryset, self.request, view=self, is_search=True)
serializer = ApproveUserSerializer(paginated, many=True)

return self.get_paginated_response(serializer.data)
46 changes: 45 additions & 1 deletion wtnt/core/pagenations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from rest_framework.pagination import PageNumberPagination, CursorPagination
from django.core.paginator import Paginator
import wtnt.settings as api_settings

from django.core.cache import cache


class ListPagenationSize10(PageNumberPagination):
page_size = 10
Expand All @@ -22,8 +25,49 @@ def get_previous_link(self):
return url.replace("http://localhost:8000", api_settings.MY_DOMAIN)


class CachedPaginator(Paginator):
def __init__(self, *args, cache_key=None, cache_timeout=600, **kwargs):
super().__init__(*args, **kwargs)
self.cache_key = cache_key
self.cache_timeout = cache_timeout

@property
def count(self):
if self.cache_key:
cached_count = cache.get(self.cache_key)
if cached_count is not None:
return cached_count

count = super().count

if self.cache_key:
cache.set(self.cache_key, count, timeout=self.cache_timeout)

return count


class UserListPagenationSize10(ListPagenationSize10):
def paginate_queryset(self, queryset, request, view=None, is_search=None):
self.request = request
cache_key = "" if is_search else "cached_count_user"
paginator = CachedPaginator(queryset, self.page_size, cache_key=cache_key, cache_timeout=60 * 10)
self.page = paginator.page(self.request.query_params.get("page", 1))

return list(self.page)


class TeamListPagenationSize10(ListPagenationSize10):
def paginate_queryset(self, queryset, request, view=None, is_search=None):
self.request = request
cache_key = "" if is_search else "cached_count_team"
paginator = CachedPaginator(queryset, self.page_size, cache_key=cache_key, cache_timeout=60 * 10)
self.page = paginator.page(self.request.query_params.get("page", 1))

return list(self.page)


class TeamPagination(CursorPagination):
page_size = 2
page_size = 4
ordering = "-created_at"

def get_next_link(self):
Expand Down

0 comments on commit 47f3742

Please sign in to comment.