generated from pricelees/issue-pr-template
refactor: 기존 테마 로직에 캐시 적용 & 조회 시에는 DB 요청 횟수를 기록할 커스텀 메트릭 추가
This commit is contained in:
parent
cf90f0fb91
commit
a15adcbd16
@ -3,22 +3,18 @@ package com.sangdol.roomescape.theme.business
|
||||
import com.sangdol.common.persistence.IDGenerator
|
||||
import com.sangdol.roomescape.admin.business.AdminService
|
||||
import com.sangdol.roomescape.common.types.AuditingInfo
|
||||
import com.sangdol.roomescape.theme.dto.ThemeDetailResponse
|
||||
import com.sangdol.roomescape.theme.dto.ThemeSummaryListResponse
|
||||
import com.sangdol.roomescape.theme.dto.ThemeNameListResponse
|
||||
import com.sangdol.roomescape.theme.dto.ThemeCreateRequest
|
||||
import com.sangdol.roomescape.theme.dto.ThemeCreateResponse
|
||||
import com.sangdol.roomescape.theme.dto.ThemeUpdateRequest
|
||||
import com.sangdol.roomescape.theme.dto.*
|
||||
import com.sangdol.roomescape.theme.exception.ThemeErrorCode
|
||||
import com.sangdol.roomescape.theme.exception.ThemeException
|
||||
import com.sangdol.roomescape.theme.infrastructure.persistence.ThemeEntity
|
||||
import com.sangdol.roomescape.theme.infrastructure.persistence.ThemeRepository
|
||||
import com.sangdol.roomescape.theme.mapper.toDetailResponse
|
||||
import com.sangdol.roomescape.theme.mapper.toSummaryListResponse
|
||||
import com.sangdol.roomescape.theme.mapper.toEntity
|
||||
import com.sangdol.roomescape.theme.mapper.toNameListResponse
|
||||
import com.sangdol.roomescape.theme.mapper.toSummaryListResponse
|
||||
import io.github.oshai.kotlinlogging.KLogger
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import org.springframework.cache.annotation.CacheEvict
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
@ -81,7 +77,7 @@ class AdminThemeService(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@CacheEvict(cacheNames = ["theme-details"], key = "#id")
|
||||
@Transactional
|
||||
fun deleteTheme(id: Long) {
|
||||
log.info { "[deleteTheme] 테마 삭제 시작: id=${id}" }
|
||||
@ -93,6 +89,7 @@ class AdminThemeService(
|
||||
}
|
||||
}
|
||||
|
||||
@CacheEvict(cacheNames = ["theme-details"], key = "#id")
|
||||
@Transactional
|
||||
fun updateTheme(id: Long, request: ThemeUpdateRequest) {
|
||||
log.info { "[updateTheme] 테마 수정 시작: id=${id}, request=${request}" }
|
||||
|
||||
@ -10,6 +10,8 @@ import com.sangdol.roomescape.theme.mapper.toInfoResponse
|
||||
import com.sangdol.roomescape.theme.mapper.toListResponse
|
||||
import io.github.oshai.kotlinlogging.KLogger
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.micrometer.core.instrument.MeterRegistry
|
||||
import org.springframework.cache.annotation.Cacheable
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
@ -21,13 +23,19 @@ private val log: KLogger = KotlinLogging.logger {}
|
||||
|
||||
@Service
|
||||
class ThemeService(
|
||||
private val themeRepository: ThemeRepository
|
||||
private val themeRepository: ThemeRepository,
|
||||
meterRegistry: MeterRegistry
|
||||
) {
|
||||
private val themeDetailQueryRequestCount = meterRegistry.counter("theme.detail.query.requested")
|
||||
|
||||
@Cacheable(cacheNames = ["theme-details"], key="#id")
|
||||
@Transactional(readOnly = true)
|
||||
fun findInfoById(id: Long): ThemeInfoResponse {
|
||||
log.info { "[findInfoById] 테마 조회 시작: id=$id" }
|
||||
|
||||
val theme = themeRepository.findByIdOrNull(id) ?: run {
|
||||
val theme = themeRepository.findByIdOrNull(id)?.also {
|
||||
themeDetailQueryRequestCount.increment()
|
||||
} ?: run {
|
||||
log.warn { "[updateTheme] 테마 조회 실패: id=$id" }
|
||||
throw ThemeException(ThemeErrorCode.THEME_NOT_FOUND)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user