-
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
…nt-apply-stress-test Feature/#203 rush event apply stress test
- Loading branch information
Showing
8 changed files
with
85 additions
and
19 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
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
...rc/main/java/JGS/CasperEvent/domain/event/service/redisService/RushEventRedisService.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 JGS.CasperEvent.domain.event.service.redisService; | ||
|
||
import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Set; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RushEventRedisService { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
private final RushParticipantsRepository rushParticipantsRepository; | ||
|
||
public Long getOptionCount(Long eventId, int optionId) { | ||
String redisKey = getRedisKey(eventId, optionId); | ||
|
||
// Redis에서 값 조회 | ||
String cachedCount = redisTemplate.opsForValue().get(redisKey); | ||
|
||
if (cachedCount != null) { | ||
// Redis에 값이 있으면 캐시된 값 반환 | ||
return Long.valueOf(cachedCount); | ||
} else { | ||
// Redis에 값이 없으면 DB에서 값 조회 | ||
long countFromDb = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(eventId, optionId); | ||
|
||
// 조회한 값을 Redis에 저장 | ||
redisTemplate.opsForValue().set(redisKey, String.valueOf(countFromDb)); | ||
|
||
return countFromDb; | ||
} | ||
} | ||
|
||
public void incrementOptionCount(Long eventId, int optionId) { | ||
String redisKey = getRedisKey(eventId, optionId); | ||
|
||
// Redis에서 해당 키의 값을 증가시킴 | ||
redisTemplate.opsForValue().increment(redisKey); | ||
} | ||
|
||
private String getRedisKey(Long eventId, int optionId) { | ||
return "rushEvent:" + eventId + ":option:" + optionId; | ||
} | ||
|
||
public void clearAllrushEventRate() { | ||
// 특정 rushEventId에 대한 모든 옵션 키들을 가져와 삭제 | ||
String pattern = "rushEvent:*" + ":option:*"; | ||
Set<String> keys = redisTemplate.keys(pattern); | ||
if (keys != null && !keys.isEmpty()) { | ||
redisTemplate.delete(keys); | ||
} | ||
} | ||
} |
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