From b032804ecfcee9cb5ec371f50e39f12810f2dcde Mon Sep 17 00:00:00 2001 From: jisu Date: Sun, 2 Mar 2025 23:34:45 +0900 Subject: [PATCH] refactor: refactor caching logic to use cacheGateway object handling --- application/build.gradle.kts | 1 - .../com/dobby/service/ExperimentPostService.kt | 8 ++------ .../kotlin/com/dobby/gateway/CacheGateway.kt | 2 ++ .../gateway/cache/RedisCacheGatewayImpl.kt | 16 +++++++++++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/application/build.gradle.kts b/application/build.gradle.kts index c81d3c1a..11f29d7d 100644 --- a/application/build.gradle.kts +++ b/application/build.gradle.kts @@ -21,7 +21,6 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-stdlib") implementation("org.springframework.boot:spring-boot-starter-data-jpa") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("io.mockk:mockk:1.13.10") diff --git a/application/src/main/kotlin/com/dobby/service/ExperimentPostService.kt b/application/src/main/kotlin/com/dobby/service/ExperimentPostService.kt index 767aefdc..067bfb59 100644 --- a/application/src/main/kotlin/com/dobby/service/ExperimentPostService.kt +++ b/application/src/main/kotlin/com/dobby/service/ExperimentPostService.kt @@ -7,7 +7,6 @@ import com.dobby.exception.InvalidRequestValueException import com.dobby.gateway.CacheGateway import com.dobby.enums.areaInfo.Area import com.dobby.enums.experiment.RecruitStatus -import com.fasterxml.jackson.databind.ObjectMapper import jakarta.transaction.Transactional import org.springframework.stereotype.Service @@ -29,7 +28,6 @@ class ExperimentPostService( private val getMyExperimentPostsUseCase: GetMyExperimentPostsUseCase, private val getMyExperimentPostTotalCountUseCase: GetMyExperimentPostTotalCountUseCase, private val cacheGateway: CacheGateway, - private val objectMapper: ObjectMapper, ) { @Transactional fun createNewExperimentPost(input: CreateExperimentPostUseCase.Input): CreateExperimentPostUseCase.Output { @@ -94,14 +92,12 @@ class ExperimentPostService( private fun getCachedExperimentPostCounts(recruitStatus: RecruitStatus): GetExperimentPostCountsByRegionUseCase.Output? { val cacheKey = "experimentPostCounts:$recruitStatus" - return cacheGateway.get(cacheKey)?.let { - objectMapper.readValue(it, GetExperimentPostCountsByRegionUseCase.Output::class.java) - } + return cacheGateway.getObject(cacheKey, GetExperimentPostCountsByRegionUseCase.Output::class.java) } private fun cacheExperimentPostCounts(recruitStatus: RecruitStatus, output: Any) { val cacheKey = "experimentPostCounts:$recruitStatus" - cacheGateway.set(cacheKey, objectMapper.writeValueAsString(output)) + cacheGateway.setObject(cacheKey, output) } private fun validateFilter(input: GetExperimentPostsUseCase.Input) { diff --git a/domain/src/main/kotlin/com/dobby/gateway/CacheGateway.kt b/domain/src/main/kotlin/com/dobby/gateway/CacheGateway.kt index 5809323b..fd09c58d 100644 --- a/domain/src/main/kotlin/com/dobby/gateway/CacheGateway.kt +++ b/domain/src/main/kotlin/com/dobby/gateway/CacheGateway.kt @@ -1,7 +1,9 @@ package com.dobby.gateway interface CacheGateway { + fun getObject(key: String, clazz: Class): T? fun get(key: String): String? + fun setObject(key: String, value: Any) fun set(key: String, value: String) fun setCode(key: String, value: String) fun incrementRequestCount(key: String) diff --git a/src/main/kotlin/com/dobby/backend/infrastructure/gateway/cache/RedisCacheGatewayImpl.kt b/src/main/kotlin/com/dobby/backend/infrastructure/gateway/cache/RedisCacheGatewayImpl.kt index d87c2f4f..857fb2d0 100644 --- a/src/main/kotlin/com/dobby/backend/infrastructure/gateway/cache/RedisCacheGatewayImpl.kt +++ b/src/main/kotlin/com/dobby/backend/infrastructure/gateway/cache/RedisCacheGatewayImpl.kt @@ -1,6 +1,7 @@ package com.dobby.backend.infrastructure.gateway.cache import com.dobby.gateway.CacheGateway +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.springframework.core.env.Environment import org.springframework.data.redis.core.RedisTemplate import org.springframework.stereotype.Component @@ -12,14 +13,27 @@ class RedisCacheGatewayImpl( private val environment: Environment ) : CacheGateway { + private val objectMapper = jacksonObjectMapper() + private val cacheTimeout = 240L private val codeTimeout = 10L private val requestTimeout = 24L + override fun getObject(key: String, clazz: Class): T? { + return get(key)?.let { json -> + objectMapper.readValue(json, clazz) + } + } + override fun get(key: String): String? { return redisTemplate.opsForValue().get(getCacheKey(key)) } + override fun setObject(key: String, value: Any) { + val jsonValue = objectMapper.writeValueAsString(value) + set(key, jsonValue) + } + override fun set(key: String, value: String) { redisTemplate.opsForValue().set(getCacheKey(key), value, cacheTimeout, TimeUnit.MINUTES) } @@ -36,7 +50,7 @@ class RedisCacheGatewayImpl( val cacheKey = getCacheKey(key) val count = redisTemplate.opsForValue().increment(cacheKey, 1) ?: 1 - if(count == 1L){ + if (count == 1L) { redisTemplate.expire(cacheKey, requestTimeout, TimeUnit.HOURS) } }