| 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;
|
| 22009 |
ashik.ali |
7 |
import org.springframework.cache.CacheManager;
|
|
|
8 |
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
9 |
import org.springframework.cache.annotation.EnableCaching;
|
| 30213 |
amit.gupta |
10 |
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
| 22009 |
ashik.ali |
11 |
import org.springframework.context.annotation.Bean;
|
|
|
12 |
import org.springframework.context.annotation.Configuration;
|
| 35388 |
amit |
13 |
import org.springframework.context.annotation.PropertySource;
|
| 30289 |
amit.gupta |
14 |
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
15 |
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
16 |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
| 36057 |
ranu |
17 |
import org.springframework.data.redis.core.RedisTemplate;
|
| 30289 |
amit.gupta |
18 |
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
| 36081 |
amit |
19 |
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
| 30289 |
amit.gupta |
20 |
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
| 36057 |
ranu |
21 |
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
| 22009 |
ashik.ali |
22 |
|
| 30289 |
amit.gupta |
23 |
import java.time.Duration;
|
| 36049 |
amit |
24 |
import java.util.HashMap;
|
|
|
25 |
import java.util.Map;
|
| 30213 |
amit.gupta |
26 |
import java.util.concurrent.TimeUnit;
|
| 22009 |
ashik.ali |
27 |
|
|
|
28 |
@Configuration
|
|
|
29 |
@EnableCaching
|
| 35388 |
amit |
30 |
@PropertySource("classpath:application.properties")
|
| 23716 |
amit.gupta |
31 |
public class CacheConfig extends CachingConfigurerSupport {
|
|
|
32 |
|
| 30213 |
amit.gupta |
33 |
@Override
|
| 22009 |
ashik.ali |
34 |
@Bean
|
| 30213 |
amit.gupta |
35 |
public CacheManager cacheManager() {
|
|
|
36 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
37 |
return cacheManager;
|
| 30206 |
amit.gupta |
38 |
}
|
|
|
39 |
|
|
|
40 |
@Bean
|
| 30213 |
amit.gupta |
41 |
public CacheManager timeoutCacheManager() {
|
|
|
42 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
43 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES);
|
|
|
44 |
cacheManager.setCaffeine(caffeine);
|
|
|
45 |
return cacheManager;
|
| 30206 |
amit.gupta |
46 |
}
|
|
|
47 |
|
|
|
48 |
@Bean
|
| 30213 |
amit.gupta |
49 |
public CacheManager timeout15CacheManager() {
|
|
|
50 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
51 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(15, TimeUnit.MINUTES);
|
|
|
52 |
cacheManager.setCaffeine(caffeine);
|
|
|
53 |
return cacheManager;
|
| 22009 |
ashik.ali |
54 |
}
|
| 23716 |
amit.gupta |
55 |
|
| 22009 |
ashik.ali |
56 |
@Bean
|
| 30213 |
amit.gupta |
57 |
public CacheManager oneDayCacheManager() {
|
|
|
58 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
59 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(6, TimeUnit.HOURS);
|
|
|
60 |
cacheManager.setCaffeine(caffeine);
|
|
|
61 |
return cacheManager;
|
| 22009 |
ashik.ali |
62 |
}
|
| 30206 |
amit.gupta |
63 |
|
| 30213 |
amit.gupta |
64 |
|
| 29854 |
amit.gupta |
65 |
@Bean
|
| 30213 |
amit.gupta |
66 |
public CacheManager twoMintimeoutCacheManager() {
|
|
|
67 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
68 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(2, TimeUnit.MINUTES);
|
|
|
69 |
cacheManager.setCaffeine(caffeine);
|
|
|
70 |
return cacheManager;
|
| 29854 |
amit.gupta |
71 |
}
|
| 23716 |
amit.gupta |
72 |
|
| 23405 |
amit.gupta |
73 |
@Bean
|
| 34715 |
ranu |
74 |
public CacheManager fiveMintimeoutCacheManager() {
|
|
|
75 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
76 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES);
|
|
|
77 |
cacheManager.setCaffeine(caffeine);
|
|
|
78 |
return cacheManager;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
@Bean
|
| 30213 |
amit.gupta |
82 |
public CacheManager thirtyMinsTimeOutCacheManager() {
|
|
|
83 |
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
|
|
84 |
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES);
|
|
|
85 |
cacheManager.setCaffeine(caffeine);
|
|
|
86 |
return cacheManager;
|
| 30289 |
amit.gupta |
87 |
|
|
|
88 |
|
| 23405 |
amit.gupta |
89 |
}
|
| 22009 |
ashik.ali |
90 |
|
| 30206 |
amit.gupta |
91 |
|
| 30289 |
amit.gupta |
92 |
@Bean
|
| 30213 |
amit.gupta |
93 |
JedisConnectionFactory jedisConnectionFactory() {
|
|
|
94 |
return new JedisConnectionFactory();
|
|
|
95 |
}
|
|
|
96 |
|
| 22708 |
amit.gupta |
97 |
@Bean
|
| 36057 |
ranu |
98 |
public RedisTemplate<String, Object> redisTemplate() {
|
|
|
99 |
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
|
100 |
template.setConnectionFactory(jedisConnectionFactory());
|
|
|
101 |
template.setKeySerializer(new StringRedisSerializer());
|
|
|
102 |
template.setValueSerializer(new GenericJackson2JsonRedisSerializer(getObjectMapper()));
|
|
|
103 |
template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
104 |
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(getObjectMapper()));
|
|
|
105 |
return template;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
@Bean
|
| 30213 |
amit.gupta |
109 |
public RedisCacheConfiguration cacheConfiguration() {
|
|
|
110 |
return RedisCacheConfiguration.defaultCacheConfig()
|
|
|
111 |
.entryTtl(Duration.ofMinutes(120))
|
|
|
112 |
.disableCachingNullValues()
|
|
|
113 |
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(
|
| 30289 |
amit.gupta |
114 |
new GenericJackson2JsonRedisSerializer(
|
|
|
115 |
this.getObjectMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY))));
|
| 22708 |
amit.gupta |
116 |
}
|
|
|
117 |
|
| 30289 |
amit.gupta |
118 |
private ObjectMapper getObjectMapper() {
|
|
|
119 |
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
|
|
|
120 |
return mapper;
|
|
|
121 |
}
|
|
|
122 |
|
| 23405 |
amit.gupta |
123 |
@Bean
|
| 35600 |
amit |
124 |
public CacheManager redisVeryShortCacheManager() {
|
|
|
125 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
|
|
126 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofMinutes(15)))
|
|
|
127 |
.build();
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
@Bean
|
| 35171 |
amit |
131 |
public CacheManager redisShortCacheManager() {
|
|
|
132 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 35176 |
amit |
133 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofMinutes(60)))
|
| 35171 |
amit |
134 |
.build();
|
|
|
135 |
}
|
| 36081 |
amit |
136 |
private RedisCacheConfiguration jdkCacheConfiguration(Duration ttl) {
|
|
|
137 |
return RedisCacheConfiguration.defaultCacheConfig()
|
|
|
138 |
.entryTtl(ttl)
|
|
|
139 |
.disableCachingNullValues()
|
|
|
140 |
.serializeValuesWith(RedisSerializationContext.SerializationPair
|
|
|
141 |
.fromSerializer(new JdkSerializationRedisSerializer()));
|
|
|
142 |
}
|
|
|
143 |
|
| 35171 |
amit |
144 |
@Bean
|
| 30289 |
amit.gupta |
145 |
public CacheManager redisCacheManager() {
|
| 36081 |
amit |
146 |
RedisCacheConfiguration defaultJsonConfig = cacheConfiguration().entryTtl(Duration.ofHours(6));
|
|
|
147 |
RedisCacheConfiguration jdkConfig = jdkCacheConfiguration(Duration.ofHours(6));
|
|
|
148 |
|
| 36049 |
amit |
149 |
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
|
| 36081 |
amit |
150 |
cacheConfigurations.put("offer.achievement", jdkCacheConfiguration(Duration.ofMinutes(30)));
|
|
|
151 |
// Use JDK serialization for caches returning Map<Integer, ...>
|
|
|
152 |
// to preserve Integer key types (JSON serializes map keys as String)
|
|
|
153 |
cacheConfigurations.put("allOffers", jdkConfig);
|
|
|
154 |
cacheConfigurations.put("offer.slabpayout", jdkConfig);
|
|
|
155 |
cacheConfigurations.put("catalog.published_yearmonth", jdkConfig);
|
|
|
156 |
|
| 34828 |
amit |
157 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 36081 |
amit |
158 |
.cacheDefaults(defaultJsonConfig)
|
| 36049 |
amit |
159 |
.withInitialCacheConfigurations(cacheConfigurations)
|
| 34828 |
amit |
160 |
.build();
|
| 30289 |
amit.gupta |
161 |
}
|
| 33323 |
amit.gupta |
162 |
@Bean
|
| 35388 |
amit |
163 |
public CacheManager redisOneDayCacheManager() {
|
|
|
164 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
|
|
165 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofDays(1)))
|
|
|
166 |
.build();
|
|
|
167 |
}
|
|
|
168 |
@Bean
|
| 33323 |
amit.gupta |
169 |
public CacheManager redisFortnightlyCacheManage() {
|
| 34828 |
amit |
170 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 33323 |
amit.gupta |
171 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ofDays(15).minusMinutes(5)))
|
| 34828 |
amit |
172 |
.build();
|
| 33323 |
amit.gupta |
173 |
}
|
| 23405 |
amit.gupta |
174 |
|
| 30305 |
amit.gupta |
175 |
@Bean
|
|
|
176 |
public CacheManager redisEternalCacheManager() {
|
| 34828 |
amit |
177 |
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory())
|
| 30305 |
amit.gupta |
178 |
.cacheDefaults(cacheConfiguration().entryTtl(Duration.ZERO))
|
| 34828 |
amit |
179 |
.build();
|
| 30305 |
amit.gupta |
180 |
}
|
|
|
181 |
|
| 22009 |
ashik.ali |
182 |
}
|