|
12 | 12 | import org.springframework.data.redis.cache.RedisCacheManager; |
13 | 13 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
14 | 14 | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
| 15 | +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
15 | 16 | import org.springframework.data.redis.serializer.RedisSerializationContext; |
| 17 | +import org.springframework.data.redis.serializer.RedisSerializer; |
16 | 18 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
17 | 19 |
|
18 | | -import com.fasterxml.jackson.annotation.JsonTypeInfo; |
19 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
20 | | -import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; |
21 | 21 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 22 | +import com.fund.stockProject.stock.dto.response.StockInfoResponse; |
22 | 23 |
|
23 | 24 | @Configuration |
24 | 25 | @EnableCaching |
@@ -52,25 +53,35 @@ public RedisCacheConfiguration defaultCacheConfiguration(ObjectMapper redisCache |
52 | 53 | @Bean |
53 | 54 | public CacheManager cacheManager( |
54 | 55 | RedisConnectionFactory connectionFactory, |
55 | | - RedisCacheConfiguration defaultCacheConfiguration) { |
| 56 | + RedisCacheConfiguration defaultCacheConfiguration, |
| 57 | + ObjectMapper redisCacheObjectMapper) { |
56 | 58 |
|
57 | 59 | Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>(); |
58 | 60 |
|
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + RedisSerializer<Object> stockInfoSerializer = |
| 63 | + (RedisSerializer<Object>) new Jackson2JsonRedisSerializer<>(StockInfoResponse.class); |
| 64 | + ((Jackson2JsonRedisSerializer<?>) stockInfoSerializer).setObjectMapper(redisCacheObjectMapper); |
| 65 | + RedisSerializationContext.SerializationPair<Object> stockInfoSerializationPair = |
| 66 | + RedisSerializationContext.SerializationPair.fromSerializer(stockInfoSerializer); |
| 67 | + |
59 | 68 | // 숏뷰 추천: 5분 캐시 (사용자별 추천 결과) |
60 | 69 | cacheConfigurations.put("shortview", |
61 | 70 | defaultCacheConfiguration.entryTtl(Duration.ofMinutes(5))); |
62 | 71 |
|
63 | 72 | // 실시간 가격: 30초 캐시 (변동성 높음) |
64 | 73 | cacheConfigurations.put("stockPrice", |
65 | | - defaultCacheConfiguration.entryTtl(Duration.ofSeconds(30))); |
| 74 | + defaultCacheConfiguration.entryTtl(Duration.ofSeconds(30)) |
| 75 | + .serializeValuesWith(stockInfoSerializationPair)); |
66 | 76 |
|
67 | 77 | // 주식 정보: 1시간 캐시 (기본 정보, 변동 적음) |
68 | 78 | cacheConfigurations.put("stockInfo", |
69 | 79 | defaultCacheConfiguration.entryTtl(Duration.ofHours(1))); |
70 | 80 |
|
71 | 81 | // 검색 결과: 30분 캐시 |
72 | 82 | cacheConfigurations.put("searchResult", |
73 | | - defaultCacheConfiguration.entryTtl(Duration.ofMinutes(30))); |
| 83 | + defaultCacheConfiguration.entryTtl(Duration.ofMinutes(30)) |
| 84 | + .serializeValuesWith(stockInfoSerializationPair)); |
74 | 85 |
|
75 | 86 | // 유효한 주식 목록: 1시간 캐시 |
76 | 87 | cacheConfigurations.put("validStocks", |
|
0 commit comments