| 22009 |
ashik.ali |
1 |
package com.spice.profitmandi.dao.config;
|
|
|
2 |
|
| 30289 |
amit.gupta |
3 |
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
5 |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
| 30213 |
amit.gupta |
6 |
import com.github.benmanes.caffeine.cache.Caffeine;
|
| 36521 |
amit |
7 |
import org.apache.logging.log4j.LogManager;
|
|
|
8 |
import org.apache.logging.log4j.Logger;
|
|
|
9 |
import org.springframework.cache.Cache;
|
| 22009 |
ashik.ali |
10 |
import org.springframework.cache.CacheManager;
|
|
|
11 |
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
12 |
import org.springframework.cache.annotation.EnableCaching;
|
| 30213 |
amit.gupta |
13 |
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
| 36521 |
amit |
14 |
import org.springframework.cache.interceptor.CacheErrorHandler;
|
| 22009 |
ashik.ali |
15 |
import org.springframework.context.annotation.Bean;
|
|
|
16 |
import org.springframework.context.annotation.Configuration;
|
| 35388 |
amit |
17 |
import org.springframework.context.annotation.PropertySource;
|
| 30289 |
amit.gupta |
18 |
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
19 |
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
20 |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
| 36057 |
ranu |
21 |
import org.springframework.data.redis.core.RedisTemplate;
|
| 30289 |
amit.gupta |
22 |
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
| 36081 |
amit |
23 |
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
| 30289 |
amit.gupta |
24 |
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
| 36057 |
ranu |
25 |
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
| 22009 |
ashik.ali |
26 |
|
| 30289 |
amit.gupta |
27 |
import java.time.Duration;
|
| 36049 |
amit |
28 |
import java.util.HashMap;
|
|
|
29 |
import java.util.Map;
|
| 30213 |
amit.gupta |
30 |
import java.util.concurrent.TimeUnit;
|
| 22009 |
ashik.ali |
31 |
|
|
|
32 |
@Configuration
|
|
|
33 |
@EnableCaching
|
| 35388 |
amit |
34 |
@PropertySource("classpath:application.properties")
|
| 23716 |
amit.gupta |
35 |
public class CacheConfig extends CachingConfigurerSupport {
|
|
|
36 |
|
| 36521 |
amit |
37 |
private static final Logger LOGGER = LogManager.getLogger(CacheConfig.class);
|
|
|
38 |
|
| 30213 |
amit.gupta |
39 |
@Override
|
| 36521 |
amit |
40 |
public CacheErrorHandler errorHandler() {
|
|
|
41 |
return new CacheErrorHandler() {
|
|
|
42 |
@Override
|
|
|
43 |
public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
|
|
|
44 |
LOGGER.warn("Cache get error on [{}] key [{}]: {}", cache.getName(), key, e.getMessage());
|
|
|
45 |
cache.evict(key);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
@Override
|
|
|
49 |
public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
|
|
|
50 |
LOGGER.warn("Cache put error on [{}] key [{}]: {}", cache.getName(), key, e.getMessage());
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
@Override
|
|
|
54 |
public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
|
|
|
55 |
LOGGER.warn("Cache evict error on [{}] key [{}]: {}", cache.getName(), key, e.getMessage());
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public void handleCacheClearError(RuntimeException e, Cache cache) {
|
|
|
60 |
LOGGER.warn("Cache clear error on [{}]: {}", cache.getName(), e.getMessage());
|
|
|
61 |
}
|
|
|
62 |
};
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
@Override
|
| 22009 |
ashik.ali |
66 |
@Bean
|
| 30213 |
amit.gupta |
67 |
public CacheManager cacheManager() {
|
|
|
68 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
69 |
return cacheManager;
|
| 30206 |
amit.gupta |
70 |
}
|
|
|
71 |
|
|
|
72 |
@Bean
|
| 30213 |
amit.gupta |
73 |
public CacheManager timeoutCacheManager() {
|
|
|
74 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
75 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES);
|
|
|
76 |
cacheManager.setCaffeine(caffeine);
|
|
|
77 |
return cacheManager;
|
| 30206 |
amit.gupta |
78 |
}
|
|
|
79 |
|
|
|
80 |
@Bean
|
| 30213 |
amit.gupta |
81 |
public CacheManager timeout15CacheManager() {
|
|
|
82 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
83 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(15, TimeUnit.MINUTES);
|
|
|
84 |
cacheManager.setCaffeine(caffeine);
|
|
|
85 |
return cacheManager;
|
| 22009 |
ashik.ali |
86 |
}
|
| 23716 |
amit.gupta |
87 |
|
| 22009 |
ashik.ali |
88 |
@Bean
|
| 30213 |
amit.gupta |
89 |
public CacheManager oneDayCacheManager() {
|
|
|
90 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
91 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(6, TimeUnit.HOURS);
|
|
|
92 |
cacheManager.setCaffeine(caffeine);
|
|
|
93 |
return cacheManager;
|
| 22009 |
ashik.ali |
94 |
}
|
| 30206 |
amit.gupta |
95 |
|
| 30213 |
amit.gupta |
96 |
|
| 29854 |
amit.gupta |
97 |
@Bean
|
| 30213 |
amit.gupta |
98 |
public CacheManager twoMintimeoutCacheManager() {
|
|
|
99 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
100 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(2, TimeUnit.MINUTES);
|
|
|
101 |
cacheManager.setCaffeine(caffeine);
|
|
|
102 |
return cacheManager;
|
| 29854 |
amit.gupta |
103 |
}
|
| 23716 |
amit.gupta |
104 |
|
| 23405 |
amit.gupta |
105 |
@Bean
|
| 34715 |
ranu |
106 |
public CacheManager fiveMintimeoutCacheManager() {
|
|
|
107 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
108 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES);
|
|
|
109 |
cacheManager.setCaffeine(caffeine);
|
|
|
110 |
return cacheManager;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
@Bean
|
| 30213 |
amit.gupta |
114 |
public CacheManager thirtyMinsTimeOutCacheManager() {
|
|
|
115 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
116 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES);
|
|
|
117 |
cacheManager.setCaffeine(caffeine);
|
|
|
118 |
return cacheManager;
|
| 30289 |
amit.gupta |
119 |
|
|
|
120 |
|
| 23405 |
amit.gupta |
121 |
}
|
| 22009 |
ashik.ali |
122 |
|
| 30206 |
amit.gupta |
123 |
|
| 30289 |
amit.gupta |
124 |
@Bean
|
| 30213 |
amit.gupta |
125 |
JedisConnectionFactory jedisConnectionFactory() {
|
|
|
126 |
return new JedisConnectionFactory();
|
|
|
127 |
}
|
|
|
128 |
|
| 22708 |
amit.gupta |
129 |
@Bean
|
| 36057 |
ranu |
130 |
public RedisTemplate<String, Object> redisTemplate() {
|
|
|
131 |
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
|
132 |
template.setConnectionFactory(jedisConnectionFactory());
|
|
|
133 |
template.setKeySerializer(new StringRedisSerializer());
|
|
|
134 |
template.setValueSerializer(new GenericJackson2JsonRedisSerializer(getObjectMapper()));
|
|
|
135 |
template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
136 |
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(getObjectMapper()));
|
|
|
137 |
return template;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
@Bean
|
| 30213 |
amit.gupta |
141 |
public RedisCacheConfiguration cacheConfiguration() {
|
|
|
142 |
return RedisCacheConfiguration.defaultCacheConfig()
|
|
|
143 |
.entryTtl(Duration.ofMinutes(120))
|
|
|
144 |
.disableCachingNullValues()
|
|
|
145 |
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(
|
| 30289 |
amit.gupta |
146 |
new GenericJackson2JsonRedisSerializer(
|
|
|
147 |
this.getObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY))));
|
| 22708 |
amit.gupta |
148 |
}
|
|
|
149 |
|
| 30289 |
amit.gupta |
150 |
private ObjectMapper getObjectMapper() {
|
|
|
151 |
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
|
|
|
152 |
return mapper;
|
|
|
153 |
}
|
|
|
154 |
|
| 23405 |
amit.gupta |
155 |
@Bean
|
| 35600 |
amit |
156 |
public CacheManager redisVeryShortCacheManager() {
|
|
|
157 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
|
|
158 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofMinutes(15)))
|
|
|
159 |
.build();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
@Bean
|
| 35171 |
amit |
163 |
public CacheManager redisShortCacheManager() {
|
|
|
164 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 35176 |
amit |
165 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofMinutes(60)))
|
| 35171 |
amit |
166 |
.build();
|
|
|
167 |
}
|
| 36081 |
amit |
168 |
private RedisCacheConfiguration jdkCacheConfiguration(Duration ttl) {
|
|
|
169 |
return RedisCacheConfiguration.defaultCacheConfig()
|
|
|
170 |
.entryTtl(ttl)
|
|
|
171 |
.disableCachingNullValues()
|
|
|
172 |
.serializeValuesWith(RedisSerializationContext.SerializationPair
|
|
|
173 |
.fromSerializer(new JdkSerializationRedisSerializer()));
|
|
|
174 |
}
|
|
|
175 |
|
| 35171 |
amit |
176 |
@Bean
|
| 30289 |
amit.gupta |
177 |
public CacheManager redisCacheManager() {
|
| 36081 |
amit |
178 |
RedisCacheConfiguration defaultJsonConfig = cacheConfiguration().entryTtl(Duration.ofHours(6));
|
|
|
179 |
RedisCacheConfiguration jdkConfig = jdkCacheConfiguration(Duration.ofHours(6));
|
|
|
180 |
|
| 36049 |
amit |
181 |
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
|
| 36081 |
amit |
182 |
cacheConfigurations.put("offer.achievement", jdkCacheConfiguration(Duration.ofMinutes(30)));
|
| 36521 |
amit |
183 |
cacheConfigurations.put("offer.definition", jdkConfig);
|
|
|
184 |
cacheConfigurations.put("monthOfferIds", jdkConfig);
|
| 36081 |
amit |
185 |
cacheConfigurations.put("offer.slabpayout", jdkConfig);
|
|
|
186 |
cacheConfigurations.put("catalog.published_yearmonth", jdkConfig);
|
|
|
187 |
|
| 34828 |
amit |
188 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 36081 |
amit |
189 |
.cacheDefaults(defaultJsonConfig)
|
| 36049 |
amit |
190 |
.withInitialCacheConfigurations(cacheConfigurations)
|
| 34828 |
amit |
191 |
.build();
|
| 30289 |
amit.gupta |
192 |
}
|
| 33323 |
amit.gupta |
193 |
@Bean
|
| 36305 |
amit |
194 |
public CacheManager redisThreeHourCacheManager() {
|
|
|
195 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
|
|
196 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofHours(3)))
|
|
|
197 |
.build();
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
@Bean
|
| 35388 |
amit |
201 |
public CacheManager redisOneDayCacheManager() {
|
|
|
202 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
|
|
203 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofDays(1)))
|
|
|
204 |
.build();
|
|
|
205 |
}
|
|
|
206 |
@Bean
|
| 33323 |
amit.gupta |
207 |
public CacheManager redisFortnightlyCacheManage() {
|
| 34828 |
amit |
208 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 33323 |
amit.gupta |
209 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofDays(15).minusMinutes(5)))
|
| 34828 |
amit |
210 |
.build();
|
| 33323 |
amit.gupta |
211 |
}
|
| 23405 |
amit.gupta |
212 |
|
| 30305 |
amit.gupta |
213 |
@Bean
|
|
|
214 |
public CacheManager redisEternalCacheManager() {
|
| 34828 |
amit |
215 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 30305 |
amit.gupta |
216 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ZERO))
|
| 34828 |
amit |
217 |
.build();
|
| 30305 |
amit.gupta |
218 |
}
|
|
|
219 |
|
| 22009 |
ashik.ali |
220 |
}
|