|
1 | 1 | package com.msa.commerce.common.config;
|
2 | 2 |
|
3 |
| -import com.msa.commerce.common.config.cache.CacheDefinition; |
4 |
| -import com.msa.commerce.common.config.cache.CacheStrategy; |
5 |
| -import com.msa.commerce.common.config.cache.DomainCacheRegistry; |
| 3 | +import java.time.Duration; |
| 4 | + |
6 | 5 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
7 | 6 | import org.springframework.cache.annotation.EnableCaching;
|
8 | 7 | import org.springframework.context.annotation.Bean;
|
|
15 | 14 | import org.springframework.data.redis.serializer.RedisSerializationContext;
|
16 | 15 | import org.springframework.data.redis.serializer.StringRedisSerializer;
|
17 | 16 |
|
18 |
| -import jakarta.annotation.PostConstruct; |
19 |
| -import java.time.Duration; |
20 |
| -import java.util.HashMap; |
21 |
| -import java.util.Map; |
22 |
| -import java.util.Set; |
23 |
| - |
24 | 17 | @Configuration
|
25 | 18 | @EnableCaching
|
26 | 19 | @ConditionalOnProperty(name = "spring.data.redis.host")
|
27 | 20 | public class RedisConfig {
|
28 | 21 |
|
29 |
| - public static final String DEFAULT_CACHE = "default"; |
30 |
| - public static final String PRODUCT_CACHE = "product"; |
31 |
| - public static final String PRODUCT_VIEW_COUNT_CACHE = "product-view-count"; |
32 |
| - public static final String USER_CACHE = "user"; |
33 |
| - public static final String ORDER_CACHE = "order"; |
34 |
| - public static final String PAYMENT_CACHE = "payment"; |
35 |
| - |
36 |
| - @PostConstruct |
37 |
| - public void registerDefaultCaches() { |
38 |
| - DomainCacheRegistry.registerCaches( |
39 |
| - CacheDefinition.of(PRODUCT_CACHE, CacheStrategy.LONG_TERM, |
40 |
| - "Product basic information - master data that changes infrequently"), |
41 |
| - CacheDefinition.of(PRODUCT_VIEW_COUNT_CACHE, CacheStrategy.SHORT_TERM, |
42 |
| - "Product view count - real-time critical data"), |
43 |
| - |
44 |
| - CacheDefinition.of(USER_CACHE, CacheStrategy.MEDIUM_TERM, |
45 |
| - "User profile information - session based data"), |
46 |
| - |
47 |
| - CacheDefinition.of(ORDER_CACHE, CacheStrategy.MEDIUM_TERM, |
48 |
| - "Order information - transactional data with state changes"), |
49 |
| - |
50 |
| - CacheDefinition.of(PAYMENT_CACHE, CacheStrategy.SHORT_TERM, |
51 |
| - "Payment information - rapidly changing status data"), |
52 |
| - |
53 |
| - CacheDefinition.of(DEFAULT_CACHE, CacheStrategy.DEFAULT, |
54 |
| - "Default general purpose cache") |
55 |
| - ); |
56 |
| - } |
| 22 | + private static final Duration DEFAULT_TTL = Duration.ofMinutes(30); |
57 | 23 |
|
58 | 24 | @Bean
|
59 | 25 | public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
60 | 26 | RedisTemplate<String, Object> template = new RedisTemplate<>();
|
61 | 27 | template.setConnectionFactory(connectionFactory);
|
62 |
| - |
| 28 | + |
63 | 29 | template.setKeySerializer(new StringRedisSerializer());
|
64 | 30 | template.setHashKeySerializer(new StringRedisSerializer());
|
65 |
| - |
66 |
| - GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); |
67 |
| - template.setValueSerializer(serializer); |
68 |
| - template.setHashValueSerializer(serializer); |
69 |
| - |
| 31 | + |
| 32 | + GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer(); |
| 33 | + template.setValueSerializer(jsonSerializer); |
| 34 | + template.setHashValueSerializer(jsonSerializer); |
| 35 | + |
70 | 36 | template.afterPropertiesSet();
|
71 | 37 | return template;
|
72 | 38 | }
|
73 | 39 |
|
74 | 40 | @Bean
|
75 | 41 | public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
|
76 | 42 | RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
|
77 |
| - .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) |
78 |
| - .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) |
79 |
| - .entryTtl(CacheStrategy.DEFAULT.getTtl()) |
80 |
| - .disableCachingNullValues(); |
81 |
| - |
82 |
| - Map<String, RedisCacheConfiguration> cacheConfigurations = buildCacheConfigurations(defaultConfig); |
| 43 | + .serializeKeysWith(RedisSerializationContext.SerializationPair |
| 44 | + .fromSerializer(new StringRedisSerializer())) |
| 45 | + .serializeValuesWith(RedisSerializationContext.SerializationPair |
| 46 | + .fromSerializer(new GenericJackson2JsonRedisSerializer())) |
| 47 | + .entryTtl(DEFAULT_TTL) |
| 48 | + .disableCachingNullValues(); |
83 | 49 |
|
84 | 50 | return RedisCacheManager.builder(connectionFactory)
|
85 |
| - .cacheDefaults(defaultConfig) |
86 |
| - .withInitialCacheConfigurations(cacheConfigurations) |
87 |
| - .transactionAware() |
88 |
| - .build(); |
89 |
| - } |
90 |
| - |
91 |
| - private Map<String, RedisCacheConfiguration> buildCacheConfigurations(RedisCacheConfiguration defaultConfig) { |
92 |
| - Map<String, RedisCacheConfiguration> configurations = new HashMap<>(); |
93 |
| - |
94 |
| - DomainCacheRegistry.getAllCacheDefinitions().forEach((cacheName, cacheDefinition) -> { |
95 |
| - RedisCacheConfiguration config = defaultConfig.entryTtl(cacheDefinition.getTtl()); |
96 |
| - configurations.put(cacheName, config); |
97 |
| - }); |
98 |
| - |
99 |
| - return configurations; |
| 51 | + .cacheDefaults(defaultConfig) |
| 52 | + .transactionAware() |
| 53 | + .build(); |
100 | 54 | }
|
101 | 55 |
|
102 |
| - public static class CacheNames { |
103 |
| - public static final String DEFAULT = DEFAULT_CACHE; |
104 |
| - public static final String PRODUCT = PRODUCT_CACHE; |
105 |
| - public static final String PRODUCT_VIEW_COUNT = PRODUCT_VIEW_COUNT_CACHE; |
106 |
| - public static final String USER = USER_CACHE; |
107 |
| - public static final String ORDER = ORDER_CACHE; |
108 |
| - public static final String PAYMENT = PAYMENT_CACHE; |
109 |
| - |
110 |
| - private CacheNames() { |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - public static class CacheUtils { |
115 |
| - |
116 |
| - public static void registerCache(String cacheName, CacheStrategy strategy, String description) { |
117 |
| - DomainCacheRegistry.registerCache( |
118 |
| - CacheDefinition.of(cacheName, strategy, description) |
119 |
| - ); |
120 |
| - } |
121 |
| - |
122 |
| - public static CacheDefinition getCacheInfo(String cacheName) { |
123 |
| - return DomainCacheRegistry.getCacheDefinition(cacheName); |
124 |
| - } |
125 |
| - |
126 |
| - public static Set<String> getAllCacheNames() { |
127 |
| - return DomainCacheRegistry.getCacheNames(); |
128 |
| - } |
129 |
| - |
130 |
| - public static boolean isCacheRegistered(String cacheName) { |
131 |
| - return DomainCacheRegistry.isCacheRegistered(cacheName); |
132 |
| - } |
133 |
| - |
134 |
| - public static String generateCacheName(String domain, String type) { |
135 |
| - if (domain == null || domain.trim().isEmpty()) { |
136 |
| - throw new IllegalArgumentException("Domain cannot be null or empty"); |
137 |
| - } |
138 |
| - if (type == null || type.trim().isEmpty()) { |
139 |
| - throw new IllegalArgumentException("Type cannot be null or empty"); |
140 |
| - } |
141 |
| - return domain.toLowerCase().trim() + "-" + type.toLowerCase().trim(); |
142 |
| - } |
143 |
| - |
144 |
| - public static String generateHierarchicalCacheName(String... parts) { |
145 |
| - if (parts == null || parts.length == 0) { |
146 |
| - throw new IllegalArgumentException("At least one part is required"); |
147 |
| - } |
148 |
| - return String.join(":", parts).toLowerCase(); |
149 |
| - } |
150 |
| - |
151 |
| - private CacheUtils() { |
152 |
| - } |
153 |
| - } |
154 | 56 | }
|
0 commit comments