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