@Configuration
@EnableCaching //开启缓存支持
public class RedisConfig extends CachingConfigurerSupport{
@Resource
private LettuceConnectionFactory lettuceConnectionFactory;
//@description 自定义的缓存key的生成策略,若想使用这个key 只需要将注解上keyGenerator的值设置为keyGenerator即可
@Override
@Bean
public KeyGenerator keyGenerator(){
return new KeyGenetator(){
@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配置
@Beam
public RedisTemplate<String,Obejct> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
//设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL,Visiblety.ANY);
om.enableDefaultTyping(DefaultTyping.NON_FINAL);
jsckson2JsonRedisSerializer.setObjectMapper(om);
//配置redisTemplate
ReidsTemplate<String,Object> 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 序列化
return redisTemplate;
}
//此处的缓存到java虚拟机内存,非redis
@Override
@Bean
public CacheManager cacheManager(){
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<ConcurrentMapCache> list = new ArrayList<>();
list.add(new ConcurrentMapCache(name:"dictCache"));
list.add(new ConcurrentMapCache(name:"jeecgDemo"));
list.add(new ConcurrentMapCache(name:"permission"));
cacheManager.setCaches(list);
cacheManager.afterPropertiesSet();
return cacheManager;
}
//redis缓存支持设置缓存过期事件
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory){
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTt1(second:3600),//3600秒,默认策略,未配置的key会使用这个
this.getRdisCacheConfigurationMap() //指定key 策略
);
}
//指定redis缓存超时时间
private Map<String,RedisChcheConfiguration> getRedisCacheConfigruationMap(){
Map<Stirng,RedisCacheConfiguration> redisCacheConfigurationMap = new
HashMap<>();
redisCacheConfigurationMap.put("dictTableCache",this.getRedisCacheConfigurationWithTt1(seconds:600));//600秒表数据字典只缓存10分钟
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTt1(Integet seconds){
Jackson2JsonRedisSerializer<Object> 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;
}
}
记事本纯手写,应该有很多不对的地方;
标签:om,缓存,java,Object,redis,return,new,redisTemplate From: https://blog.51cto.com/u_15890333/5884590