Skip to content

Commit

Permalink
refactor: refactor caching logic to use cacheGateway object handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji-soo708 committed Mar 2, 2025
1 parent b718cc7 commit b032804
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 0 additions & 1 deletion application/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions domain/src/main/kotlin/com/dobby/gateway/CacheGateway.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.dobby.gateway

interface CacheGateway {
fun <T> getObject(key: String, clazz: Class<T>): 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <T> getObject(key: String, clazz: Class<T>): 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)
}
Expand All @@ -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)
}
}
Expand Down

0 comments on commit b032804

Please sign in to comment.