All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lone.config.RedisConfig Maven / Gradle / Ivy

The newest version!
package com.lone.config;

import java.lang.reflect.Method;
import java.time.Duration;

import javax.annotation.Resource;

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 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;

@Configuration
@EnableCaching // 开启缓存支持
public class RedisConfig extends CachingConfigurerSupport {

	@Resource
	private LettuceConnectionFactory lettuceConnectionFactory;

	/**
	 * @description 自定义的缓存key的生成策略 若想使用这个key
	 *              只需要讲注解上keyGenerator的值设置为keyGenerator即可
* @return 自定义策略生成的key */ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuffer sb = new StringBuffer(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } /** * RedisTemplate配置 */ @Bean public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 设置序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, Visibility.ANY); om.enableDefaultTyping(DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置redisTemplate RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(lettuceConnectionFactory); RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer);// key序列化 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化 redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化 redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } /** * Redis缓存支持设置缓存过期时间 * @param redisConnectionFactory * @return */ // @Bean // public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) { // return new RedisCacheManager( // RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), // this.getRedisCacheConfigurationWithTtl(3600), //3600秒,默认策略,未配置的 key 会使用这个 // this.getRedisCacheConfigurationMap() // 指定 key 策略 // ); // } /** * 指定redis缓存超时时间 * @return */ // private Map getRedisCacheConfigurationMap() { // Map redisCacheConfigurationMap = new HashMap<>(); // redisCacheConfigurationMap.put("dictTableCache", this.getRedisCacheConfigurationWithTtl(600));// 600秒(表数据字典只缓存10分钟) // // return redisCacheConfigurationMap; // } // private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { // Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); // ObjectMapper om = new ObjectMapper(); // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); // jackson2JsonRedisSerializer.setObjectMapper(om); // // RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); // redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( // RedisSerializationContext // .SerializationPair // .fromSerializer(jackson2JsonRedisSerializer) // ).entryTtl(Duration.ofSeconds(seconds)); // // return redisCacheConfiguration; // } /** * 缓存配置管理器 */ @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); // 创建默认缓存配置对象 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时; RedisCacheManager cacheManager = new RedisCacheManager(writer, config); return cacheManager; } /** * 此处的缓存到JAVA虚拟机内存,非存Redis */ // @Override // @Bean // public CacheManager cacheManager() { // SimpleCacheManager cacheManager = new SimpleCacheManager(); // List list=new ArrayList(); // list.add(new ConcurrentMapCache("dictCache")); // list.add(new ConcurrentMapCache("jeecgDemo")); // list.add(new ConcurrentMapCache("permission")); // cacheManager.setCaches(list); // cacheManager.afterPropertiesSet(); // return cacheManager; // } }