일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- devops
- jenkins
- java
- tool
- db
- ReactJS
- 요리
- Gradle
- redis
- Spring Batch
- ubuntu
- AWS
- Spring
- linux
- Design Patterns
- Web Server
- IntelliJ
- MySQL
- laravel
- javascript
- springboot
- elasticsearch
- jsp
- 맛집
- Git
- php
- JVM
- Oracle
- it
- Spring Boot
Archives
- Today
- Total
아무거나
Kotlin + Spring Boot 에 Ehcache 적용 본문
반응형
EhCache 적용
- 의존성 추가(build.gradle.kts)
...
dependencies {
...
// Cache
implementation("org.springframework.boot:spring-boot-starter-cache")
implementation("org.ehcache:ehcache:3.9.5")
...
- Application 구동 클래스에 @EnableCaching 추가
@EnableCaching // 추가
@SpringBootApplication
class KotlinJpaCodebaseApplication
fun main(args: Array<String>) {
runApplication<KotlinJpaCodebaseApplication>(*args)
}
- Config 관련 클래스 추가
[EhCacheConfig.kt]
@Configuration
class EhCacheConfig {
...
}
[EhCacheLogger.kt]
import com.bkjeon.base.core.log.logger
import org.ehcache.event.CacheEvent
import org.ehcache.event.CacheEventListener
class EhCacheLogger: CacheEventListener<Any, Any> {
private val log = logger()
override fun onEvent(cacheEvent: CacheEvent<out Any, out Any>) {
log.info("Key: [${cacheEvent.key}] | EventType: [${cacheEvent.type}] | Old value: [${cacheEvent.oldValue}] | New value: [${cacheEvent.newValue}]")
}
}
- Cache 적용
[resources/ehcache.xml]
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>
<cache-template name="default">
<listeners>
<listener>
<class>com.bkjeon.base.core.config.cache.EhCacheLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>UPDATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
<events-to-fire-on>REMOVED</events-to-fire-on>
<events-to-fire-on>EVICTED</events-to-fire-on>
</listener>
</listeners>
</cache-template>
<cache alias="getCacheSampleList" uses-template="default">
<key-type>java.lang.String</key-type>
<value-type>java.util.ArrayList</value-type>
<expiry>
<ttl unit="minutes">60</ttl>
</expiry>
<resources>
<heap>100</heap>
<offheap unit="MB">1</offheap>
</resources>
</cache>
</config>
[CacheSampleService.kt]
@Cacheable(cacheNames = ["getCacheSampleList"])
fun getCacheSampleList(@ModelAttribute request: CacheSampleRequest): String {
log.info("EhCache Key List: ${cacheManager.cacheNames}")
val sampleDataList: MutableList<SampleData> = ArrayList()
for (i: Int in 1..10000000) {
sampleDataList.add(SampleData("bkjeon", i, 20_000))
}
for (i: Int in 1..10000000) {
sampleDataList.add(SampleData("bkjeon2", i, 20_000))
}
return "OK"
}
- Event 로깅 추가
[application.yml]
...
logging:
level:
org.springframework.cache: TRACE
반응형
'Java & Kotlin > Kotlin' 카테고리의 다른 글
스케쥴러 구현 (@EnableScheduling) (0) | 2024.11.12 |
---|---|
객체 간 매핑을 위한 Kotlin MapStruct 적용 (1) | 2024.06.28 |
Kotlin + Spring Boot DynamoDB SDK 적용 (0) | 2024.05.30 |
Comments