그냥 메모용으로..
(너무나 흔해서 설명 필요 없음. 구현방식만 내맘대로)
캐시로 사용될 키-TTL 에 대한 Enum 하나. ( 직접 처리해도 됨 )
enum class RedisCacheKeyProperties(val description: String, val ttl: Long, val key: Boolean, val placeholder: String, val dpOrderNumber: Int) {
cached_default(
"시스템 유지 관리 캐시",
60 * 60 * 24L,
true,
"KEY",
0
), //second s * m * h
cached_company_id(
"회사 정보 캐시",
60 * 60 * 24L,
true,
"KEY",
0
), //second s * m * h
}
Lettuce 설정
@Configuration(proxyBeanMethods = false)
class LettuceRedisCacheConfiguration {
@Bean
fun redisCacheManagerCustomizer() = RedisCacheManagerBuilderCustomizer { builder ->
builder.withInitialCacheConfigurations(getCacheKeyValuesMap())
}
@Bean
fun cacheConfiguration(): RedisCacheConfiguration? {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(60))
.disableCachingNullValues()
.serializeValuesWith(SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer()))
}
fun getCacheKeyValuesMap() = RedisCacheKeyProperties.values()
.associate { it.name to RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(it.ttl)) }
}
YML
spring:
redis:
host: 127.0.0.1
port: 6379
lettuce:
pool:
max-active: 15
max-idle: 8
min-idle: 3
max-wait: -1
shutdown-timeout: 2000
timeout: 3000
cache:
type: redis
build.gradle.kt
// default Lettuce
..
implementation("org.apache.commons:commons-pool2")
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
..
대충 코드
@Service
class CompanyFinder (val grissomApiClient : GrissomApiClient){
@Cacheable(key = "#companyId", value = ["cached_company_id"])
fun findByCompanyId(companyId : Long) : CompanySimpleInfo {
return grissomApiClient.getCompany(companyId)
}
}
https://github.com/KimHyeongi/KotlinSpringboot-Tips