Skip to content

Commit

Permalink
refactor: redis List자료구조에 ttl 설정하기
Browse files Browse the repository at this point in the history
- 한 게임당 최대 300초가 넘지 않는다고 판단하여 300으로 설정 -> 추후 논의 필요

- 한 문제가 지날 때마다 List key를 삭제
  • Loading branch information
Jeongjjuna committed Sep 18, 2024
1 parent eb727fd commit cc966ba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import yjh.cstar.game.presentation.request.RankingCreateRequest
import yjh.cstar.game.presentation.response.QuizInfoResponse
import yjh.cstar.game.presentation.response.RankingResponse
import yjh.cstar.quiz.domain.Quiz
import yjh.cstar.util.RedisUtil
import yjh.cstar.websocket.application.BroadCastService
import java.time.LocalDateTime

Expand All @@ -25,6 +26,7 @@ class GameEngineService(
private val answerValidationService: AnswerValidationService,
private val broadCastService: BroadCastService,
private val redisRankingService: RedisRankingService,
private val redisUtil: RedisUtil,
) {

companion object {
Expand Down Expand Up @@ -58,6 +60,8 @@ class GameEngineService(
QuizInfoResponse(quiz.id, quiz.question)
)

redisUtil.delete("roomId : " + roomId + ", " + "quizId : " + quiz.id)

val startTime = System.currentTimeMillis()
var notExistWinner = true
while (checkTimeIn(startTime) && notExistWinner) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/yjh/cstar/util/RedisUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class RedisUtil(
private val redisTemplate: RedisTemplate<String, String>,
) {

fun rpush(key: String, value: String): Long? {
return redisTemplate.opsForList().rightPush(key, value)
fun rpush(key: String, value: String, ttl: Long? = 9999): Long? {
val listSize = redisTemplate.opsForList().rightPush(key, value)
ttl?.let {
redisTemplate.expire(key, ttl, TimeUnit.SECONDS)
}
return listSize
}

fun lpop(key: String, timeout: Long = 60, timeUnit: TimeUnit = TimeUnit.SECONDS): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class GameAnswerPushRepositoryAdapter(
val key = "roomId : " + roomId.toString() + ", " + "quizId : " + quizId.toString()
val answerResultEntity = AnswerResultEntity.from(answerResult)
val value = objectMapper.writeValueAsString(answerResultEntity)
redisUtil.rpush(key, value)
val ttl = 300L
redisUtil.rpush(key, value, ttl)
}
}

0 comments on commit cc966ba

Please sign in to comment.