-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from mingj7235/feature/cache-for-read-concert
Cache 도입 - 콘서트 조회, 콘서트 스케쥴 조회에 Cache 를 도입한다.
- Loading branch information
Showing
37 changed files
with
900 additions
and
1,399 deletions.
There are no files selected for viewing
7 changes: 2 additions & 5 deletions
7
src/main/kotlin/com/hhplus/concert/business/application/dto/QueueServiceDto.kt
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,18 +1,15 @@ | ||
package com.hhplus.concert.business.application.dto | ||
|
||
import com.hhplus.concert.common.type.QueueStatus | ||
import java.time.LocalDateTime | ||
|
||
class QueueServiceDto { | ||
data class IssuedToken( | ||
val token: String, | ||
val createdAt: LocalDateTime, | ||
) | ||
|
||
data class Queue( | ||
val queueId: Long, | ||
val joinAt: LocalDateTime, | ||
val status: QueueStatus, | ||
val remainingWaitListCount: Int, | ||
val remainingWaitListCount: Long, | ||
val estimatedWaitTime: Long, | ||
) | ||
} |
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
66 changes: 18 additions & 48 deletions
66
src/main/kotlin/com/hhplus/concert/business/application/service/QueueService.kt
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,94 +1,64 @@ | ||
package com.hhplus.concert.business.application.service | ||
|
||
import com.hhplus.concert.business.application.dto.QueueServiceDto | ||
import com.hhplus.concert.business.domain.manager.QueueManager | ||
import com.hhplus.concert.business.domain.manager.UserManager | ||
import com.hhplus.concert.business.domain.manager.queue.QueueManager | ||
import com.hhplus.concert.common.type.QueueStatus | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.time.LocalDateTime | ||
|
||
@Service | ||
class QueueService( | ||
private val queueManager: QueueManager, | ||
private val userManager: UserManager, | ||
) { | ||
/** | ||
* userId 전제 검증 | ||
* 현재 웨이팅 상태인 queue 가 저장되어있는지 검증 | ||
* 있다면 기존 queue 상태를 초기화, 대기열에서 밀려남 | ||
* userId 를 통해 user 를 찾아온다. | ||
* waiting 상태로 queue 저장 및 token 발급 | ||
*/ | ||
@Transactional | ||
fun issueQueueToken(userId: Long): QueueServiceDto.IssuedToken { | ||
val user = userManager.findById(userId) | ||
val queue = | ||
queueManager.findByUserIdAndStatus( | ||
userId = user.id, | ||
queueStatus = QueueStatus.WAITING, | ||
) | ||
|
||
// queue 가 기존에 존재한다면, 그 queue 를 취소상태로 변경한다. | ||
if (queue != null) { | ||
queueManager.updateStatus(queue, QueueStatus.CANCELLED) | ||
} | ||
|
||
return QueueServiceDto.IssuedToken( | ||
token = queueManager.enqueueAndIssueToken(user), | ||
createdAt = LocalDateTime.now(), | ||
token = queueManager.enqueueAndIssueToken(user.id), | ||
) | ||
} | ||
|
||
/** | ||
* token 을 통해 queue 의 정보를 반환한다. | ||
* token 을 통해 queue 의 상태를 조회한다. | ||
* 현재 queue 상태가 waiting 상태라면 현재 대기열이 얼마나 남았는지를 계산하여 반환한다. | ||
* 그 밖의 상태라면, 얼마나 대기를 해야하는지 알 필요가 없으므로 0 을 반환한다. | ||
*/ | ||
@Transactional | ||
fun findQueueByToken(token: String): QueueServiceDto.Queue { | ||
val queue = queueManager.findByToken(token) | ||
val status = queueManager.getQueueStatus(token) | ||
val isWaiting = status == QueueStatus.WAITING | ||
|
||
val position = if (isWaiting) queueManager.getPositionInWaitingStatus(token) else NO_REMAINING_WAIT | ||
val estimatedWaitTime = if (isWaiting) queueManager.calculateEstimatedWaitSeconds(position) else NO_REMAINING_WAIT | ||
|
||
return QueueServiceDto.Queue( | ||
queueId = queue.id, | ||
status = queue.queueStatus, | ||
joinAt = queue.joinedAt, | ||
remainingWaitListCount = | ||
if (queue.queueStatus == QueueStatus.WAITING) { | ||
queueManager.getPositionInWaitingStatus(queue.id) | ||
} else { | ||
NO_REMAINING_WAIT_LIST_COUNT | ||
}, | ||
status = status, | ||
remainingWaitListCount = position, | ||
estimatedWaitTime = estimatedWaitTime, | ||
) | ||
} | ||
|
||
/** | ||
* 스케쥴러를 통해 queue 의 process 상태인 상태의 queue 개수를 유지시킨다. | ||
* 스케쥴러를 통해 WAITING 상태의 대기열을 PROCESSING 상태로 변경한다. | ||
*/ | ||
@Transactional | ||
fun maintainProcessingCount() { | ||
val neededToUpdateCount = ALLOWED_MAX_SIZE - queueManager.countByQueueStatus(QueueStatus.PROCESSING) | ||
|
||
if (neededToUpdateCount > 0) { | ||
queueManager.updateStatus( | ||
queueIds = queueManager.getNeededUpdateToProcessingIdsFromWaiting(neededToUpdateCount), | ||
queueStatus = QueueStatus.PROCESSING, | ||
) | ||
} | ||
fun updateToProcessingTokens() { | ||
queueManager.updateToProcessingTokens() | ||
} | ||
|
||
/** | ||
* 스케쥴러를 통해 일정 시간이 지났지만 여전히 WAITING 상태인 queue 를 CANCELLED 로 변환시킨다. | ||
* 스케쥴러를 통해 만료 시간이 지났지만 여전히 WAITING 상태인 대기열을 삭제한다. | ||
*/ | ||
@Transactional | ||
fun cancelExpiredWaitingQueue() { | ||
queueManager.updateStatus( | ||
queueIds = queueManager.getExpiredWaitingQueueIds(), | ||
queueStatus = QueueStatus.CANCELLED, | ||
) | ||
queueManager.removeExpiredWaitingQueue() | ||
} | ||
|
||
companion object { | ||
const val NO_REMAINING_WAIT_LIST_COUNT = 0 | ||
const val ALLOWED_MAX_SIZE = 100 | ||
const val NO_REMAINING_WAIT = 0L | ||
} | ||
} |
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.