Skip to content

Commit

Permalink
[Feat/#224] 로컬 캐시(EHCache) 도입 및 게시글 단건 데이터 캐시 적용 (#226)
Browse files Browse the repository at this point in the history
* feat: 로컬 캐시(EHCache) 디펜던시 추가 In api-repo 모듈

* feat: EHCache 설정 적용 및 신규 캐시 키(selectArticleRecordCache) 추가

* refactor: ehcache 설정 xml -> javaconfig 변경

* feat: SelectArticleRecord에 Serializable 추가

* feat: LocalCacheConfig의 상수 활용하는 방법으로 수정

* refactor: EHCache에서 event.newValue, oldValue 로깅하지 않도록 변경

* refactor: localCacheProvider 선언

* refactor: EhcacheCachingProvider로 수정

---------

Co-authored-by: belljun3395 <[email protected]>
  • Loading branch information
hun-ca and belljun3395 authored Jul 21, 2024
1 parent 85136c9 commit 656db6d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api-repo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ dependencies {
/** test container */
implementation(platform("org.testcontainers:testcontainers-bom:${DependencyVersion.TEST_CONTAINER}"))
testImplementation("org.testcontainers:mysql")

/** Local Cache **/
implementation("org.ehcache:ehcache:${DependencyVersion.EHCACHE}")
implementation("org.springframework.boot:spring-boot-starter-cache")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.few.api.repo.common

import io.github.oshai.kotlinlogging.KotlinLogging
import org.ehcache.event.CacheEvent
import org.ehcache.event.CacheEventListener

class LocalCacheEventLogger : CacheEventListener<Any, Any> {
private val log = KotlinLogging.logger {}

override fun onEvent(event: CacheEvent<out Any, out Any>) {
log.debug { "Local Cache event: ${event.type} for item with key: ${event.key}. " }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.few.api.repo.config

import com.few.api.repo.common.LocalCacheEventLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import org.ehcache.config.builders.CacheConfigurationBuilder
import org.ehcache.config.builders.ResourcePoolsBuilder
import org.ehcache.config.units.EntryUnit
import org.ehcache.event.EventType
import org.ehcache.impl.config.event.DefaultCacheEventListenerConfiguration
import org.ehcache.jsr107.Eh107Configuration
import org.ehcache.jsr107.EhcacheCachingProvider
import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.jcache.JCacheCacheManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableCaching
class LocalCacheConfig {
private val log = KotlinLogging.logger {}

companion object {
const val LOCAL_CM = "localCacheManager"
const val SELECT_ARTICLE_RECORD_CACHE = "selectArticleRecordCache"
}

@Bean(LOCAL_CM)
fun localCacheManager(): CacheManager {
val cacheEventListenerConfigurationConfig = DefaultCacheEventListenerConfiguration(
setOf(
EventType.CREATED,
EventType.EXPIRED,
EventType.REMOVED,
EventType.UPDATED
),
LocalCacheEventLogger::class.java
)
val cacheManager = EhcacheCachingProvider().cacheManager

val cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(
Any::class.java,
Any::class.java,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(50, EntryUnit.ENTRIES)
)
.withService(cacheEventListenerConfigurationConfig)
.build()

val selectArticleRecordCacheConfig: javax.cache.configuration.Configuration<Any, Any> =
Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfigurationBuilder)
runCatching {
cacheManager.createCache(SELECT_ARTICLE_RECORD_CACHE, selectArticleRecordCacheConfig)
}.onFailure {
log.error(it) { "Failed to create cache" }
}

return JCacheCacheManager(cacheManager)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.few.api.repo.dao.article

import com.few.api.repo.config.LocalCacheConfig.Companion.LOCAL_CM
import com.few.api.repo.config.LocalCacheConfig.Companion.SELECT_ARTICLE_RECORD_CACHE
import com.few.api.repo.dao.article.command.InsertFullArticleRecordCommand
import com.few.api.repo.dao.article.query.SelectArticleRecordQuery
import com.few.api.repo.dao.article.query.SelectWorkBookArticleRecordQuery
Expand All @@ -11,12 +13,15 @@ import jooq.jooq_dsl.tables.ArticleIfo
import jooq.jooq_dsl.tables.ArticleMst
import jooq.jooq_dsl.tables.MappingWorkbookArticle
import org.jooq.DSLContext
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Repository

@Repository
class ArticleDao(
private val dslContext: DSLContext,
) {

@Cacheable(key = "#query.articleId", cacheManager = LOCAL_CM, cacheNames = [SELECT_ARTICLE_RECORD_CACHE])
fun selectArticleRecord(query: SelectArticleRecordQuery): SelectArticleRecord? {
val articleId = query.articleId

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.few.api.repo.dao.article.record

import java.io.Serializable
import java.net.URL
import java.time.LocalDateTime

Expand All @@ -11,4 +12,4 @@ data class SelectArticleRecord(
val category: Byte,
val content: String,
val createdAt: LocalDateTime,
)
) : Serializable
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/DependencyVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ object DependencyVersion {

/** Logger **/
const val KOTLIN_LOGGING = "7.0.0"

/** Local Cache **/
const val EHCACHE = "3.10.0"
}

0 comments on commit 656db6d

Please sign in to comment.