From 61b063240141b6565b4d1730caf20414293e1989 Mon Sep 17 00:00:00 2001 From: thguss Date: Wed, 21 Aug 2024 19:55:53 +0900 Subject: [PATCH 01/18] chore: configuration redis --- .../src/main/resources/application-local.yml | 2 ++ .../src/main/resources/application-prod.yml | 2 ++ .../output/redis/config/RedisCacheConfig.kt | 30 +++++++++++++++++++ .../piikii/output/redis/config/RedisConfig.kt | 8 +++-- .../cache-config/application-local.yml | 4 +-- .../cache-config/application-prod.yml | 5 ++-- 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt diff --git a/piikii-bootstrap/src/main/resources/application-local.yml b/piikii-bootstrap/src/main/resources/application-local.yml index f1966347..6969c1db 100644 --- a/piikii-bootstrap/src/main/resources/application-local.yml +++ b/piikii-bootstrap/src/main/resources/application-local.yml @@ -2,6 +2,8 @@ spring: config: activate: on-profile: local + import: + - classpath:cache-config/application-local.yml server: port: 8080 diff --git a/piikii-bootstrap/src/main/resources/application-prod.yml b/piikii-bootstrap/src/main/resources/application-prod.yml index dccd3dd3..59676d2a 100644 --- a/piikii-bootstrap/src/main/resources/application-prod.yml +++ b/piikii-bootstrap/src/main/resources/application-prod.yml @@ -2,6 +2,8 @@ spring: config: activate: on-profile: prod + import: + - classpath:cache-config/application-prod.yml server: port: 8080 diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt new file mode 100644 index 00000000..3dcb9528 --- /dev/null +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt @@ -0,0 +1,30 @@ +package com.piikii.output.redis.config + +import org.springframework.cache.CacheManager +import org.springframework.cache.annotation.EnableCaching +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.data.redis.cache.RedisCacheConfiguration +import org.springframework.data.redis.cache.RedisCacheManager +import org.springframework.data.redis.connection.RedisConnectionFactory +import org.springframework.data.redis.serializer.RedisSerializationContext +import org.springframework.data.redis.serializer.StringRedisSerializer +import java.time.Duration + +@Configuration +@EnableCaching +class RedisCacheConfig { + + @Bean + fun cacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager { + val redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofHours(1)) // 캐시 유효시간 설정 + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) + + return RedisCacheManager.RedisCacheManagerBuilder + .fromConnectionFactory(redisConnectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build() + } +} diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index db1dcaa0..57ff6e14 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -7,13 +7,15 @@ import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.json.JsonMapper import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory import org.springframework.data.redis.core.RedisTemplate +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer import org.springframework.data.redis.serializer.StringRedisSerializer -// TODO: 필요 시에 등록을 위해 설정 -// @Configuration +@Configuration +@EnableRedisRepositories class RedisConfig { @Value("\${redis.host}") private val redisHost: String? = null @@ -38,7 +40,7 @@ class RedisConfig { } @Bean - fun redistemplate(): RedisTemplate { + fun redisTemplate(): RedisTemplate { val redisTemplate = RedisTemplate() redisTemplate.connectionFactory = lettuceConnectionFactory() redisTemplate.keySerializer = StringRedisSerializer() diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml index 55fec8b9..ed90075d 100644 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml +++ b/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml @@ -3,5 +3,5 @@ spring: activate: on-profile: local redis: - host: localhost - port: 6379 \ No newline at end of file + host: ${REDIS_HOST} + port: 6379 diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml index d07c1c1b..afe974a1 100644 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml +++ b/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml @@ -2,6 +2,7 @@ spring: config: activate: on-profile: prod + redis: - host: localhost - port: 6379 \ No newline at end of file + host: ${REDIS_HOST} + port: 6379 From 16c56aacc1fbb7c980f7e5302a0a626aa27226b0 Mon Sep 17 00:00:00 2001 From: thguss Date: Wed, 21 Aug 2024 21:56:46 +0900 Subject: [PATCH 02/18] add: added cache in get course distance --- .../domain/course/CourseService.kt | 12 ++---- .../port/output/cache/CourseCachePort.kt | 9 +++++ .../redis/adapter/CourseRedisCacheAdapter.kt | 40 +++++++++++++++++++ .../piikii/output/redis/config/RedisConfig.kt | 17 +++++++- .../cache-config/application-local.yml | 2 + .../cache-config/application-prod.yml | 2 + 6 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt create mode 100644 piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt index c4c3ccb4..525dae89 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt @@ -6,13 +6,13 @@ import com.piikii.application.domain.place.Place import com.piikii.application.domain.schedule.Schedule import com.piikii.application.port.input.CourseUseCase import com.piikii.application.port.input.dto.response.CourseResponse +import com.piikii.application.port.output.cache.CourseCachePort import com.piikii.application.port.output.persistence.CourseQueryPort import com.piikii.application.port.output.persistence.PlaceCommandPort import com.piikii.application.port.output.persistence.PlaceQueryPort import com.piikii.application.port.output.persistence.RoomQueryPort import com.piikii.application.port.output.persistence.ScheduleQueryPort import com.piikii.application.port.output.persistence.VoteQueryPort -import com.piikii.application.port.output.web.NavigationPort import com.piikii.common.exception.ExceptionCode import com.piikii.common.exception.PiikiiException import org.springframework.stereotype.Service @@ -27,7 +27,7 @@ class CourseService( private val placeQueryPort: PlaceQueryPort, private val placeCommandPort: PlaceCommandPort, private val voteQueryPort: VoteQueryPort, - private val navigationPort: NavigationPort, + private val courseCachePort: CourseCachePort, ) : CourseUseCase { override fun isCourseExist(roomUid: UuidTypeId): Boolean { return courseQueryPort.isCourseExist(roomUid) @@ -123,17 +123,11 @@ class CourseService( confirmedPlace: Place, ): CoursePlace { val coordinate = confirmedPlace.getCoordinate() - return CoursePlace.from( schedule = schedule, place = confirmedPlace, coordinate = coordinate, - distance = - preCoursePlace?.coordinate?.let { preCoordinate -> - coordinate.let { coordinate -> - navigationPort.getDistance(start = preCoordinate, end = coordinate) - } - }, + distance = courseCachePort.getDistance(preCoursePlace, confirmedPlace), ) } diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt new file mode 100644 index 00000000..138c3723 --- /dev/null +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt @@ -0,0 +1,9 @@ +package com.piikii.application.port.output.cache + +import com.piikii.application.domain.course.CoursePlace +import com.piikii.application.domain.course.Distance +import com.piikii.application.domain.place.Place + +interface CourseCachePort { + fun getDistance(startPlace: CoursePlace?, endPlace: Place): Distance? +} diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt new file mode 100644 index 00000000..84920b12 --- /dev/null +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -0,0 +1,40 @@ +package com.piikii.output.redis.adapter + +import com.piikii.application.domain.course.CoursePlace +import com.piikii.application.domain.course.Distance +import com.piikii.application.domain.generic.LongTypeId +import com.piikii.application.domain.place.Place +import com.piikii.application.port.output.cache.CourseCachePort +import com.piikii.application.port.output.web.NavigationPort +import org.springframework.beans.factory.annotation.Value +import org.springframework.cache.annotation.Cacheable +import org.springframework.data.redis.core.RedisTemplate +import org.springframework.stereotype.Component + +@Component +class CourseRedisCacheAdapter( + private val navigationPort: NavigationPort, + private val redisTemplate: RedisTemplate +): CourseCachePort { + + @Cacheable( + value = ["Distances"], + key = "#key", + cacheManager = "cacheManager", + condition = "#key != null", + ) + override fun getDistance(startPlace: CoursePlace?, endPlace: Place): Distance? { + val key = getKey(startPlace?.placeId, endPlace.id) + println(key) + println(redisTemplate.keys("*")) + return startPlace?.coordinate?.let { preCoordinate -> + endPlace.getCoordinate().let { coordinate -> + navigationPort.getDistance(start = preCoordinate, end = coordinate) + } + } + } + + private fun getKey(startPlaceId: LongTypeId?, endPlaceId: LongTypeId): String? { + return startPlaceId?.let { "${it.getValue()}_${endPlaceId.getValue()}" } + } +} diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index 57ff6e14..87fccfbd 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -8,6 +8,9 @@ import com.fasterxml.jackson.databind.json.JsonMapper import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.data.redis.connection.RedisPassword +import org.springframework.data.redis.connection.RedisStandaloneConfiguration +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory import org.springframework.data.redis.core.RedisTemplate import org.springframework.data.redis.repository.configuration.EnableRedisRepositories @@ -23,6 +26,9 @@ class RedisConfig { @Value("\${redis.port}") private val redisPort = 0 + @Value("\${redis.password}") + private val redisPassword: String? = null + @Bean fun objectMapper(): ObjectMapper { return JsonMapper.builder() @@ -36,7 +42,16 @@ class RedisConfig { @Bean fun lettuceConnectionFactory(): LettuceConnectionFactory { - return LettuceConnectionFactory(redisHost!!, redisPort) + val redisConfig = RedisStandaloneConfiguration() + redisConfig.hostName = redisHost!! + redisConfig.port = redisPort + redisConfig.password = RedisPassword.of(redisPassword) + + val clientConfig = LettuceClientConfiguration.builder() + .useSsl() // SSL 사용 설정 + .build() + + return LettuceConnectionFactory(redisConfig, clientConfig) } @Bean diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml index ed90075d..590dae97 100644 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml +++ b/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml @@ -5,3 +5,5 @@ spring: redis: host: ${REDIS_HOST} port: 6379 + password: ${REDIS_PASSWORD} + ssl: true diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml index afe974a1..b328f997 100644 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml +++ b/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml @@ -6,3 +6,5 @@ spring: redis: host: ${REDIS_HOST} port: 6379 + password: ${REDIS_PASSWORD} + ssl: true From b07e9bb1d53283d3f0ada5ae66c55b84ea35feab Mon Sep 17 00:00:00 2001 From: thguss Date: Thu, 22 Aug 2024 01:19:07 +0900 Subject: [PATCH 03/18] add: added test logic --- .../output/redis/adapter/CourseRedisCacheAdapter.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt index 84920b12..59ebbfbc 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -25,6 +25,14 @@ class CourseRedisCacheAdapter( ) override fun getDistance(startPlace: CoursePlace?, endPlace: Place): Distance? { val key = getKey(startPlace?.placeId, endPlace.id) + + // test + if (key != null) { + redisTemplate.opsForValue().set(key, "someValue") + val cachedValue = redisTemplate.opsForValue().get(key) + println("Cached value: $cachedValue") + } + println(key) println(redisTemplate.keys("*")) return startPlace?.coordinate?.let { preCoordinate -> From 75325353d8f00f594ecbf28eea3cc41bd983e501 Mon Sep 17 00:00:00 2001 From: thguss Date: Thu, 22 Aug 2024 21:49:02 +0900 Subject: [PATCH 04/18] fix: updated cache configuration yml file --- .../src/main/resources/application-local.yml | 2 -- .../src/main/resources/application-prod.yml | 2 -- piikii-bootstrap/src/main/resources/application.yml | 1 + piikii-bootstrap/src/test/resources/application.yml | 1 + .../main/resources/cache-config/application-prod.yml | 10 ---------- .../{application-local.yml => application.yml} | 4 ---- 6 files changed, 2 insertions(+), 18 deletions(-) delete mode 100644 piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml rename piikii-output-cache/redis/src/main/resources/cache-config/{application-local.yml => application.yml} (60%) diff --git a/piikii-bootstrap/src/main/resources/application-local.yml b/piikii-bootstrap/src/main/resources/application-local.yml index 6969c1db..f1966347 100644 --- a/piikii-bootstrap/src/main/resources/application-local.yml +++ b/piikii-bootstrap/src/main/resources/application-local.yml @@ -2,8 +2,6 @@ spring: config: activate: on-profile: local - import: - - classpath:cache-config/application-local.yml server: port: 8080 diff --git a/piikii-bootstrap/src/main/resources/application-prod.yml b/piikii-bootstrap/src/main/resources/application-prod.yml index 59676d2a..dccd3dd3 100644 --- a/piikii-bootstrap/src/main/resources/application-prod.yml +++ b/piikii-bootstrap/src/main/resources/application-prod.yml @@ -2,8 +2,6 @@ spring: config: activate: on-profile: prod - import: - - classpath:cache-config/application-prod.yml server: port: 8080 diff --git a/piikii-bootstrap/src/main/resources/application.yml b/piikii-bootstrap/src/main/resources/application.yml index 6ae1dbc1..69417b26 100644 --- a/piikii-bootstrap/src/main/resources/application.yml +++ b/piikii-bootstrap/src/main/resources/application.yml @@ -7,6 +7,7 @@ spring: - classpath:avocado-config/application.yml - classpath:tmap-config/application.yml - classpath:application-actuator.yml + - classpath:cache-config/application.yml application: name: "piikii" messages: diff --git a/piikii-bootstrap/src/test/resources/application.yml b/piikii-bootstrap/src/test/resources/application.yml index d2143e51..a1d53417 100644 --- a/piikii-bootstrap/src/test/resources/application.yml +++ b/piikii-bootstrap/src/test/resources/application.yml @@ -7,6 +7,7 @@ spring: - classpath:avocado-config/application.yml - classpath:database-config/application-test.yml - classpath:tmap-config/application.yml + - classpath:cache-config/application.yml application: name: "piikii" messages: diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml deleted file mode 100644 index b328f997..00000000 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-prod.yml +++ /dev/null @@ -1,10 +0,0 @@ -spring: - config: - activate: - on-profile: prod - -redis: - host: ${REDIS_HOST} - port: 6379 - password: ${REDIS_PASSWORD} - ssl: true diff --git a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml b/piikii-output-cache/redis/src/main/resources/cache-config/application.yml similarity index 60% rename from piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml rename to piikii-output-cache/redis/src/main/resources/cache-config/application.yml index 590dae97..c9637c51 100644 --- a/piikii-output-cache/redis/src/main/resources/cache-config/application-local.yml +++ b/piikii-output-cache/redis/src/main/resources/cache-config/application.yml @@ -1,7 +1,3 @@ -spring: - config: - activate: - on-profile: local redis: host: ${REDIS_HOST} port: 6379 From dfc015824b15d8b7901a1c12231fce95647e6ab7 Mon Sep 17 00:00:00 2001 From: thguss Date: Thu, 22 Aug 2024 21:50:06 +0900 Subject: [PATCH 05/18] add: added redis cache --- .../application/domain/course/Distance.kt | 4 +-- .../port/output/cache/CourseCachePort.kt | 5 ++- .../domain/course/CourseServiceTest.kt | 18 ++++++++--- .../redis/adapter/CourseRedisCacheAdapter.kt | 32 +++++-------------- .../output/redis/config/RedisCacheConfig.kt | 15 ++++++--- .../piikii/output/redis/config/RedisConfig.kt | 7 ++-- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt index 9b7850c2..28ea6136 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt @@ -1,8 +1,8 @@ package com.piikii.application.domain.course data class Distance( - val totalDistanceMeter: Int?, - val totalTimeMinute: Int?, + val totalDistanceMeter: Int? = null, + val totalTimeMinute: Int? = null, ) { companion object { val EMPTY = Distance(null, null) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt index 138c3723..decf5486 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt @@ -5,5 +5,8 @@ import com.piikii.application.domain.course.Distance import com.piikii.application.domain.place.Place interface CourseCachePort { - fun getDistance(startPlace: CoursePlace?, endPlace: Place): Distance? + fun getDistance( + startPlace: CoursePlace?, + endPlace: Place, + ): Distance? } diff --git a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt index c81306f9..ea4c0666 100644 --- a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt +++ b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt @@ -5,13 +5,13 @@ import com.piikii.application.domain.fixture.RoomFixture import com.piikii.application.domain.fixture.ScheduleFixture import com.piikii.application.domain.fixture.VoteFixture import com.piikii.application.domain.vote.VoteResult +import com.piikii.application.port.output.cache.CourseCachePort import com.piikii.application.port.output.persistence.CourseQueryPort import com.piikii.application.port.output.persistence.PlaceCommandPort import com.piikii.application.port.output.persistence.PlaceQueryPort import com.piikii.application.port.output.persistence.RoomQueryPort import com.piikii.application.port.output.persistence.ScheduleQueryPort import com.piikii.application.port.output.persistence.VoteQueryPort -import com.piikii.application.port.output.web.NavigationPort import com.piikii.common.exception.PiikiiException import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -45,7 +45,7 @@ class CourseServiceTest { lateinit var voteQueryPort: VoteQueryPort @Mock - lateinit var navigationPort: NavigationPort + lateinit var courseCachePort: CourseCachePort @Mock lateinit var courseQueryPort: CourseQueryPort @@ -132,9 +132,17 @@ class CourseServiceTest { ), ) - val coordinate1 = Coordinate(places[0].longitude, places[0].latitude) - val coordinate2 = Coordinate(places[2].longitude, places[2].latitude) - given(navigationPort.getDistance(coordinate1, coordinate2)) + val coursePlace = + CoursePlace.from( + schedule = schedules[0], + place = places[0], + coordinate = Coordinate(places[0].longitude, places[0].latitude), + distance = Distance.EMPTY, + ) + + given(courseCachePort.getDistance(null, places[0])) + .willReturn(Distance.EMPTY) + given(courseCachePort.getDistance(coursePlace, places[2])) .willReturn(Distance(100, 5)) val updatedPlace = places[2].copy(confirmed = true) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt index 59ebbfbc..24027663 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -2,47 +2,31 @@ package com.piikii.output.redis.adapter import com.piikii.application.domain.course.CoursePlace import com.piikii.application.domain.course.Distance -import com.piikii.application.domain.generic.LongTypeId import com.piikii.application.domain.place.Place import com.piikii.application.port.output.cache.CourseCachePort import com.piikii.application.port.output.web.NavigationPort -import org.springframework.beans.factory.annotation.Value import org.springframework.cache.annotation.Cacheable -import org.springframework.data.redis.core.RedisTemplate import org.springframework.stereotype.Component @Component class CourseRedisCacheAdapter( private val navigationPort: NavigationPort, - private val redisTemplate: RedisTemplate -): CourseCachePort { - +) : CourseCachePort { @Cacheable( value = ["Distances"], - key = "#key", + key = "#startPlace?.placeId + '_' + #endPlace.id", cacheManager = "cacheManager", - condition = "#key != null", + condition = "#startPlace != null", + unless = "#result == null", ) - override fun getDistance(startPlace: CoursePlace?, endPlace: Place): Distance? { - val key = getKey(startPlace?.placeId, endPlace.id) - - // test - if (key != null) { - redisTemplate.opsForValue().set(key, "someValue") - val cachedValue = redisTemplate.opsForValue().get(key) - println("Cached value: $cachedValue") - } - - println(key) - println(redisTemplate.keys("*")) + override fun getDistance( + startPlace: CoursePlace?, + endPlace: Place, + ): Distance? { return startPlace?.coordinate?.let { preCoordinate -> endPlace.getCoordinate().let { coordinate -> navigationPort.getDistance(start = preCoordinate, end = coordinate) } } } - - private fun getKey(startPlaceId: LongTypeId?, endPlaceId: LongTypeId): String? { - return startPlaceId?.let { "${it.getValue()}_${endPlaceId.getValue()}" } - } } diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt index 3dcb9528..8b6e63cb 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt @@ -7,6 +7,7 @@ import org.springframework.context.annotation.Configuration import org.springframework.data.redis.cache.RedisCacheConfiguration import org.springframework.data.redis.cache.RedisCacheManager import org.springframework.data.redis.connection.RedisConnectionFactory +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer import org.springframework.data.redis.serializer.RedisSerializationContext import org.springframework.data.redis.serializer.StringRedisSerializer import java.time.Duration @@ -14,13 +15,17 @@ import java.time.Duration @Configuration @EnableCaching class RedisCacheConfig { - @Bean fun cacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager { - val redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() - .entryTtl(Duration.ofHours(1)) // 캐시 유효시간 설정 - .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) - .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) + val redisCacheConfiguration = + RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofDays(7)) + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) + .serializeValuesWith( + RedisSerializationContext.SerializationPair.fromSerializer( + GenericJackson2JsonRedisSerializer(), + ), + ) return RedisCacheManager.RedisCacheManagerBuilder .fromConnectionFactory(redisConnectionFactory) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index 87fccfbd..ea586b4f 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -47,9 +47,10 @@ class RedisConfig { redisConfig.port = redisPort redisConfig.password = RedisPassword.of(redisPassword) - val clientConfig = LettuceClientConfiguration.builder() - .useSsl() // SSL 사용 설정 - .build() + val clientConfig = + LettuceClientConfiguration.builder() + .useSsl() + .build() return LettuceConnectionFactory(redisConfig, clientConfig) } From 03d3c5e147a2c7f496cec2eb21a38599305f14f9 Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 20:47:34 +0900 Subject: [PATCH 06/18] fix: review: compose ConfigurationProperties --- .../com/piikii/output/redis/config/RedisConfig.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index ea586b4f..d9e491f8 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -19,16 +19,14 @@ import org.springframework.data.redis.serializer.StringRedisSerializer @Configuration @EnableRedisRepositories -class RedisConfig { +class RedisConfig( @Value("\${redis.host}") - private val redisHost: String? = null - + private val redisHost: String, @Value("\${redis.port}") - private val redisPort = 0 - + private val redisPort: Int, @Value("\${redis.password}") - private val redisPassword: String? = null - + private val redisPassword: String, +) { @Bean fun objectMapper(): ObjectMapper { return JsonMapper.builder() From 49f0b9d5a84316430d4799e012482d735d0defdc Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 20:49:52 +0900 Subject: [PATCH 07/18] fix: review move to apply scope --- .../com/piikii/output/redis/config/RedisConfig.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index d9e491f8..9ac2f3d1 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -40,10 +40,12 @@ class RedisConfig( @Bean fun lettuceConnectionFactory(): LettuceConnectionFactory { - val redisConfig = RedisStandaloneConfiguration() - redisConfig.hostName = redisHost!! - redisConfig.port = redisPort - redisConfig.password = RedisPassword.of(redisPassword) + val redisConfig = + RedisStandaloneConfiguration().apply { + hostName = redisHost + port = redisPort + password = RedisPassword.of(redisPassword) + } val clientConfig = LettuceClientConfiguration.builder() From dd3e3229144758a0ee094e01d89a948abf43c3cf Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 20:52:34 +0900 Subject: [PATCH 08/18] fix: review: integrate configuration --- .../output/redis/config/RedisCacheConfig.kt | 35 ------------------- .../piikii/output/redis/config/RedisConfig.kt | 26 ++++++++++++++ 2 files changed, 26 insertions(+), 35 deletions(-) delete mode 100644 piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt deleted file mode 100644 index 8b6e63cb..00000000 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisCacheConfig.kt +++ /dev/null @@ -1,35 +0,0 @@ -package com.piikii.output.redis.config - -import org.springframework.cache.CacheManager -import org.springframework.cache.annotation.EnableCaching -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration -import org.springframework.data.redis.cache.RedisCacheConfiguration -import org.springframework.data.redis.cache.RedisCacheManager -import org.springframework.data.redis.connection.RedisConnectionFactory -import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer -import org.springframework.data.redis.serializer.RedisSerializationContext -import org.springframework.data.redis.serializer.StringRedisSerializer -import java.time.Duration - -@Configuration -@EnableCaching -class RedisCacheConfig { - @Bean - fun cacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager { - val redisCacheConfiguration = - RedisCacheConfiguration.defaultCacheConfig() - .entryTtl(Duration.ofDays(7)) - .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) - .serializeValuesWith( - RedisSerializationContext.SerializationPair.fromSerializer( - GenericJackson2JsonRedisSerializer(), - ), - ) - - return RedisCacheManager.RedisCacheManagerBuilder - .fromConnectionFactory(redisConnectionFactory) - .cacheDefaults(redisCacheConfiguration) - .build() - } -} diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index 9ac2f3d1..013cd23a 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -6,8 +6,13 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.json.JsonMapper import org.springframework.beans.factory.annotation.Value +import org.springframework.cache.CacheManager +import org.springframework.cache.annotation.EnableCaching import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.data.redis.cache.RedisCacheConfiguration +import org.springframework.data.redis.cache.RedisCacheManager +import org.springframework.data.redis.connection.RedisConnectionFactory import org.springframework.data.redis.connection.RedisPassword import org.springframework.data.redis.connection.RedisStandaloneConfiguration import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration @@ -15,10 +20,13 @@ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactor import org.springframework.data.redis.core.RedisTemplate import org.springframework.data.redis.repository.configuration.EnableRedisRepositories import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer +import org.springframework.data.redis.serializer.RedisSerializationContext import org.springframework.data.redis.serializer.StringRedisSerializer +import java.time.Duration @Configuration @EnableRedisRepositories +@EnableCaching class RedisConfig( @Value("\${redis.host}") private val redisHost: String, @@ -63,4 +71,22 @@ class RedisConfig( redisTemplate.valueSerializer = GenericJackson2JsonRedisSerializer(objectMapper()) return redisTemplate } + + @Bean + fun cacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager { + val redisCacheConfiguration = + RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofDays(7)) + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) + .serializeValuesWith( + RedisSerializationContext.SerializationPair.fromSerializer( + GenericJackson2JsonRedisSerializer(), + ), + ) + + return RedisCacheManager.RedisCacheManagerBuilder + .fromConnectionFactory(redisConnectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build() + } } From 31cf95e28dc59de3770a77dc0a985d65221d3b7b Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 20:59:34 +0900 Subject: [PATCH 09/18] fix: review: fixed parameter not nullable --- .../com/piikii/application/domain/course/CourseService.kt | 5 ++++- .../piikii/application/port/output/cache/CourseCachePort.kt | 2 +- .../piikii/application/domain/course/CourseServiceTest.kt | 2 -- .../piikii/output/redis/adapter/CourseRedisCacheAdapter.kt | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt index 525dae89..6f2b1ef6 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt @@ -127,7 +127,10 @@ class CourseService( schedule = schedule, place = confirmedPlace, coordinate = coordinate, - distance = courseCachePort.getDistance(preCoursePlace, confirmedPlace), + distance = + preCoursePlace?.let { + courseCachePort.getDistance(it, confirmedPlace) + } ?: Distance.EMPTY, ) } diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt index decf5486..85eced64 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt @@ -6,7 +6,7 @@ import com.piikii.application.domain.place.Place interface CourseCachePort { fun getDistance( - startPlace: CoursePlace?, + startPlace: CoursePlace, endPlace: Place, ): Distance? } diff --git a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt index ea4c0666..b4084539 100644 --- a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt +++ b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt @@ -140,8 +140,6 @@ class CourseServiceTest { distance = Distance.EMPTY, ) - given(courseCachePort.getDistance(null, places[0])) - .willReturn(Distance.EMPTY) given(courseCachePort.getDistance(coursePlace, places[2])) .willReturn(Distance(100, 5)) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt index 24027663..4a9d9f96 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -20,10 +20,10 @@ class CourseRedisCacheAdapter( unless = "#result == null", ) override fun getDistance( - startPlace: CoursePlace?, + startPlace: CoursePlace, endPlace: Place, ): Distance? { - return startPlace?.coordinate?.let { preCoordinate -> + return startPlace.coordinate?.let { preCoordinate -> endPlace.getCoordinate().let { coordinate -> navigationPort.getDistance(start = preCoordinate, end = coordinate) } From 449685f0bc50dd07d9f20dce8cba82932e222220 Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 22:43:10 +0900 Subject: [PATCH 10/18] fix: review: unify class type --- .../application/domain/course/CoursePlace.kt | 39 ++++++------------- .../domain/course/CourseService.kt | 32 ++++++--------- .../port/input/dto/response/CourseResponse.kt | 14 +++---- .../port/output/cache/CourseCachePort.kt | 5 +-- .../domain/course/CourseServiceTest.kt | 10 +---- .../redis/adapter/CourseRedisCacheAdapter.kt | 7 ++-- 6 files changed, 36 insertions(+), 71 deletions(-) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CoursePlace.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CoursePlace.kt index 44ffc4bd..496898bc 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CoursePlace.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CoursePlace.kt @@ -8,32 +8,17 @@ import com.piikii.application.domain.schedule.ScheduleType data class CoursePlace( val scheduleId: LongTypeId, val scheduleType: ScheduleType, - val placeId: LongTypeId, - val name: String, - val url: String?, - val address: String?, - val phoneNumber: String?, - val coordinate: Coordinate?, - val distance: Distance?, + val prePlace: Place?, + val place: Place, + val coordinate: Coordinate, + val distance: Distance, ) { - companion object { - fun from( - schedule: Schedule, - place: Place, - coordinate: Coordinate?, - distance: Distance?, - ): CoursePlace { - return CoursePlace( - scheduleId = schedule.id, - scheduleType = schedule.type, - placeId = place.id, - name = place.name, - url = place.url, - address = place.address, - phoneNumber = place.phoneNumber, - coordinate = coordinate, - distance = distance, - ) - } - } + constructor(schedule: Schedule, prePlace: Place?, place: Place, distance: Distance) : this( + scheduleId = schedule.id, + scheduleType = schedule.type, + prePlace = prePlace, + place = place, + coordinate = place.getCoordinate(), + distance = distance, + ) } diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt index 6f2b1ef6..1eb85560 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt @@ -82,24 +82,15 @@ class CourseService( places: List, agreeCountByPlaceId: Map, ): Map { - // initial 값 설정: null과 빈 Map의 쌍으로 초기화 - val initial: Map = emptyMap() - + val initCourse: Map = emptyMap() return mapPlacesBySchedule(schedules, places) - .entries.fold(initial) { prePlaceBySchedule, (schedule, places) -> - // 현재 CoursePlace 생성 + .entries.fold(initCourse) { course, (schedule, places) -> val confirmedPlace = getConfirmedPlace(schedule, places, agreeCountByPlaceId) - - if (confirmedPlace != null) { - val preCoursePlace = prePlaceBySchedule.values.lastOrNull() - val curCoursePlace = getCoursePlace(schedule, preCoursePlace, confirmedPlace) - - // currentCoursePlace를 누적된 placeBySchedule 맵에 추가 - prePlaceBySchedule + (schedule to curCoursePlace) - } else { - // confirmedPlace가 null인 경우 기존 맵을 그대로 반환 - prePlaceBySchedule - } + confirmedPlace?.let { + val preCoursePlace = course.values.lastOrNull() + val curCoursePlace = getCoursePlace(schedule, preCoursePlace?.place, it) + course + (schedule to curCoursePlace) + } ?: course } } @@ -119,16 +110,15 @@ class CourseService( private fun getCoursePlace( schedule: Schedule, - preCoursePlace: CoursePlace?, + prePlace: Place?, confirmedPlace: Place, ): CoursePlace { - val coordinate = confirmedPlace.getCoordinate() - return CoursePlace.from( + return CoursePlace( schedule = schedule, + prePlace = prePlace, place = confirmedPlace, - coordinate = coordinate, distance = - preCoursePlace?.let { + prePlace?.let { courseCachePort.getDistance(it, confirmedPlace) } ?: Distance.EMPTY, ) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/input/dto/response/CourseResponse.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/input/dto/response/CourseResponse.kt index 9144a963..067f3783 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/input/dto/response/CourseResponse.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/input/dto/response/CourseResponse.kt @@ -51,13 +51,13 @@ data class CoursePlaceResponse( return CoursePlaceResponse( scheduleId = coursePlace.scheduleId.getValue(), scheduleType = coursePlace.scheduleType, - placeId = coursePlace.placeId.getValue(), - name = coursePlace.name, - url = coursePlace.url, - address = coursePlace.address, - phoneNumber = coursePlace.phoneNumber, - distance = coursePlace.distance?.totalDistanceMeter, - time = coursePlace.distance?.totalTimeMinute, + placeId = coursePlace.place.id.getValue(), + name = coursePlace.place.name, + url = coursePlace.place.url, + address = coursePlace.place.address, + phoneNumber = coursePlace.place.phoneNumber, + distance = coursePlace.distance.totalDistanceMeter, + time = coursePlace.distance.totalTimeMinute, ) } } diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt index 85eced64..80fe0acf 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt @@ -1,12 +1,11 @@ package com.piikii.application.port.output.cache -import com.piikii.application.domain.course.CoursePlace import com.piikii.application.domain.course.Distance import com.piikii.application.domain.place.Place interface CourseCachePort { fun getDistance( - startPlace: CoursePlace, + startPlace: Place, endPlace: Place, - ): Distance? + ): Distance } diff --git a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt index b4084539..7aa68594 100644 --- a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt +++ b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt @@ -132,15 +132,7 @@ class CourseServiceTest { ), ) - val coursePlace = - CoursePlace.from( - schedule = schedules[0], - place = places[0], - coordinate = Coordinate(places[0].longitude, places[0].latitude), - distance = Distance.EMPTY, - ) - - given(courseCachePort.getDistance(coursePlace, places[2])) + given(courseCachePort.getDistance(places[0], places[2])) .willReturn(Distance(100, 5)) val updatedPlace = places[2].copy(confirmed = true) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt index 4a9d9f96..646cfa86 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -1,6 +1,5 @@ package com.piikii.output.redis.adapter -import com.piikii.application.domain.course.CoursePlace import com.piikii.application.domain.course.Distance import com.piikii.application.domain.place.Place import com.piikii.application.port.output.cache.CourseCachePort @@ -20,10 +19,10 @@ class CourseRedisCacheAdapter( unless = "#result == null", ) override fun getDistance( - startPlace: CoursePlace, + startPlace: Place, endPlace: Place, - ): Distance? { - return startPlace.coordinate?.let { preCoordinate -> + ): Distance { + return startPlace.getCoordinate().let { preCoordinate -> endPlace.getCoordinate().let { coordinate -> navigationPort.getDistance(start = preCoordinate, end = coordinate) } From e743fd1263e8e1958e574f63790fa7de44da0728 Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 22:49:21 +0900 Subject: [PATCH 11/18] fix: review: enhance readability --- .../piikii/output/redis/adapter/CourseRedisCacheAdapter.kt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt index 646cfa86..bd033f11 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt @@ -22,10 +22,6 @@ class CourseRedisCacheAdapter( startPlace: Place, endPlace: Place, ): Distance { - return startPlace.getCoordinate().let { preCoordinate -> - endPlace.getCoordinate().let { coordinate -> - navigationPort.getDistance(start = preCoordinate, end = coordinate) - } - } + return navigationPort.getDistance(startPlace.getCoordinate(), endPlace.getCoordinate()) } } From f2602e3de8afbdc83b95ede5c3d630baee7cbc94 Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 23:27:50 +0900 Subject: [PATCH 12/18] fix: review: deleted cache adapter --- .../domain/course/CourseService.kt | 6 +-- .../port/output/cache/CourseCachePort.kt | 11 ----- .../port/output/web/NavigationPort.kt | 6 +-- .../domain/course/CourseServiceTest.kt | 6 +-- .../redis/adapter/CourseRedisCacheAdapter.kt | 27 ---------- .../web/tmap/adapter/TmapNavigationAdapter.kt | 24 ++++++--- .../web/tmap/TmapNavigationAdapterTest.kt | 49 +++++++++++++++++-- 7 files changed, 71 insertions(+), 58 deletions(-) delete mode 100644 piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt delete mode 100644 piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt index 1eb85560..eb9c6663 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/CourseService.kt @@ -6,13 +6,13 @@ import com.piikii.application.domain.place.Place import com.piikii.application.domain.schedule.Schedule import com.piikii.application.port.input.CourseUseCase import com.piikii.application.port.input.dto.response.CourseResponse -import com.piikii.application.port.output.cache.CourseCachePort import com.piikii.application.port.output.persistence.CourseQueryPort import com.piikii.application.port.output.persistence.PlaceCommandPort import com.piikii.application.port.output.persistence.PlaceQueryPort import com.piikii.application.port.output.persistence.RoomQueryPort import com.piikii.application.port.output.persistence.ScheduleQueryPort import com.piikii.application.port.output.persistence.VoteQueryPort +import com.piikii.application.port.output.web.NavigationPort import com.piikii.common.exception.ExceptionCode import com.piikii.common.exception.PiikiiException import org.springframework.stereotype.Service @@ -27,7 +27,7 @@ class CourseService( private val placeQueryPort: PlaceQueryPort, private val placeCommandPort: PlaceCommandPort, private val voteQueryPort: VoteQueryPort, - private val courseCachePort: CourseCachePort, + private val navigationPort: NavigationPort, ) : CourseUseCase { override fun isCourseExist(roomUid: UuidTypeId): Boolean { return courseQueryPort.isCourseExist(roomUid) @@ -119,7 +119,7 @@ class CourseService( place = confirmedPlace, distance = prePlace?.let { - courseCachePort.getDistance(it, confirmedPlace) + navigationPort.getDistance(it, confirmedPlace) } ?: Distance.EMPTY, ) } diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt deleted file mode 100644 index 80fe0acf..00000000 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/output/cache/CourseCachePort.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.piikii.application.port.output.cache - -import com.piikii.application.domain.course.Distance -import com.piikii.application.domain.place.Place - -interface CourseCachePort { - fun getDistance( - startPlace: Place, - endPlace: Place, - ): Distance -} diff --git a/piikii-application/src/main/kotlin/com/piikii/application/port/output/web/NavigationPort.kt b/piikii-application/src/main/kotlin/com/piikii/application/port/output/web/NavigationPort.kt index 951815ab..76680d90 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/port/output/web/NavigationPort.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/port/output/web/NavigationPort.kt @@ -1,11 +1,11 @@ package com.piikii.application.port.output.web -import com.piikii.application.domain.course.Coordinate import com.piikii.application.domain.course.Distance +import com.piikii.application.domain.place.Place interface NavigationPort { fun getDistance( - start: Coordinate, - end: Coordinate, + startPlace: Place, + endPlace: Place, ): Distance } diff --git a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt index 7aa68594..3fb2487b 100644 --- a/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt +++ b/piikii-application/src/test/kotlin/com/piikii/application/domain/course/CourseServiceTest.kt @@ -5,13 +5,13 @@ import com.piikii.application.domain.fixture.RoomFixture import com.piikii.application.domain.fixture.ScheduleFixture import com.piikii.application.domain.fixture.VoteFixture import com.piikii.application.domain.vote.VoteResult -import com.piikii.application.port.output.cache.CourseCachePort import com.piikii.application.port.output.persistence.CourseQueryPort import com.piikii.application.port.output.persistence.PlaceCommandPort import com.piikii.application.port.output.persistence.PlaceQueryPort import com.piikii.application.port.output.persistence.RoomQueryPort import com.piikii.application.port.output.persistence.ScheduleQueryPort import com.piikii.application.port.output.persistence.VoteQueryPort +import com.piikii.application.port.output.web.NavigationPort import com.piikii.common.exception.PiikiiException import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -45,7 +45,7 @@ class CourseServiceTest { lateinit var voteQueryPort: VoteQueryPort @Mock - lateinit var courseCachePort: CourseCachePort + lateinit var navigationPort: NavigationPort @Mock lateinit var courseQueryPort: CourseQueryPort @@ -132,7 +132,7 @@ class CourseServiceTest { ), ) - given(courseCachePort.getDistance(places[0], places[2])) + given(navigationPort.getDistance(places[0], places[2])) .willReturn(Distance(100, 5)) val updatedPlace = places[2].copy(confirmed = true) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt deleted file mode 100644 index bd033f11..00000000 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/adapter/CourseRedisCacheAdapter.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.piikii.output.redis.adapter - -import com.piikii.application.domain.course.Distance -import com.piikii.application.domain.place.Place -import com.piikii.application.port.output.cache.CourseCachePort -import com.piikii.application.port.output.web.NavigationPort -import org.springframework.cache.annotation.Cacheable -import org.springframework.stereotype.Component - -@Component -class CourseRedisCacheAdapter( - private val navigationPort: NavigationPort, -) : CourseCachePort { - @Cacheable( - value = ["Distances"], - key = "#startPlace?.placeId + '_' + #endPlace.id", - cacheManager = "cacheManager", - condition = "#startPlace != null", - unless = "#result == null", - ) - override fun getDistance( - startPlace: Place, - endPlace: Place, - ): Distance { - return navigationPort.getDistance(startPlace.getCoordinate(), endPlace.getCoordinate()) - } -} diff --git a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt index 3772ba1b..3953d712 100644 --- a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt +++ b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt @@ -2,9 +2,11 @@ package com.piikii.output.web.tmap.adapter import com.piikii.application.domain.course.Coordinate import com.piikii.application.domain.course.Distance +import com.piikii.application.domain.place.Place import com.piikii.application.port.output.web.NavigationPort import com.piikii.common.exception.ExceptionCode import com.piikii.common.exception.PiikiiException +import org.springframework.cache.annotation.Cacheable import org.springframework.stereotype.Component import org.springframework.web.client.RestClient import org.springframework.web.client.body @@ -13,15 +15,23 @@ import org.springframework.web.client.body class TmapNavigationAdapter( private val tmapApiClient: RestClient, ) : NavigationPort { + @Cacheable( + value = ["Distances"], + key = "#startPlace.id + '_' + #endPlace.id", + cacheManager = "cacheManager", + condition = "#startPlace != null", + unless = "#result == null", + ) override fun getDistance( - start: Coordinate, - end: Coordinate, + startPlace: Place, + endPlace: Place, ): Distance { - return if (start.isValid() && end.isValid()) { - getDistanceFromTmap(start, end) - } else { - Distance.EMPTY - } + val startCoordinate = startPlace.getCoordinate() + val endCoordinate = endPlace.getCoordinate() + return startCoordinate.takeIf { it.isValid() } + ?.takeIf { endCoordinate.isValid() } + ?.let { getDistanceFromTmap(startCoordinate, endCoordinate) } + ?: Distance.EMPTY } private fun getDistanceFromTmap( diff --git a/piikii-output-web/tmap/src/test/kotlin/com/piikii/output/web/tmap/TmapNavigationAdapterTest.kt b/piikii-output-web/tmap/src/test/kotlin/com/piikii/output/web/tmap/TmapNavigationAdapterTest.kt index e2f8bb71..930f475b 100644 --- a/piikii-output-web/tmap/src/test/kotlin/com/piikii/output/web/tmap/TmapNavigationAdapterTest.kt +++ b/piikii-output-web/tmap/src/test/kotlin/com/piikii/output/web/tmap/TmapNavigationAdapterTest.kt @@ -1,6 +1,10 @@ package com.piikii.output.web.tmap -import com.piikii.application.domain.course.Coordinate +import com.piikii.application.domain.generic.LongTypeId +import com.piikii.application.domain.generic.ThumbnailLinks +import com.piikii.application.domain.generic.UuidTypeId +import com.piikii.application.domain.place.Origin +import com.piikii.application.domain.place.Place import com.piikii.output.web.tmap.adapter.TmapNavigationAdapter import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test @@ -8,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles import org.springframework.test.context.ContextConfiguration +import java.util.UUID @Disabled @SpringBootTest @@ -19,10 +24,46 @@ class TmapNavigationAdapterTest { @Test fun getDistanceTest() { - val start = Coordinate(x = 126.9246033, y = 33.45241976) - val end = Coordinate(x = 126.9041895, y = 33.4048969) + val startPlace = + Place( + id = LongTypeId(1L), + roomUid = UuidTypeId(UUID.randomUUID()), + scheduleId = LongTypeId(0L), + name = "", + url = null, + thumbnailLinks = ThumbnailLinks(listOf()), + address = null, + phoneNumber = null, + starGrade = null, + origin = Origin.MANUAL, + memo = null, + confirmed = true, + reviewCount = null, + longitude = 126.9246033, + latitude = 33.45241976, + openingHours = null, + ) + val endPlace = + Place( + id = LongTypeId(1L), + roomUid = UuidTypeId(UUID.randomUUID()), + scheduleId = LongTypeId(0L), + name = "", + url = null, + thumbnailLinks = ThumbnailLinks(listOf()), + address = null, + phoneNumber = null, + starGrade = null, + origin = Origin.MANUAL, + memo = null, + confirmed = true, + reviewCount = null, + longitude = 126.9041895, + latitude = 33.4048969, + openingHours = null, + ) - val distance = tmapNavigationClient.getDistance(start, end) + val distance = tmapNavigationClient.getDistance(startPlace, endPlace) println("distance = $distance") } } From 19254409641a55b04bcf3722ff77ae136a4b9139 Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 23:34:10 +0900 Subject: [PATCH 13/18] fix: review: deleted cacheManager --- .../piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt index 3953d712..b3bc4c6b 100644 --- a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt +++ b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt @@ -16,11 +16,9 @@ class TmapNavigationAdapter( private val tmapApiClient: RestClient, ) : NavigationPort { @Cacheable( - value = ["Distances"], + value = ["Distance"], key = "#startPlace.id + '_' + #endPlace.id", - cacheManager = "cacheManager", - condition = "#startPlace != null", - unless = "#result == null", + unless = "#result == T(com.piikii.application.domain.course.Distance).EMPTY", ) override fun getDistance( startPlace: Place, From 8f035ab622984ac752c034cfe4e791069965d96f Mon Sep 17 00:00:00 2001 From: thguss Date: Sat, 24 Aug 2024 23:44:03 +0900 Subject: [PATCH 14/18] add: added @JvmField --- .../main/kotlin/com/piikii/application/domain/course/Distance.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt index 28ea6136..8283a884 100644 --- a/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt +++ b/piikii-application/src/main/kotlin/com/piikii/application/domain/course/Distance.kt @@ -5,6 +5,7 @@ data class Distance( val totalTimeMinute: Int? = null, ) { companion object { + @JvmField val EMPTY = Distance(null, null) } } From e4f497acf22cd686d956051bde2679f5d380ee2b Mon Sep 17 00:00:00 2001 From: thguss Date: Wed, 28 Aug 2024 01:03:34 +0900 Subject: [PATCH 15/18] fix: updated @Value to Properties --- .../piikii/output/redis/config/RedisConfig.kt | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index 013cd23a..fa92f83b 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -5,7 +5,8 @@ import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.json.JsonMapper -import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.cache.CacheManager import org.springframework.cache.annotation.EnableCaching import org.springframework.context.annotation.Bean @@ -27,14 +28,8 @@ import java.time.Duration @Configuration @EnableRedisRepositories @EnableCaching -class RedisConfig( - @Value("\${redis.host}") - private val redisHost: String, - @Value("\${redis.port}") - private val redisPort: Int, - @Value("\${redis.password}") - private val redisPassword: String, -) { +@EnableConfigurationProperties(RedisProperties::class) +class RedisConfig { @Bean fun objectMapper(): ObjectMapper { return JsonMapper.builder() @@ -47,12 +42,12 @@ class RedisConfig( } @Bean - fun lettuceConnectionFactory(): LettuceConnectionFactory { + fun lettuceConnectionFactory(redisProperties: RedisProperties): LettuceConnectionFactory { val redisConfig = RedisStandaloneConfiguration().apply { - hostName = redisHost - port = redisPort - password = RedisPassword.of(redisPassword) + hostName = redisProperties.host + port = redisProperties.port + password = RedisPassword.of(redisProperties.password) } val clientConfig = @@ -64,9 +59,9 @@ class RedisConfig( } @Bean - fun redisTemplate(): RedisTemplate { + fun redisTemplate(redisProperties: RedisProperties): RedisTemplate { val redisTemplate = RedisTemplate() - redisTemplate.connectionFactory = lettuceConnectionFactory() + redisTemplate.connectionFactory = lettuceConnectionFactory(redisProperties) redisTemplate.keySerializer = StringRedisSerializer() redisTemplate.valueSerializer = GenericJackson2JsonRedisSerializer(objectMapper()) return redisTemplate @@ -90,3 +85,10 @@ class RedisConfig( .build() } } + +@ConfigurationProperties(prefix = "redis") +data class RedisProperties( + val host: String, + val port: Int, + val password: String, +) From 5bfff5f23302efd960d68eda5f5997b29fbbf806 Mon Sep 17 00:00:00 2001 From: thguss Date: Wed, 28 Aug 2024 01:07:56 +0900 Subject: [PATCH 16/18] fix: updated custom objectMapper to default --- .../piikii/output/redis/config/RedisConfig.kt | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index fa92f83b..d048f427 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -1,10 +1,6 @@ package com.piikii.output.redis.config -import com.fasterxml.jackson.databind.DeserializationFeature -import com.fasterxml.jackson.databind.MapperFeature import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.databind.SerializationFeature -import com.fasterxml.jackson.databind.json.JsonMapper import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.cache.CacheManager @@ -30,16 +26,6 @@ import java.time.Duration @EnableCaching @EnableConfigurationProperties(RedisProperties::class) class RedisConfig { - @Bean - fun objectMapper(): ObjectMapper { - return JsonMapper.builder() - .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true) - .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false) - .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) - .configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true) - .findAndAddModules() - .build() - } @Bean fun lettuceConnectionFactory(redisProperties: RedisProperties): LettuceConnectionFactory { @@ -59,11 +45,11 @@ class RedisConfig { } @Bean - fun redisTemplate(redisProperties: RedisProperties): RedisTemplate { + fun redisTemplate(redisProperties: RedisProperties, objectMapper: ObjectMapper): RedisTemplate { val redisTemplate = RedisTemplate() redisTemplate.connectionFactory = lettuceConnectionFactory(redisProperties) redisTemplate.keySerializer = StringRedisSerializer() - redisTemplate.valueSerializer = GenericJackson2JsonRedisSerializer(objectMapper()) + redisTemplate.valueSerializer = GenericJackson2JsonRedisSerializer(objectMapper) return redisTemplate } From f4a2bec2eb3fe731ddf2a5ecafc927f1e8647f10 Mon Sep 17 00:00:00 2001 From: thguss Date: Wed, 28 Aug 2024 01:13:42 +0900 Subject: [PATCH 17/18] fix: updated type Any to Generic --- .../com/piikii/output/redis/config/RedisConfig.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt index d048f427..4e21f4e3 100644 --- a/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt +++ b/piikii-output-cache/redis/src/main/kotlin/com/piikii/output/redis/config/RedisConfig.kt @@ -26,7 +26,6 @@ import java.time.Duration @EnableCaching @EnableConfigurationProperties(RedisProperties::class) class RedisConfig { - @Bean fun lettuceConnectionFactory(redisProperties: RedisProperties): LettuceConnectionFactory { val redisConfig = @@ -45,9 +44,12 @@ class RedisConfig { } @Bean - fun redisTemplate(redisProperties: RedisProperties, objectMapper: ObjectMapper): RedisTemplate { - val redisTemplate = RedisTemplate() - redisTemplate.connectionFactory = lettuceConnectionFactory(redisProperties) + fun redisTemplate( + redisConnectionFactory: RedisConnectionFactory, + objectMapper: ObjectMapper, + ): RedisTemplate { + val redisTemplate = RedisTemplate() + redisTemplate.connectionFactory = redisConnectionFactory redisTemplate.keySerializer = StringRedisSerializer() redisTemplate.valueSerializer = GenericJackson2JsonRedisSerializer(objectMapper) return redisTemplate From d43d17fed556ef7d6be97b33e7fe5d5217507b91 Mon Sep 17 00:00:00 2001 From: thguss Date: Fri, 30 Aug 2024 01:52:50 +0900 Subject: [PATCH 18/18] fix: updated code to readable (review) --- .../output/web/tmap/adapter/TmapNavigationAdapter.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt index b3bc4c6b..01ec2324 100644 --- a/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt +++ b/piikii-output-web/tmap/src/main/kotlin/com/piikii/output/web/tmap/adapter/TmapNavigationAdapter.kt @@ -26,10 +26,11 @@ class TmapNavigationAdapter( ): Distance { val startCoordinate = startPlace.getCoordinate() val endCoordinate = endPlace.getCoordinate() - return startCoordinate.takeIf { it.isValid() } - ?.takeIf { endCoordinate.isValid() } - ?.let { getDistanceFromTmap(startCoordinate, endCoordinate) } - ?: Distance.EMPTY + return if (startCoordinate.isValid() && endCoordinate.isValid()) { + getDistanceFromTmap(startCoordinate, endCoordinate) + } else { + Distance.EMPTY + } } private fun getDistanceFromTmap(