-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- cache를 사용하기 위해서 config설정을 하였습니다.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/com/insert/ioj/global/config/CacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.insert.ioj.global.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import org.springframework.cache.CacheManager; | ||
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.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import java.time.Duration; | ||
|
||
@Configuration | ||
@EnableCaching | ||
public class CacheConfig { | ||
@Bean | ||
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.registerModule(new JavaTimeModule()); | ||
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
|
||
GenericJackson2JsonRedisSerializer redisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper); | ||
|
||
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.entryTtl(Duration.ofHours(3)) | ||
.disableCachingNullValues() | ||
.serializeKeysWith( | ||
RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) | ||
) | ||
.serializeValuesWith( | ||
RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer) | ||
); | ||
|
||
return RedisCacheManager.builder(redisConnectionFactory) | ||
.cacheDefaults(cacheConfiguration) | ||
.build(); | ||
} | ||
} |