|
@@ -5,25 +5,37 @@ import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
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.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.cache.CacheManager;
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|
|
|
+import org.springframework.cache.annotation.EnableCaching;
|
|
|
|
+import org.springframework.cache.interceptor.KeyGenerator;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
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;
|
|
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
|
|
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
|
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
|
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
|
|
+@EnableCaching
|
|
@Slf4j
|
|
@Slf4j
|
|
public class RedisConfigurer extends CachingConfigurerSupport {
|
|
public class RedisConfigurer extends CachingConfigurerSupport {
|
|
|
|
|
|
|
|
+ @Value("${spring.redis.database.cache}")
|
|
|
|
+ private String databaseCache;
|
|
@Value("${spring.redis.database.token}")
|
|
@Value("${spring.redis.database.token}")
|
|
private String databaseMr;
|
|
private String databaseMr;
|
|
@Value("${spring.redis.host}")
|
|
@Value("${spring.redis.host}")
|
|
@@ -43,6 +55,10 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
@Value("${spring.redis.lettuce.pool.min-idle}")
|
|
@Value("${spring.redis.lettuce.pool.min-idle}")
|
|
private int minIdle;
|
|
private int minIdle;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ @Qualifier("factoryForCache")
|
|
|
|
+ private LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
|
+
|
|
@Bean
|
|
@Bean
|
|
public GenericObjectPoolConfig getRedisConfig() {
|
|
public GenericObjectPoolConfig getRedisConfig() {
|
|
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
|
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
|
@@ -53,6 +69,45 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
return poolConfig;
|
|
return poolConfig;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Bean
|
|
|
|
+ @Override
|
|
|
|
+ public CacheManager cacheManager() {
|
|
|
|
+ RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
|
|
|
|
+ // 设置 key为string序列化
|
|
|
|
+ .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
|
|
|
|
+ // 设置value为json序列化
|
|
|
|
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getSerializer()))
|
|
|
|
+ // 不缓存空值
|
|
|
|
+ .disableCachingNullValues();
|
|
|
|
+ RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory)
|
|
|
|
+ .cacheDefaults(redisCacheConfiguration)
|
|
|
|
+ .transactionAware()
|
|
|
|
+ .build();
|
|
|
|
+ cacheManager.afterPropertiesSet();
|
|
|
|
+ log.info("RedisCacheManager config success");
|
|
|
|
+ return cacheManager;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Bean(name = "springSessionDefaultRedisSerializer")
|
|
|
|
+ public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() {
|
|
|
|
+ return new GenericJackson2JsonRedisSerializer();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 缓存使用的redis
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Bean("factoryForCache")
|
|
|
|
+ @Primary
|
|
|
|
+ public LettuceConnectionFactory redisConnectionFactory() {
|
|
|
|
+ return getRedisConnectionFactory(Integer.valueOf(databaseCache));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplate() {
|
|
|
|
+ return getRedisTemplate(lettuceConnectionFactory);
|
|
|
|
+ }
|
|
|
|
|
|
private Jackson2JsonRedisSerializer getSerializer() {
|
|
private Jackson2JsonRedisSerializer getSerializer() {
|
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
|
@@ -63,6 +118,26 @@ public class RedisConfigurer extends CachingConfigurerSupport {
|
|
return jackson2JsonRedisSerializer;
|
|
return jackson2JsonRedisSerializer;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Bean
|
|
|
|
+ @Override
|
|
|
|
+ public KeyGenerator keyGenerator() {
|
|
|
|
+ // 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
|
|
|
|
+ // 使用:进行分割,可以很多显示出层级关系
|
|
|
|
+ // 这里其实就是new了一个KeyGenerator对象
|
|
|
|
+ return (target, method, params) -> {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ sb.append(target.getClass().getName());
|
|
|
|
+ sb.append(":");
|
|
|
|
+ sb.append(method.getName());
|
|
|
|
+ for (Object obj : params) {
|
|
|
|
+ sb.append(":" + String.valueOf(obj));
|
|
|
|
+ }
|
|
|
|
+ String rsToUse = String.valueOf(sb);
|
|
|
|
+ log.info("自动生成Redis Key -> [{}]", rsToUse);
|
|
|
|
+ return rsToUse;
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Token使用的redis
|
|
* Token使用的redis
|
|
*
|
|
*
|