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