|
@@ -1,109 +0,0 @@
|
|
-package com.xxl.job.executor.config;
|
|
|
|
-
|
|
|
|
-import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
|
|
|
-import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
|
|
|
-import org.springframework.cache.CacheManager;
|
|
|
|
-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.Configuration;
|
|
|
|
-import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
|
-import org.springframework.data.redis.cache.RedisCacheManager;
|
|
|
|
-import org.springframework.data.redis.cache.RedisCacheWriter;
|
|
|
|
-import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
-import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
|
-import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
|
-import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
-
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
-import java.lang.reflect.Method;
|
|
|
|
-import java.time.Duration;
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-
|
|
|
|
-@Configuration
|
|
|
|
-@EnableCaching
|
|
|
|
-public class RedisConfig extends CachingConfigurerSupport {
|
|
|
|
-
|
|
|
|
- @Resource
|
|
|
|
- private LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @return 自定义策略生成的key
|
|
|
|
- * @description 自定义的缓存key的生成策略 若想使用这个key
|
|
|
|
- * 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
|
|
|
|
- */
|
|
|
|
- @Override
|
|
|
|
- @Bean
|
|
|
|
- public KeyGenerator keyGenerator() {
|
|
|
|
- return new KeyGenerator() {
|
|
|
|
- @Override
|
|
|
|
- public Object generate(Object target, Method method, Object... params) {
|
|
|
|
-
|
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- sb.append(target.getClass().getName());
|
|
|
|
- sb.append(method.getDeclaringClass().getName());
|
|
|
|
- Arrays.stream(params).map(Object::toString).forEach(sb::append);
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * RedisTemplate配置
|
|
|
|
- */
|
|
|
|
- @Bean
|
|
|
|
- public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
|
|
|
- // 设置序列化
|
|
|
|
- Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
|
|
|
|
- ObjectMapper om = new ObjectMapper();
|
|
|
|
- om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
|
|
|
|
- om.enableDefaultTyping(DefaultTyping.NON_FINAL);
|
|
|
|
- jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
|
- // 配置redisTemplate
|
|
|
|
- RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
|
|
|
|
- redisTemplate.setConnectionFactory(lettuceConnectionFactory);
|
|
|
|
- RedisSerializer<?> stringSerializer = new StringRedisSerializer();
|
|
|
|
- // key序列化
|
|
|
|
- redisTemplate.setKeySerializer(stringSerializer);
|
|
|
|
- // value序列化
|
|
|
|
- redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
|
|
|
- // Hash key序列化
|
|
|
|
- redisTemplate.setHashKeySerializer(stringSerializer);
|
|
|
|
- // Hash value序列化
|
|
|
|
- redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
|
|
|
|
- redisTemplate.afterPropertiesSet();
|
|
|
|
- return redisTemplate;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 缓存配置管理器
|
|
|
|
- */
|
|
|
|
- @Bean
|
|
|
|
- public CacheManager cacheManager(LettuceConnectionFactory factory) {
|
|
|
|
- // 以锁写入的方式创建RedisCacheWriter对象
|
|
|
|
- RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(factory);
|
|
|
|
- /**
|
|
|
|
- * 设置CacheManager的Value序列化方式为JdkSerializationRedisSerializer,
|
|
|
|
- * 但其实RedisCacheConfiguration默认就是使用 StringRedisSerializer序列化key,
|
|
|
|
- * JdkSerializationRedisSerializer序列化value, 所以以下注释代码就是默认实现,没必要写,直接注释掉
|
|
|
|
- */
|
|
|
|
- // RedisSerializationContext.SerializationPair pair =
|
|
|
|
- // RedisSerializationContext.SerializationPair.fromSerializer(new
|
|
|
|
- // JdkSerializationRedisSerializer(this.getClass().getClassLoader()));
|
|
|
|
- // RedisCacheConfiguration config =
|
|
|
|
- // RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
|
|
|
|
- // 创建默认缓存配置对象
|
|
|
|
-
|
|
|
|
- // 创建默认缓存配置对象
|
|
|
|
- //设置缓存默认有效期3小时;
|
|
|
|
- RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(3));
|
|
|
|
-
|
|
|
|
- RedisCacheManager cacheManager = new RedisCacheManager(writer, config);
|
|
|
|
- return cacheManager;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|