-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
317 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions
17
src/main/java/com/kkokkomu/short_news/core/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.kkokkomu.short_news.core.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Bean | ||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
return redisTemplate; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/kkokkomu/short_news/core/config/service/RedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.kkokkomu.short_news.core.config.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class RedisService { | ||
|
||
@Autowired | ||
private RedisTemplate<String, String> redisTemplate; | ||
|
||
// 뉴스 조회수 | ||
private static final String NEWS_VIEW_COUNT_PREFIX = "news:viewCount:"; | ||
|
||
public void incrementViewCount(Long newsId) { | ||
String key = NEWS_VIEW_COUNT_PREFIX + newsId; | ||
redisTemplate.opsForValue().increment(key); | ||
} | ||
|
||
public Integer getViewCount(Long newsId) { | ||
String key = NEWS_VIEW_COUNT_PREFIX + newsId; | ||
String count = redisTemplate.opsForValue().get(key); | ||
return count != null ? Integer.parseInt(count) : 0; | ||
} | ||
|
||
// 뉴스 시청기록 | ||
private static final String VIEW_HISTORY_PREFIX = "news:viewHistory:"; | ||
|
||
public void saveNewsViewHistory(Long userId, Long newsId) { | ||
String key = VIEW_HISTORY_PREFIX + userId; | ||
redisTemplate.opsForSet().add(key, String.valueOf(newsId)); | ||
} | ||
|
||
public Set<Long> getNewsViewHistory(Long userId) { | ||
String key = VIEW_HISTORY_PREFIX + userId; | ||
Set<String> stringSet = redisTemplate.opsForSet().members(key); // Set<String> 반환 | ||
|
||
// Set<String>을 Set<Long>으로 변환 | ||
return stringSet.stream() | ||
.map(Long::valueOf) // String -> Long 변환 | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
public void deleteNewsViewHistory(Long userId) { | ||
String key = VIEW_HISTORY_PREFIX + userId; | ||
redisTemplate.delete(key); | ||
} | ||
} | ||
|
22 changes: 0 additions & 22 deletions
22
src/main/java/com/kkokkomu/short_news/core/event/NewsViewHistListener.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package com.kkokkomu.short_news.core.scheduler; | ||
|
||
import com.kkokkomu.short_news.core.config.service.RedisService; | ||
import com.kkokkomu.short_news.news.domain.News; | ||
import com.kkokkomu.short_news.news.dto.news.request.CreateGenerateNewsDto; | ||
import com.kkokkomu.short_news.news.dto.news.response.GenerateNewsDto; | ||
import com.kkokkomu.short_news.core.config.service.MailService; | ||
import com.kkokkomu.short_news.news.service.AdminNewsService; | ||
import com.kkokkomu.short_news.news.service.HomeNewsService; | ||
import com.kkokkomu.short_news.news.service.NewsViewHistService; | ||
import com.kkokkomu.short_news.user.domain.User; | ||
import com.kkokkomu.short_news.user.service.UserLookupService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
|
@@ -17,7 +23,10 @@ | |
@RequiredArgsConstructor | ||
public class NewsScheduler { | ||
private final AdminNewsService adminNewsService; | ||
private final HomeNewsService homeNewsService; | ||
private final MailService mailService; | ||
private final UserLookupService userLookupService; | ||
private final NewsViewHistService newsViewHistService; | ||
|
||
@Scheduled(cron = "0 0 8 * * *") // 매일 아침 8시 | ||
public void generateNewsAt8AM() { | ||
|
@@ -59,5 +68,20 @@ public void generateNewsAt8AM() { | |
log.info("send [email protected]"); | ||
mailService.sendEmail("[email protected]", LocalDate.now().toString() + " kkm 뉴스", content.toString()); | ||
|
||
} | ||
} // 뉴스 생성 | ||
|
||
@Scheduled(fixedRate = 600000) // 10분 마다 | ||
public void syncViewCountToDatabase() { | ||
log.info("syncViewCountToDatabase"); | ||
homeNewsService.updateViewCnt(); | ||
} // 뉴스 조회수 동기화 | ||
|
||
@Scheduled(cron = "0 0 4 * * ?") // 매일 새벽 4시에 실행 | ||
public void syncAllUsersViewHistory() { | ||
log.info("syncAllUsersViewHistory"); | ||
List<User> users = userLookupService.findAll(); | ||
for (User user : users) { | ||
newsViewHistService.updateNewsHist(user.getId()); | ||
} | ||
} // 모든 유저에 대해 시청기록 동기화 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.