Skip to content

Commit

Permalink
Improved Redis cache, fixed N+1 issue in list view.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebamus committed Nov 1, 2024
1 parent eea12f8 commit d4f2018
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion blog/views/blog/blog_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BlogListView(ListView):
context_object_name = "board"

def get_queryset(self):
return BlogPost.activate_objects.get_data()
return BlogPost.activate_objects.get_data().select_related('author')


class BlogDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion blog/views/books/books_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BooksListView(ListView):
context_object_name = "board"

def get_queryset(self):
return BooksPost.activate_objects.get_data()
return BooksPost.activate_objects.get_data().select_related('author')


class BooksDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion blog/views/online_study/online_study_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OnlineStudyListView(ListView):
context_object_name = "board"

def get_queryset(self):
return OnlineStudyPost.activate_objects.get_data()
return OnlineStudyPost.activate_objects.get_data().select_related('author')


class OnlineStudyDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion blog/views/opensource/opensource_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OpenSourceListView(ListView):
context_object_name = "board"

def get_queryset(self):
return OpenSourcePost.activate_objects.get_data()
return OpenSourcePost.activate_objects.get_data().select_related('author')


class OpenSourceDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion blog/views/project/project_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ProjectListView(ListView):
context_object_name = "board"

def get_queryset(self):
return ProjectPost.activate_objects.get_data()
return ProjectPost.activate_objects.get_data().select_related('author')


class ProjectDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion board/views/notice/notice_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NoticeListView(ListView):
context_object_name = "board"

def get_queryset(self):
return Notice.activate_objects.get_data()
return Notice.activate_objects.get_data().select_related('author')


class NoticeDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion board/views/reactivation/reactivation_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ReactivationListView(ListView):
context_object_name = "board"

def get_queryset(self):
return Reactivation.activate_objects.get_data()
return Reactivation.activate_objects.get_data().select_related('author')


class ReactivationDetailView(DetailView):
Expand Down
2 changes: 1 addition & 1 deletion board/views/visiter/visiter_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class VisiterListView(ListView):
context_object_name = "board"

def get_queryset(self):
return Visiter.activate_objects.get_data()
return Visiter.activate_objects.get_data().select_related('author')


class VisiterDetailView(DetailView):
Expand Down
11 changes: 7 additions & 4 deletions common/components/django_redis_cache_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

# django-redis cache set
def dredis_cache_set(prefix: str, pk: int, **kwargs: dict) -> None:
logger.debug(f"kwargs : {kwargs}")
if 1 < len(kwargs):
logger.debug(f"kwargs length : {len(kwargs)}")
# kwargs를 가독성 있게 출력
kwargs_output = ', '.join([f"{key}: {value}" for key, value in kwargs.items()])
logger.debug(f"kwargs : {{{kwargs_output}}}")

# kwargs가 하나 이상의 키를 가지고 있고, 첫 번째 키의 값이 존재하는지 확인
if kwargs and any(value is not None for value in kwargs.values()):
for key, value in kwargs.items():
redis_key = prefix + ":" + str(pk) + ":" + key
logger.debug(f"redis key : {redis_key}")
logger.debug(f"redis value : {value}")
logger.debug(f"redis value : {value.__dict__}")
result = cache.set(redis_key, value, timeout=CACHE_TTL, nx=False)
logger.debug(f"cache.set result : {result}")

Expand Down

0 comments on commit d4f2018

Please sign in to comment.