|
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
@@ -13,6 +14,7 @@ import org.springframework.cache.annotation.EnableCaching;
|
|
|
import org.springframework.cache.interceptor.KeyGenerator;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
@@ -60,6 +62,7 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
private int minIdle;
|
|
|
|
|
|
@Autowired
|
|
|
+ @Qualifier("factoryForCache")
|
|
|
private LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
|
|
|
@Bean
|
|
@@ -72,24 +75,6 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
return poolConfig;
|
|
|
}
|
|
|
|
|
|
- @Bean(destroyMethod = "destroy")
|
|
|
- public LettuceConnectionFactory redisConnectionFactory() {
|
|
|
- log.info("Create LettuceConnectionFactory successful");
|
|
|
- RedisStandaloneConfiguration connection = new RedisStandaloneConfiguration();
|
|
|
- connection.setHostName(host);
|
|
|
- connection.setPort(port);
|
|
|
- connection.setPassword(password);
|
|
|
- connection.setDatabase(Integer.valueOf(databaseCache));
|
|
|
- GenericObjectPoolConfig poolConfig = getRedisConfig();
|
|
|
- LettuceClientConfiguration builder = LettucePoolingClientConfiguration.builder()
|
|
|
- .commandTimeout(Duration.ofMillis(timeout))
|
|
|
- .poolConfig(poolConfig)
|
|
|
- .shutdownTimeout(Duration.ZERO)
|
|
|
- .build();
|
|
|
- LettuceConnectionFactory factory = new LettuceConnectionFactory(connection, builder);
|
|
|
- return factory;
|
|
|
- }
|
|
|
-
|
|
|
@Bean
|
|
|
@Override
|
|
|
public CacheManager cacheManager() {
|
|
@@ -114,19 +99,19 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
return new GenericJackson2JsonRedisSerializer();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 缓存使用的redis
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean("factoryForCache")
|
|
|
+ @Primary
|
|
|
+ public LettuceConnectionFactory redisConnectionFactory() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseCache));
|
|
|
+ }
|
|
|
@Bean
|
|
|
- public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
|
|
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
|
|
- redisTemplate.setConnectionFactory(lettuceConnectionFactory);
|
|
|
-
|
|
|
- // value值的序列化
|
|
|
- redisTemplate.setValueSerializer(getSerializer());
|
|
|
- redisTemplate.setHashValueSerializer(getSerializer());
|
|
|
- // key的序列化采用StringRedisSerializer
|
|
|
- redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
- redisTemplate.afterPropertiesSet();
|
|
|
- return redisTemplate;
|
|
|
+ public RedisTemplate<String, Object> redisTemplate() {
|
|
|
+ return getRedisTemplate(lettuceConnectionFactory);
|
|
|
}
|
|
|
|
|
|
private Jackson2JsonRedisSerializer getSerializer() {
|
|
@@ -143,7 +128,7 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
public KeyGenerator keyGenerator() {
|
|
|
// 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
|
|
|
// 使用:进行分割,可以很多显示出层级关系
|
|
|
- // 这里其实就是new了一个KeyGenerator对象,只是这是lambda表达式的写法,我感觉很好用,大家感兴趣可以去了解下
|
|
|
+ // 这里其实就是new了一个KeyGenerator对象
|
|
|
return (target, method, params) -> {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
sb.append(target.getClass().getName());
|
|
@@ -158,15 +143,19 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 短信验证使用的redis
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Bean("factoryForIdc")
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForIdc() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseIdc));
|
|
|
+ }
|
|
|
+
|
|
|
@Bean(name = "redisTemplateForIdc")
|
|
|
- public RedisTemplate<String, Object> redisTemplateForIdc() {
|
|
|
- return getRedisTemplate(Integer.valueOf(databaseIdc));
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForIdc(@Qualifier("factoryForIdc") LettuceConnectionFactory factory) {
|
|
|
+ return getRedisTemplate(factory);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -174,9 +163,14 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Bean("factoryForSms")
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForSms() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseSms));
|
|
|
+ }
|
|
|
+
|
|
|
@Bean(name = "redisTemplateForSms")
|
|
|
- public RedisTemplate<String, Object> redisTemplateForSms() {
|
|
|
- return getRedisTemplate(Integer.valueOf(databaseSms));
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForSms(@Qualifier("factoryForSms") LettuceConnectionFactory factory) {
|
|
|
+ return getRedisTemplate(factory);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -184,9 +178,14 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Bean("factoryForImg")
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForImg() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseImg));
|
|
|
+ }
|
|
|
+
|
|
|
@Bean(name = "redisTemplateForImg")
|
|
|
- public RedisTemplate<String, Object> redisTemplateForImg() {
|
|
|
- return getRedisTemplate(Integer.valueOf(databaseImg));
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForImg(@Qualifier("factoryForImg") LettuceConnectionFactory factory) {
|
|
|
+ return getRedisTemplate(factory);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -194,12 +193,17 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Bean("factoryForToken")
|
|
|
+ public LettuceConnectionFactory redisConnectionFactoryForToken() {
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseToken));
|
|
|
+ }
|
|
|
+
|
|
|
@Bean(name = "redisTemplateForToken")
|
|
|
- public RedisTemplate<String, Object> redisTemplateForToken() {
|
|
|
- return getRedisTemplate(Integer.valueOf(databaseToken));
|
|
|
+ public RedisTemplate<String, Object> redisTemplateForToken(@Qualifier("factoryForToken") LettuceConnectionFactory factory) {
|
|
|
+ return getRedisTemplate(factory);
|
|
|
}
|
|
|
|
|
|
- private RedisTemplate<String, Object> getRedisTemplate(Integer database) {
|
|
|
+ private LettuceConnectionFactory getRedisConnectionFactory(Integer database) {
|
|
|
RedisStandaloneConfiguration connection = new RedisStandaloneConfiguration();
|
|
|
connection.setHostName(host);
|
|
|
connection.setPort(port);
|
|
@@ -212,8 +216,11 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
.shutdownTimeout(Duration.ZERO)
|
|
|
.build();
|
|
|
LettuceConnectionFactory factory = new LettuceConnectionFactory(connection, builder);
|
|
|
+ return factory;
|
|
|
+ }
|
|
|
|
|
|
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
|
|
+ private RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory factory) {
|
|
|
+ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
redisTemplate.setConnectionFactory(factory);
|
|
|
|
|
|
// value值的序列化
|