Skip to content

Commit b79d50a

Browse files
committed
Refactor: 의도에 맞는 의미를 전달하고자 아키텍처의 의도에 맞는 클래스 네이밍을 변겨 함수의 네이밍 변경
- cache aside 패턴으로 사용하고 있는 클래스는 클래스명에 'CacheAside' 포함 - cache에 데이터를 직접 쓰는 함수가 포함된 클래스는 클래스명에 'CacheUpdater' 포함 - 인위적으로 DB를 검색하여 cache를 최신화하는 함수가 포함된 클래스는 클래스명에 'CacheRefresher' 포함
1 parent 33506f7 commit b79d50a

File tree

16 files changed

+77
-78
lines changed

16 files changed

+77
-78
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package io.ticketaka.api.common.domain
22

33
interface EventBroker {
4-
fun produceAndConsume(domainEvent: DomainEvent)
4+
fun produce(domainEvent: DomainEvent)
55
}

src/main/kotlin/io/ticketaka/api/common/infrastructure/event/DBEventBroker.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component
88
class DBEventBroker(
99
private val eventDispatcher: EventDispatcher,
1010
) : EventBroker {
11-
override fun produceAndConsume(domainEvent: DomainEvent) {
12-
eventDispatcher.dispatchAndConsume(domainEvent)
11+
override fun produce(domainEvent: DomainEvent) {
12+
eventDispatcher.dispatch(domainEvent)
1313
}
1414
}

src/main/kotlin/io/ticketaka/api/common/infrastructure/event/EventDispatcher.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EventDispatcher(
1212
private val pointRechargeEventConsumer: PointRechargeEventConsumer,
1313
private val pointChargeEventConsumer: PointChargeEventConsumer,
1414
) {
15-
fun dispatchAndConsume(event: DomainEvent) {
15+
fun dispatch(event: DomainEvent) {
1616
when (event) {
1717
is PointRechargeEvent -> {
1818
pointRechargeEventConsumer.offer(event)

src/main/kotlin/io/ticketaka/api/concert/application/ConcertQueryService.kt src/main/kotlin/io/ticketaka/api/concert/application/ConcertCacheAsideQueryService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.springframework.stereotype.Service
1010
import java.time.LocalDate
1111

1212
@Service
13-
class ConcertQueryService(
13+
class ConcertCacheAsideQueryService(
1414
private val concertRepository: ConcertRepository,
1515
private val seatRepository: SeatRepository,
1616
) {

src/main/kotlin/io/ticketaka/api/concert/application/ConcertSeatService.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import java.time.LocalDate
1212

1313
@Service
1414
class ConcertSeatService(
15-
private val concertQueryService: ConcertQueryService,
15+
private val concertCacheAsideQueryService: ConcertCacheAsideQueryService,
1616
private val seatRepository: SeatRepository,
1717
private val concertRepository: ConcertRepository,
1818
) {
1919
fun getSeatNumbers(date: LocalDate): List<SeatResult> {
20-
val concert = concertQueryService.getConcert(date)
21-
return concertQueryService.getConcertSeatNumbers(concert.id).map { SeatResult(it.number, it.status) }
20+
val concert = concertCacheAsideQueryService.getConcert(date)
21+
return concertCacheAsideQueryService.getConcertSeatNumbers(concert.id).map { SeatResult(it.number, it.status) }
2222
}
2323

2424
@Transactional(readOnly = true)

src/main/kotlin/io/ticketaka/api/point/application/PaymentService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.springframework.transaction.annotation.Transactional
1313
@Transactional(readOnly = true)
1414
class PaymentService(
1515
private val paymentRepository: PaymentRepository,
16-
private val pointQueryService: PointQueryService,
16+
private val pointCacheAsideQueryService: PointCacheAsideQueryService,
1717
private val applicationEventPublisher: ApplicationEventPublisher,
1818
) {
1919
@Transactional

src/main/kotlin/io/ticketaka/api/point/application/PointQueryService.kt src/main/kotlin/io/ticketaka/api/point/application/PointCacheAsideQueryService.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ import org.springframework.transaction.annotation.Transactional
99

1010
@Service
1111
@Transactional(readOnly = true)
12-
class PointQueryService(
12+
class PointCacheAsideQueryService(
1313
private val pointRepository: PointRepository,
1414
) {
1515
@Cacheable(value = ["point"], key = "#pointId", sync = true)
1616
fun getPoint(pointId: Long): Point {
1717
return pointRepository.findById(pointId) ?: throw NotFoundException("포인트를 찾을 수 없습니다.")
1818
}
19-
20-
fun getPointForUpdate(pointId: Long): Point {
21-
return pointRepository.findByIdForUpdate(pointId) ?: throw NotFoundException("포인트를 찾을 수 없습니다.")
22-
}
2319
}

src/main/kotlin/io/ticketaka/api/point/application/PointService.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ import io.ticketaka.api.point.application.dto.RechargeCommand
66
import io.ticketaka.api.point.domain.PointBalanceCacheUpdater
77
import io.ticketaka.api.point.domain.PointRechargeEvent
88
import io.ticketaka.api.point.domain.PointRepository
9-
import io.ticketaka.api.user.application.TokenUserQueryService
9+
import io.ticketaka.api.user.application.TokenUserCacheAsideQueryService
1010
import org.springframework.context.ApplicationEventPublisher
1111
import org.springframework.stereotype.Service
1212
import org.springframework.transaction.annotation.Transactional
1313

1414
@Service
1515
class PointService(
16-
private val tokenUserQueryService: TokenUserQueryService,
17-
private val pointQueryService: PointQueryService,
16+
private val tokenUserCacheAsideQueryService: TokenUserCacheAsideQueryService,
17+
private val pointCacheAsideQueryService: PointCacheAsideQueryService,
1818
private val pointBalanceCacheUpdater: PointBalanceCacheUpdater,
1919
private val applicationEventPublisher: ApplicationEventPublisher,
2020
private val pointRepository: PointRepository,
2121
) {
2222
@Transactional
2323
fun recharge(rechargeCommand: RechargeCommand) {
24-
val user = tokenUserQueryService.getUser(rechargeCommand.userId)
25-
val point = pointQueryService.getPoint(user.pointId)
24+
val user = tokenUserCacheAsideQueryService.getUser(rechargeCommand.userId)
25+
val point = pointCacheAsideQueryService.getPoint(user.pointId)
2626
pointBalanceCacheUpdater.recharge(point.id, rechargeCommand.amount)
2727
applicationEventPublisher.publishEvent(PointRechargeEvent(user.id, point.id, rechargeCommand.amount))
2828
}
2929

3030
fun getBalance(userId: Long): BalanceQueryModel {
31-
val user = tokenUserQueryService.getUser(userId)
32-
val point = pointQueryService.getPoint(user.pointId)
31+
val user = tokenUserCacheAsideQueryService.getUser(userId)
32+
val point = pointCacheAsideQueryService.getPoint(user.pointId)
3333
return BalanceQueryModel(user.id, point.balance)
3434
}
3535

src/main/kotlin/io/ticketaka/api/point/infrastructure/event/PointEventHandler.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class PointEventHandler(
1212
) {
1313
@EventListener
1414
fun handle(event: PointRechargeEvent) {
15-
eventBroker.produceAndConsume(event)
15+
eventBroker.produce(event)
1616
}
1717

1818
@EventListener
1919
fun handle(event: PointChargeEvent) {
20-
eventBroker.produceAndConsume(event)
20+
eventBroker.produce(event)
2121
}
2222
}

src/main/kotlin/io/ticketaka/api/reservation/application/ReservationService.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import io.ticketaka.api.reservation.application.dto.CreateReservationCommand
77
import io.ticketaka.api.reservation.domain.reservation.Reservation
88
import io.ticketaka.api.reservation.domain.reservation.ReservationRepository
99
import io.ticketaka.api.reservation.domain.reservation.ReservationSeatAllocator
10-
import io.ticketaka.api.user.application.TokenUserQueryService
10+
import io.ticketaka.api.user.application.TokenUserCacheAsideQueryService
1111
import org.springframework.scheduling.annotation.Async
1212
import org.springframework.stereotype.Service
1313
import org.springframework.transaction.annotation.Transactional
1414

1515
@Service
1616
@Transactional(readOnly = true)
1717
class ReservationService(
18-
private val tokenQueryUserService: TokenUserQueryService,
18+
private val tokenUserCacheAsideQueryService: TokenUserCacheAsideQueryService,
1919
private val concertSeatService: ConcertSeatService,
2020
private val reservationRepository: ReservationRepository,
2121
private val reservationSeatAllocator: ReservationSeatAllocator,
@@ -24,7 +24,7 @@ class ReservationService(
2424
@Async
2525
@Transactional
2626
fun createReservation(command: CreateReservationCommand) {
27-
val user = tokenQueryUserService.getUser(command.userId)
27+
val user = tokenUserCacheAsideQueryService.getUser(command.userId)
2828
val concert = concertSeatService.getAvailableConcert(command.date)
2929
val seats = concertSeatService.reserveSeat(concert.id, command.seatNumbers)
3030
val reservation = reservationRepository.save(Reservation.createPendingReservation(user.id, concert.id))
@@ -38,7 +38,7 @@ class ReservationService(
3838
userId: Long,
3939
reservationId: Long,
4040
) {
41-
val user = tokenQueryUserService.getUser(userId)
41+
val user = tokenUserCacheAsideQueryService.getUser(userId)
4242
val reservation =
4343
reservationRepository.findById(reservationId)
4444
?: throw NotFoundException("예약을 찾을 수 없습니다.")

src/main/kotlin/io/ticketaka/api/user/application/TokenUserQueryService.kt src/main/kotlin/io/ticketaka/api/user/application/TokenUserCacheAsideQueryService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.springframework.cache.annotation.Cacheable
77
import org.springframework.stereotype.Service
88

99
@Service
10-
class TokenUserQueryService(
10+
class TokenUserCacheAsideQueryService(
1111
private val userRepository: UserRepository,
1212
) {
1313
@Cacheable(value = ["user"], key = "#id")

src/main/kotlin/io/ticketaka/api/user/application/TokenUserService.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import org.springframework.stereotype.Service
88
@Service
99
class TokenUserService(
1010
private val tokenWaitingMap: TokenWaitingMap,
11-
private val tokenUserQueryService: TokenUserQueryService,
11+
private val tokenUserCacheAsideQueryService: TokenUserCacheAsideQueryService,
1212
private val applicationEventPublisher: ApplicationEventPublisher,
1313
) {
1414
fun createToken(userId: Long): Long {
15-
val user = tokenUserQueryService.getUser(userId)
15+
val user = tokenUserCacheAsideQueryService.getUser(userId)
1616

1717
val token = Token.newInstance(user.id)
1818
token.pollAllEvents().forEach { applicationEventPublisher.publishEvent(it) }

src/test/kotlin/io/ticketaka/api/concert/application/ConcertSeatServiceTest.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ class ConcertSeatServiceTest {
5555
val concert = Concert.newInstance(date)
5656
val seatNumber = "1"
5757
val seat = Seat.newInstance(seatNumber, BigDecimal(1), concert.id)
58-
val mockConcertQueryService =
59-
mock<ConcertQueryService> {
58+
val mockConcertCacheAsideQueryService =
59+
mock<ConcertCacheAsideQueryService> {
6060
on { getConcert(any()) } doReturn concert
6161
on { getConcertSeatNumbers(any()) } doReturn setOf(seat)
6262
}
63-
val concertSeatService = ConcertSeatService(mockConcertQueryService, mock(), mock())
63+
val concertSeatService = ConcertSeatService(mockConcertCacheAsideQueryService, mock(), mock())
6464

6565
// when
6666
val result = concertSeatService.getSeatNumbers(date)
@@ -74,12 +74,12 @@ class ConcertSeatServiceTest {
7474
// given
7575
val date = LocalDate.of(2024, 4, 1)
7676
val concert = Concert.newInstance(date)
77-
val mockConcertQueryService =
78-
mock<ConcertQueryService> {
77+
val mockConcertCacheAsideQueryService =
78+
mock<ConcertCacheAsideQueryService> {
7979
on { getConcert(any()) } doReturn concert
8080
on { getConcertSeatNumbers(any()) } doReturn emptySet()
8181
}
82-
val concertSeatService = ConcertSeatService(mockConcertQueryService, mock(), mock())
82+
val concertSeatService = ConcertSeatService(mockConcertCacheAsideQueryService, mock(), mock())
8383

8484
// when
8585
val result = concertSeatService.getSeatNumbers(date)

src/test/kotlin/io/ticketaka/api/point/application/PointServiceTest.kt

+19-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import io.ticketaka.api.common.exception.BadClientRequestException
44
import io.ticketaka.api.point.application.dto.RechargeCommand
55
import io.ticketaka.api.point.domain.Point
66
import io.ticketaka.api.point.domain.PointBalanceCacheUpdater
7-
import io.ticketaka.api.user.application.TokenUserQueryService
7+
import io.ticketaka.api.user.application.TokenUserCacheAsideQueryService
88
import io.ticketaka.api.user.domain.User
99
import org.junit.jupiter.api.Assertions.assertEquals
1010
import org.junit.jupiter.api.Test
@@ -31,13 +31,13 @@ class PointServiceTest {
3131
20000.toBigDecimal(),
3232
)
3333

34-
val tokenUserQueryService =
35-
mock<TokenUserQueryService> {
34+
val tokenUserCacheAsideQueryService =
35+
mock<TokenUserCacheAsideQueryService> {
3636
on { getUser(any()) } doReturn user
3737
}
3838

39-
val pointQueryService =
40-
mock<PointQueryService> {
39+
val pointCacheAsideQueryService =
40+
mock<PointCacheAsideQueryService> {
4141
on { getPoint(any()) } doReturn point
4242
}
4343

@@ -48,7 +48,8 @@ class PointServiceTest {
4848
}
4949
}
5050

51-
val pointService = PointService(tokenUserQueryService, pointQueryService, pointBalanceCacheUpdater, mock(), mock())
51+
val pointService =
52+
PointService(tokenUserCacheAsideQueryService, pointCacheAsideQueryService, pointBalanceCacheUpdater, mock(), mock())
5253

5354
// when
5455
pointService.recharge(rechargeCommand)
@@ -69,13 +70,13 @@ class PointServiceTest {
6970
(-20000).toBigDecimal(),
7071
)
7172

72-
val tokenUserQueryService =
73-
mock<TokenUserQueryService> {
73+
val tokenUserCacheAsideQueryService =
74+
mock<TokenUserCacheAsideQueryService> {
7475
on { getUser(any()) } doReturn user
7576
}
7677

77-
val pointQueryService =
78-
mock<PointQueryService> {
78+
val pointCacheAsideQueryService =
79+
mock<PointCacheAsideQueryService> {
7980
on { getPoint(any()) } doReturn point
8081
}
8182
val pointBalanceCacheUpdater =
@@ -85,7 +86,8 @@ class PointServiceTest {
8586
}
8687
}
8788

88-
val pointService = PointService(tokenUserQueryService, pointQueryService, pointBalanceCacheUpdater, mock(), mock())
89+
val pointService =
90+
PointService(tokenUserCacheAsideQueryService, pointCacheAsideQueryService, pointBalanceCacheUpdater, mock(), mock())
8991

9092
// when
9193
val exception =
@@ -102,16 +104,17 @@ class PointServiceTest {
102104
// given
103105
val point = Point.newInstance()
104106
val user = User.newInstance(point.id)
105-
val tokenUserQueryService =
106-
mock<TokenUserQueryService> {
107+
val tokenUserCacheAsideQueryService =
108+
mock<TokenUserCacheAsideQueryService> {
107109
on { getUser(any()) } doReturn user
108110
}
109-
val pointQueryService =
110-
mock<PointQueryService> {
111+
val pointCacheAsideQueryService =
112+
mock<PointCacheAsideQueryService> {
111113
on { getPoint(any()) } doReturn point
112114
}
113115
val pointBalanceCacheUpdater = mock<PointBalanceCacheUpdater>()
114-
val pointService = PointService(tokenUserQueryService, pointQueryService, pointBalanceCacheUpdater, mock(), mock())
116+
val pointService =
117+
PointService(tokenUserCacheAsideQueryService, pointCacheAsideQueryService, pointBalanceCacheUpdater, mock(), mock())
115118

116119
// when
117120
val balanceQueryModel = pointService.getBalance(user.id)

0 commit comments

Comments
 (0)