From f433c2cbbd07a936f8644345b01b53faf2a7f8c9 Mon Sep 17 00:00:00 2001 From: junyong Date: Tue, 9 Sep 2025 01:32:08 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=AA=85=EC=86=8C=20=ED=98=BC?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=A0=95=EB=B3=B4=20redis=20key=20?= =?UTF-8?q?=ED=86=B5=EC=9D=BC=20->=20=ED=98=BC=EC=9E=A1=EB=8F=84=20?= =?UTF-8?q?=EC=9D=BC=EA=B4=80=EC=84=B1=20=EB=B3=B4=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/home/service/HomeQueryService.kt | 8 +-- .../place/converter/PlaceDetailsConverter.kt | 2 +- .../service/PlaceCongestionQueryService.kt | 4 +- .../domain/place/service/PlaceQueryService.kt | 2 +- .../busan/domain/place/util/PlaceRedisUtil.kt | 59 ++++++++++--------- .../busan/domain/search/util/SearchUtil.kt | 2 +- 6 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/busanVibe/busan/domain/home/service/HomeQueryService.kt b/src/main/kotlin/busanVibe/busan/domain/home/service/HomeQueryService.kt index b655654..440449e 100644 --- a/src/main/kotlin/busanVibe/busan/domain/home/service/HomeQueryService.kt +++ b/src/main/kotlin/busanVibe/busan/domain/home/service/HomeQueryService.kt @@ -67,7 +67,7 @@ class HomeQueryService( val places = placeRepository.findAllWithImages() val placesWithCongestion = places.mapNotNull { place -> - val congestion = placeRedisUtil.getRedisCongestion(place.id) + val congestion = placeRedisUtil.getTimeCongestion(place.id) if (congestion != null) { place to congestion } else { @@ -85,7 +85,7 @@ class HomeQueryService( longitude = place.longitude?.toDouble(), type = place.type.korean, image = place.placeImages.firstOrNull()?.imgUrl.nullIfBlank(), - congestionLevel = congestion, + congestionLevel = congestion.toInt(), address = place.address ) } @@ -99,12 +99,12 @@ class HomeQueryService( return places.shuffled().take(5).map { place -> - val congestion = placeRedisUtil.getRedisCongestion(place.id) + val congestion = placeRedisUtil.getTimeCongestion(place.id) HomeResponseDTO.RecommendPlace( id = place.id, name = place.name, - congestionLevel = congestion, + congestionLevel = congestion.toInt(), type = place.type.korean, image = place.placeImages.firstOrNull()?.imgUrl.nullIfBlank(), latitude = place.latitude?.toDouble(), diff --git a/src/main/kotlin/busanVibe/busan/domain/place/converter/PlaceDetailsConverter.kt b/src/main/kotlin/busanVibe/busan/domain/place/converter/PlaceDetailsConverter.kt index 667b022..34aef04 100644 --- a/src/main/kotlin/busanVibe/busan/domain/place/converter/PlaceDetailsConverter.kt +++ b/src/main/kotlin/busanVibe/busan/domain/place/converter/PlaceDetailsConverter.kt @@ -28,7 +28,7 @@ class PlaceDetailsConverter( name = place.name, type = place.type.capitalEnglish, img = placeImages.map { it.imgUrl }, - congestionLevel = redisUtil.getRedisCongestion(place.id), + congestionLevel = redisUtil.getTimeCongestion(place.id).toInt(), likeAmount = placeLikes.size, address = place.address, isLike = isLike, diff --git a/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceCongestionQueryService.kt b/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceCongestionQueryService.kt index 0510b60..df612bd 100644 --- a/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceCongestionQueryService.kt +++ b/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceCongestionQueryService.kt @@ -68,7 +68,7 @@ class PlaceCongestionQueryService( type = it.type.capitalEnglish, latitude = it.latitude, longitude = it.longitude, - congestionLevel = placeRedisUtil.getRedisCongestion(it.id) + congestionLevel = placeRedisUtil.getTimeCongestion(it.id).toInt() ) } @@ -98,7 +98,7 @@ class PlaceCongestionQueryService( return PlaceMapResponseDTO.PlaceDefaultInfoDto( id = place.id, name = place.name, - congestionLevel = placeRedisUtil.getRedisCongestion(place.id), + congestionLevel = placeRedisUtil.getTimeCongestion(place.id).toInt(), latitude = place.latitude, longitude = place.longitude, address = place.address, diff --git a/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceQueryService.kt b/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceQueryService.kt index 33ee4ca..ace30f1 100644 --- a/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceQueryService.kt +++ b/src/main/kotlin/busanVibe/busan/domain/place/service/PlaceQueryService.kt @@ -69,7 +69,7 @@ class PlaceQueryService( // 혼잡도 조회 val placeRedisUtil = PlaceRedisUtil(redisTemplate) - val congestionMap: Map = placeIdList.associateWith { placeRedisUtil.getRedisCongestion(it) } + val congestionMap: Map = placeIdList.associateWith { placeRedisUtil.getTimeCongestion(it).toInt() } // DTO 변환 diff --git a/src/main/kotlin/busanVibe/busan/domain/place/util/PlaceRedisUtil.kt b/src/main/kotlin/busanVibe/busan/domain/place/util/PlaceRedisUtil.kt index 4ed71e6..04aee1b 100644 --- a/src/main/kotlin/busanVibe/busan/domain/place/util/PlaceRedisUtil.kt +++ b/src/main/kotlin/busanVibe/busan/domain/place/util/PlaceRedisUtil.kt @@ -14,19 +14,43 @@ class PlaceRedisUtil( private val log = LoggerFactory.getLogger("busanVibe.busan.domain.place") // 임의로 혼잡도 생성하여 반환. 레디스 키 값으로 저장함. - fun getRedisCongestion(placeId: Long?): Int{ +// fun getRedisCongestion(placeId: Long?): Int{ +// +// val key = "place:congestion:$placeId" +// val randomCongestion = getRandomCongestion().toInt().toString() +// +// redisTemplate.opsForValue() +// .set(key, randomCongestion) +// +// return Integer.parseInt(randomCongestion) +// } - val key = "place:congestion:$placeId" - val randomCongestion = getRandomCongestion().toInt().toString() + // 지정 시간 혼잡도 조회 + // null이면 현재시간 기준 + fun getTimeCongestion(placeId: Long?, dateTime: LocalDateTime?): Float { + + val dateTime = dateTime ?: LocalDateTime.now() - redisTemplate.opsForValue() - .set(key, randomCongestion) + val roundedHour = (dateTime.hour / 3) * 3 + val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour" + + val value = redisTemplate.opsForValue().get(key) - return Integer.parseInt(randomCongestion) + return (if (value != null) { + value.toFloatOrNull() ?: 0 + } else { + setPlaceTimeCongestion(placeId, dateTime.withHour(roundedHour)) + val newValue = redisTemplate.opsForValue().get(key) + newValue?.toFloatOrNull() ?: 0 + }) as Float + } + + fun getTimeCongestion(placeId: Long?):Float{ + return getTimeCongestion(placeId, LocalDateTime.now()) } // 시간 혼잡도 설정 - fun setPlaceTimeCongestion(placeId: Long, dateTime: LocalDateTime) { + private fun setPlaceTimeCongestion(placeId: Long?, dateTime: LocalDateTime) { val roundedHour = (dateTime.hour / 3) * 3 val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour" val congestion = getRandomCongestion().toString() @@ -40,27 +64,6 @@ class PlaceRedisUtil( } } - // 지정 시간 혼잡도 조회 - // null이면 현재시간 기준 - fun getTimeCongestion(placeId: Long, dateTime: LocalDateTime?): Float { - - val dateTime = dateTime ?: LocalDateTime.now() - - val roundedHour = (dateTime.hour / 3) * 3 - val key = "place:congestion:$placeId-${dateTime.year}-${dateTime.monthValue}-${dateTime.dayOfMonth}-$roundedHour" - - val value = redisTemplate.opsForValue().get(key) - - return if (value != null) { - log.info("이미 존재하는 혼잡도 기록: $key, 기존 혼잡도: $value") - value.toFloatOrNull() ?: 0f - } else { - setPlaceTimeCongestion(placeId, dateTime.withHour(roundedHour)) - val newValue = redisTemplate.opsForValue().get(key) - newValue?.toFloatOrNull() ?: 0f - } - } - // 혼잡도 생성 (1.0 ~ 5.0 사이의 Float) private fun getRandomCongestion(): Float { return (Math.random() * 4 + 1).toFloat() diff --git a/src/main/kotlin/busanVibe/busan/domain/search/util/SearchUtil.kt b/src/main/kotlin/busanVibe/busan/domain/search/util/SearchUtil.kt index e00527e..9d46de7 100644 --- a/src/main/kotlin/busanVibe/busan/domain/search/util/SearchUtil.kt +++ b/src/main/kotlin/busanVibe/busan/domain/search/util/SearchUtil.kt @@ -59,7 +59,7 @@ class SearchUtil( latitude = place.latitude?.toDouble(), longitude = place.longitude?.toDouble(), address = place.address, - congestionLevel = placeRedisUtil.getRedisCongestion(place.id), + congestionLevel = placeRedisUtil.getTimeCongestion(place.id).toInt(), isLike = place.placeLikes.any { it.user == currentUser }, startDate = null, endDate = null,