11package org .com .stocknote .domain .post .service ;
22
33import lombok .RequiredArgsConstructor ;
4+ import lombok .extern .slf4j .Slf4j ;
45import org .com .stocknote .domain .hashtag .entity .Hashtag ;
56import org .com .stocknote .domain .hashtag .service .HashtagService ;
67import org .com .stocknote .domain .like .repository .LikeRepository ;
1617import org .com .stocknote .domain .post .repository .PostRepository ;
1718import org .com .stocknote .domain .post .repository .PostSearchRepository ;
1819import org .springframework .beans .factory .annotation .Autowired ;
20+ import org .springframework .beans .factory .annotation .Qualifier ;
1921import org .springframework .cache .annotation .Cacheable ;
2022import org .springframework .data .domain .*;
2123import org .springframework .data .redis .core .RedisTemplate ;
2224import org .springframework .stereotype .Service ;
2325import org .springframework .transaction .annotation .Transactional ;
2426
27+ import java .time .Duration ;
2528import java .time .LocalDateTime ;
2629import java .util .List ;
2730import java .util .stream .Collectors ;
2831
2932@ Service
3033@ RequiredArgsConstructor
34+ @ Slf4j
3135public class PostService {
3236 private final PostRepository postRepository ;
3337 private final HashtagService hashtagService ;
@@ -113,11 +117,22 @@ public void deletePost(Long id) {
113117 //댓글은 CASCADE로 삭제됨
114118 postRepository .delete (post );
115119 }
116- @ Cacheable ( value = POPULAR_POSTS_CACHE_KEY , key = "#pageable.pageNumber" , cacheManager = "cacheManager" )
120+
117121 @ Transactional (readOnly = true )
118122 public Page <PostResponseDto > getPopularPosts (Pageable pageable ) {
119- LocalDateTime threeDaysAgo = LocalDateTime .now ().minusDays (3 );
123+ log .info ("🔥 캐시 확인: getPopularPosts 실행 (page = {})" , pageable .getPageNumber ());
124+
125+ String cacheKey = POPULAR_POSTS_CACHE_KEY + ":" + pageable .getPageNumber ();
126+
127+ // 🔹 Redis에서 캐시된 데이터 조회
128+ Object cachedData = redisTemplate .opsForValue ().get (cacheKey );
129+ if (cachedData instanceof Page ) {
130+ log .info ("✅ 캐시된 데이터 반환 (page = {})" , pageable .getPageNumber ());
131+ return (Page <PostResponseDto >) cachedData ;
132+ }
120133
134+ // 🔹 캐시된 데이터가 없으면 DB에서 조회
135+ LocalDateTime threeDaysAgo = LocalDateTime .now ().minusDays (3 );
121136 Page <Post > popularPosts = postRepository .findPopularPosts (threeDaysAgo , pageable );
122137
123138 List <PostResponseDto > sortedPosts = popularPosts .stream ()
@@ -130,10 +145,16 @@ public Page<PostResponseDto> getPopularPosts(Pageable pageable) {
130145 })
131146 .collect (Collectors .toList ());
132147
133- return new PageImpl <>(sortedPosts , pageable , popularPosts .getTotalElements ());
148+ Page <PostResponseDto > response = new PageImpl <>(sortedPosts , pageable , popularPosts .getTotalElements ());
149+
150+ // 🔹 Redis에 캐싱 (TTL: 5분 설정)
151+ redisTemplate .opsForValue ().set (cacheKey , response , Duration .ofMinutes (5 ));
152+ log .info ("🚀 새로운 데이터 Redis에 캐싱 완료 (key = {})" , cacheKey );
153+
154+ return response ;
134155 }
135156
136- // 좋아요 순 조회
157+ // 좋아요 순 조회
137158 @ Transactional (readOnly = true )
138159 public Page <PostResponseDto > getPopularPostsByLikes (Pageable pageable ) {
139160 LocalDateTime sevenDaysAgo = LocalDateTime .now ().minusDays (7 );
0 commit comments