-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInMemoryCachePointBalanceUpdater.kt
36 lines (33 loc) · 1.25 KB
/
InMemoryCachePointBalanceUpdater.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package io.ticketaka.api.point.infrastructure
import io.ticketaka.api.common.exception.NotFoundException
import io.ticketaka.api.point.domain.Point
import io.ticketaka.api.point.domain.PointBalanceUpdater
import org.springframework.cache.caffeine.CaffeineCacheManager
import org.springframework.stereotype.Component
import java.math.BigDecimal
@Component
class InMemoryCachePointBalanceUpdater(
private val caffeineCacheManager: CaffeineCacheManager,
) : PointBalanceUpdater {
override fun recharge(
pointId: Long,
amount: BigDecimal,
) {
val cache = caffeineCacheManager.getCache("point") ?: throw NotFoundException("point 캐시가 존재하지 않습니다.")
synchronized(cache) {
val point = cache.get(pointId, Point::class.java) ?: throw NotFoundException("포인트를 찾을 수 없습니다.")
point.recharge(amount)
cache.put(pointId, point)
}
}
override fun charge(
point: Point,
amount: BigDecimal,
) {
val cache = caffeineCacheManager.getCache("point") ?: throw IllegalStateException("point 캐시가 존재하지 않습니다.")
point.charge(amount)
synchronized(this) {
cache.put(point.id, point)
}
}
}