99import org .springframework .scheduling .annotation .Scheduled ;
1010import org .springframework .stereotype .Service ;
1111
12- import java .time .Duration ;
13- import java .time .LocalDate ;
14- import java .time .LocalDateTime ;
15- import java .time .format .DateTimeFormatter ;
1612import java .util .Set ;
1713import java .util .UUID ;
1814
@@ -23,27 +19,6 @@ public class ViewCountService {
2319 private final StringRedisTemplate redisTemplate ;
2420 private final ProductRepository productRepo ;
2521
26- // 로그인한 사용자의 조회수 증가 처리
27- public void incrementViewForLogged (UUID userId , UUID productId ) {
28- final String today = LocalDate .now ().format (DateTimeFormatter .ofPattern ("yyyyMMdd" ));
29- final String key = String .format ("user:%s:views:%s" , userId .toString (), today );
30-
31- // 이미 조회한 상품인지 확인 (NullPointerException 방지)
32- Long result = redisTemplate .opsForSet ().add (key , productId .toString ());
33- boolean isView = result != null && result == 1 ;
34-
35- // 키 만료 시간 설정(당일 자정까지)
36- LocalDateTime midnight = LocalDate .now ().plusDays (1 ).atStartOfDay ();
37- Duration timeUntilMidnight = Duration .between (LocalDateTime .now (), midnight );
38- redisTemplate .expire (key , timeUntilMidnight );
39-
40- if (isView ) {
41- // 조회수 증가
42- String viewCountkey = "product:views:" + productId ;
43- redisTemplate .opsForValue ().increment (viewCountkey );
44- }
45- }
46-
4722 // 비로그인 사용자의 조회 확인 (쿠키는 컨트롤러에서 처리)
4823 public void incrementViewForProduct (UUID productId ) {
4924 final String viewCountKey = "product:views:" + productId .toString ();
@@ -110,26 +85,6 @@ public void syncViewCountFromRedis() {
11085 }
11186 }
11287
113- // 오래된 사용자 조회 기록 삭제 (30일 이상 된 기록)
114- Set <String > oldUserViewKeys = redisTemplate .keys ("user:*:views:*" );
115- if (oldUserViewKeys != null && !oldUserViewKeys .isEmpty ()) {
116- for (String key : oldUserViewKeys ) {
117- String [] parts = key .split (":" );
118-
119- if (parts .length >= 4 ) {
120- String dateStr = parts [3 ];
121- try {
122- LocalDate keyDate = LocalDate .parse (dateStr , DateTimeFormatter .ofPattern ("yyyyMMdd" ));
123-
124- if (keyDate .isBefore (LocalDate .now ().minusDays (30 )))
125- redisTemplate .delete (key );
126- } catch (Exception e ) {
127- log .warn ("날짜 파싱 오류: {}" , key );
128- }
129- }
130- }
131- }
132-
13388 log .info ("조회수 동기화 및 데이터 정리 성공" );
13489 } catch (Exception e ) {
13590 log .error ("작품 조회수 동기화 중 오류 발생: " , e );
0 commit comments