package com.example.mongodemo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RedisCacheManager;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.lang.reflect.Method;
/**
* @Author: wzp
* @Date: 2020/3/17 10:26
*
* redis 设置过期时间: https://liuyanzhao.com/8257.html
*
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 自定义生成key的规则(类名+方法名+参数)
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
for (Object object : objects) {
sb.append(object.toString());
}
logger.info(sb.toString());
return sb.toString();
}
};
}
// @Bean
// public KeyGenerator appCacheAdapter() {
// return (Object o, Method method, Object... objects)->{return null};
// }
/**
* 设置redsiCacheManger作为缓存管理器
*
* @param connectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheManager redisCacheManager = RedisCacheManager.create(connectionFactory);
return redisCacheManager;
}
/**
* 解决键值序列化
*/
// 同步模板
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer()); // GenericJackson2JsonRedisSerializer
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // GenericJackson2JsonRedisSerializer
template.setEnableTransactionSupport(true);
return template;
}
// @Bean
// public ReactiveRedisTemplate<String,Object> reactiveRedisTemplate(RedisConnectionFactory factory) {
// ReactiveRedisTemplate<String, Object> redisTemplate = new ReactiveRedisTemplate<>(factory);
// redisTemplate.setConnectionFactory(factory);
// Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// ObjectMapper mapper = new ObjectMapper();
// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// jsonRedisSerializer.setObjectMapper(mapper);
// StringRedisSerializer stringSerializer = new StringRedisSerializer();
// redisTemplate.setKeySerializer(stringSerializer);
// redisTemplate.setHashKeySerializer(stringSerializer);
// redisTemplate.setValueSerializer(jsonRedisSerializer);
// redisTemplate.setHashValueSerializer(jsonRedisSerializer);
// redisTemplate.afterPropertiesSet();
//
// return redisTemplate;
// }
@Bean(name = "reactiveRedisTemplate")
//@Qualifier("reactiveRedisTemplate")
public ReactiveRedisTemplate<String, Object> stringReactiveRedisTemplate(final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
// StringRedisSerializer keySerializer = new StringRedisSerializer();
// Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// ObjectMapper mapper = new ObjectMapper();
// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
// jsonRedisSerializer.setObjectMapper(mapper);
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
.<String, Object>newSerializationContext()
.key(new StringRedisSerializer())
.value(new GenericJackson2JsonRedisSerializer())
.hashKey(new StringRedisSerializer())
.hashValue(new GenericJackson2JsonRedisSerializer())
.build();
return new ReactiveRedisTemplate<String, Object>(reactiveRedisConnectionFactory, serializationContext);
}
}
redis序列化配置
最新推荐文章于 2024-07-08 15:44:41 发布