-
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.
Browse files
Browse the repository at this point in the history
* [Feat/#187] Implement Caching for viewCount in Question * [Feat/#187] Implement Caching for viewCount in Siren * [Refactor/#187] Implement SirenCacheService * [Feat/#187] Remove @Modifying and @transactional from QuestionRepository * [Feat/#187] Update fixedRate in @scheduled * [Refactor/#187] Extract cache operations to CacheService * [Refactor/#187] Combine view count retrieval and increment logic * [Feat/#187] Add CacheConfig for Redis caching * [Refactor/#187] Remove useless log * [Refactor/#187] Remove getData method in RedisService
- Loading branch information
1 parent
2bbb961
commit 392be13
Showing
18 changed files
with
225 additions
and
41 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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/com/example/waggle/domain/board/question/service/QuestionCacheService.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,54 @@ | ||
package com.example.waggle.domain.board.question.service; | ||
|
||
import com.example.waggle.domain.board.question.repository.QuestionRepository; | ||
import com.example.waggle.domain.member.service.RedisService; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
@RequiredArgsConstructor | ||
@Service | ||
public class QuestionCacheService { | ||
|
||
private final RedisService redisService; | ||
private final QuestionRepository questionRepository; | ||
private final String VIEW_COUNT_PREFIX = "viewCount::"; | ||
private final String BOARD_PREFIX = "board::"; | ||
|
||
@CachePut(value = "viewCounts", key = "#boardId") | ||
public Long applyViewCountToRedis(Long boardId) { | ||
String viewCountKey = VIEW_COUNT_PREFIX + boardId; | ||
String currentViewCount = redisService.getValue(viewCountKey); | ||
if (currentViewCount != null) { | ||
return redisService.increment(viewCountKey); | ||
} else { | ||
Long initialViewCount = questionRepository.findViewCountByBoardId(boardId); | ||
return initialViewCount + 1; | ||
} | ||
} | ||
|
||
@Scheduled(fixedRate = 1000 * 60 * 3) | ||
public void applyViewCountToRDB() { | ||
Set<String> viewCountKeys = redisService.getKeysByPattern(VIEW_COUNT_PREFIX + "*"); | ||
if (Objects.requireNonNull(viewCountKeys).isEmpty()) { | ||
return; | ||
} | ||
|
||
for (String viewCntKey : viewCountKeys) { | ||
Long boardId = extractBoardIdFromKey(viewCntKey); | ||
Long viewCount = Long.parseLong(redisService.getValue(viewCntKey)); | ||
questionRepository.applyViewCntToRDB(boardId, viewCount); | ||
redisService.deleteData(viewCntKey); | ||
redisService.deleteData(BOARD_PREFIX + boardId); | ||
} | ||
} | ||
|
||
private static Long extractBoardIdFromKey(String key) { | ||
return Long.parseLong(key.split("::")[1]); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/waggle/domain/board/siren/service/SirenCacheService.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,56 @@ | ||
package com.example.waggle.domain.board.siren.service; | ||
|
||
import com.example.waggle.domain.board.siren.repository.SirenRepository; | ||
import com.example.waggle.domain.member.service.RedisService; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Transactional | ||
@RequiredArgsConstructor | ||
@Service | ||
public class SirenCacheService { | ||
|
||
private final RedisService redisService; | ||
private final SirenRepository sirenRepository; | ||
private final String VIEW_COUNT_PREFIX = "viewCounts::"; | ||
private final String BOARD_PREFIX = "board::"; | ||
|
||
@CachePut(value = "viewCounts", key = "#boardId") | ||
public Long applyViewCountToRedis(Long boardId) { | ||
String viewCountKey = VIEW_COUNT_PREFIX + boardId; | ||
String currentViewCount = redisService.getValue(viewCountKey); | ||
if (currentViewCount != null) { | ||
return redisService.increment(viewCountKey); | ||
} else { | ||
Long initialViewCount = sirenRepository.findViewCountByBoardId(boardId); | ||
return initialViewCount + 1; | ||
} | ||
} | ||
|
||
@Scheduled(fixedRate = 1000 * 60 * 3) | ||
public void applyViewCountToRDB() { | ||
Set<String> viewCountKeys = redisService.getKeysByPattern(VIEW_COUNT_PREFIX + "*"); | ||
if (Objects.requireNonNull(viewCountKeys).isEmpty()) { | ||
return; | ||
} | ||
|
||
for (String viewCntKey : viewCountKeys) { | ||
Long boardId = extractBoardIdFromKey(viewCntKey); | ||
Long viewCount = Long.parseLong(redisService.getValue(viewCntKey)); | ||
sirenRepository.applyViewCntToRDB(boardId, viewCount); | ||
redisService.deleteData(viewCntKey); | ||
redisService.deleteData(BOARD_PREFIX + boardId); | ||
} | ||
} | ||
|
||
private static Long extractBoardIdFromKey(String key) { | ||
return Long.parseLong(key.split("::")[1]); | ||
} | ||
} |
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.