인기 콘텐츠, 큐레이션 조회로직에 Cache 적용
This commit is contained in:
		| @@ -1,14 +1,22 @@ | ||||
| package kr.co.vividnext.sodalive.configs | ||||
|  | ||||
| import org.springframework.beans.factory.annotation.Value | ||||
| import org.springframework.cache.annotation.EnableCaching | ||||
| import org.springframework.context.annotation.Bean | ||||
| import org.springframework.context.annotation.Configuration | ||||
| import org.springframework.data.redis.cache.RedisCacheConfiguration | ||||
| import org.springframework.data.redis.cache.RedisCacheManager | ||||
| import org.springframework.data.redis.connection.RedisConnectionFactory | ||||
| import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||||
| import org.springframework.data.redis.core.RedisTemplate | ||||
| import org.springframework.data.redis.repository.configuration.EnableRedisRepositories | ||||
| import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer | ||||
| import org.springframework.data.redis.serializer.RedisSerializationContext | ||||
| import org.springframework.data.redis.serializer.StringRedisSerializer | ||||
| import java.time.Duration | ||||
|  | ||||
| @Configuration | ||||
| @EnableCaching | ||||
| @EnableRedisRepositories | ||||
| class RedisConfig( | ||||
|     @Value("\${spring.redis.host}") | ||||
| @@ -27,4 +35,31 @@ class RedisConfig( | ||||
|         redisTemplate.setConnectionFactory(redisConnectionFactory()) | ||||
|         return redisTemplate | ||||
|     } | ||||
|  | ||||
|     @Bean | ||||
|     fun cacheManager(connectionFactory: RedisConnectionFactory): RedisCacheManager { | ||||
|         val defaultConfig = RedisCacheConfiguration.defaultCacheConfig() | ||||
|             .entryTtl(Duration.ofMinutes(10)) // Default TTL | ||||
|             .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) | ||||
|             .serializeValuesWith( | ||||
|                 RedisSerializationContext.SerializationPair.fromSerializer( | ||||
|                     GenericJackson2JsonRedisSerializer() | ||||
|                 ) | ||||
|             ) | ||||
|  | ||||
|         val weekLivedCacheConfig = RedisCacheConfiguration.defaultCacheConfig() | ||||
|             .entryTtl(Duration.ofDays(7)) | ||||
|             .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer())) | ||||
|             .serializeValuesWith( | ||||
|                 RedisSerializationContext.SerializationPair.fromSerializer( | ||||
|                     GenericJackson2JsonRedisSerializer() | ||||
|                 ) | ||||
|             ) | ||||
|  | ||||
|         return RedisCacheManager.RedisCacheManagerBuilder | ||||
|             .fromConnectionFactory(connectionFactory) | ||||
|             .cacheDefaults(defaultConfig) | ||||
|             .withCacheConfiguration("weekLivedCache", weekLivedCacheConfig) | ||||
|             .build() | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -18,6 +18,7 @@ import kr.co.vividnext.sodalive.content.order.QOrder.order | ||||
| import kr.co.vividnext.sodalive.content.theme.QAudioContentTheme.audioContentTheme | ||||
| import kr.co.vividnext.sodalive.event.QEvent.event | ||||
| import kr.co.vividnext.sodalive.member.QMember.member | ||||
| import org.springframework.cache.annotation.Cacheable | ||||
| import org.springframework.data.jpa.repository.JpaRepository | ||||
| import org.springframework.stereotype.Repository | ||||
| import java.time.LocalDateTime | ||||
| @@ -387,6 +388,11 @@ class AudioContentQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) | ||||
|             .fetch() | ||||
|     } | ||||
|  | ||||
|     @Cacheable( | ||||
|         value = ["getAudioContentCurations"], | ||||
|         key = "#isAdult", | ||||
|         cacheManager = "cacheManager" | ||||
|     ) | ||||
|     override fun getAudioContentCurations(isAdult: Boolean): List<AudioContentCuration> { | ||||
|         var where = audioContentCuration.isActive.isTrue | ||||
|  | ||||
| @@ -438,6 +444,11 @@ class AudioContentQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) | ||||
|             .fetch() | ||||
|     } | ||||
|  | ||||
|     @Cacheable( | ||||
|         value = ["weekLivedCache"], | ||||
|         key = "#startDate" + '_' + "#isAdult" + '_' + "#offset" + '_' + "#limit", | ||||
|         cacheManager = "cacheManager" | ||||
|     ) | ||||
|     override fun getAudioContentRanking( | ||||
|         cloudfrontHost: String, | ||||
|         isAdult: Boolean, | ||||
|   | ||||
| @@ -73,6 +73,8 @@ spring: | ||||
|         multipart: | ||||
|             max-file-size: 1024MB | ||||
|             max-request-size: 1024MB | ||||
|     cache: | ||||
|         type: redis | ||||
| --- | ||||
| spring: | ||||
|     config: | ||||
|   | ||||
| @@ -47,3 +47,5 @@ spring: | ||||
|             hibernate: | ||||
|                 show_sql: true | ||||
|                 format_sql: true | ||||
|     cache: | ||||
|         type: redis | ||||
|   | ||||
		Reference in New Issue
	
	Block a user