From 60061634ac5e4df0c8203f90d204b97e50001498 Mon Sep 17 00:00:00 2001 From: chaejm55 Date: Fri, 11 Oct 2024 02:05:32 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20Conflict=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/post/PostService.java | 10 +------- .../service/post/PostServiceImpl.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/gdsc_knu/official_homepage/service/post/PostService.java b/src/main/java/com/gdsc_knu/official_homepage/service/post/PostService.java index f74091db..4c38019f 100644 --- a/src/main/java/com/gdsc_knu/official_homepage/service/post/PostService.java +++ b/src/main/java/com/gdsc_knu/official_homepage/service/post/PostService.java @@ -1,16 +1,8 @@ package com.gdsc_knu.official_homepage.service.post; import com.gdsc_knu.official_homepage.dto.post.PostResponse; -import com.gdsc_knu.official_homepage.entity.post.Post; +import com.gdsc_knu.official_homepage.dto.post.PostRequest; import com.gdsc_knu.official_homepage.entity.post.enumeration.Category; -import com.gdsc_knu.official_homepage.repository.PostRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import java.util.List; diff --git a/src/main/java/com/gdsc_knu/official_homepage/service/post/PostServiceImpl.java b/src/main/java/com/gdsc_knu/official_homepage/service/post/PostServiceImpl.java index 3c3b7b22..2ed68698 100644 --- a/src/main/java/com/gdsc_knu/official_homepage/service/post/PostServiceImpl.java +++ b/src/main/java/com/gdsc_knu/official_homepage/service/post/PostServiceImpl.java @@ -11,12 +11,17 @@ import com.gdsc_knu.official_homepage.repository.MemberRepository; import com.gdsc_knu.official_homepage.repository.PostRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service +@Slf4j @RequiredArgsConstructor public class PostServiceImpl implements PostService { private final PostRepository postRepository; @@ -112,4 +117,22 @@ public void deletePost(Long memberId, Long postId) { } postRepository.delete(post); } + + @Override + @Transactional(readOnly = true) + @Cacheable(value = "trending-post", key = "'Is '+#category", unless="#result.size()<5") + public List getTrendingPosts(Category category, int size) { + List posts = postRepository.findTop5ByCategory(category, size); + return posts.stream().map(PostResponse.Main::from).toList(); + } + + @Scheduled(cron = "0 0 0 * * ?") + public void invokeClearPost() { + clearTrendingPosts(); + } + + @CacheEvict(value = "trending-post", allEntries = true, beforeInvocation = true) + public void clearTrendingPosts() { + log.info("trending post 초기화"); + } }