Skip to content

Commit

Permalink
Fix: 서비스 관련 Conflict 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
chaejm55 committed Oct 10, 2024
1 parent c76a533 commit 6006163
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PostResponse.Main> getTrendingPosts(Category category, int size) {
List<Post> 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 초기화");
}
}

0 comments on commit 6006163

Please sign in to comment.