java RedisConfig
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java RedisConfig相关的知识,希望对你有一定的参考价值。
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
/**
* Descritpion:
*
* @author Jeason
* @date 2018/10/1
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Override
@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();
}
};
}
@Bean
CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
//设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
//初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
return cacheManager;
}
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
//StringRedisTemplate的构造方法中默认设置了stringSerializer
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//set key serializer
\!h StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
\!h template.setKeySerializer(stringRedisSerializer);
\!h template.setHashKeySerializer(stringRedisSerializer);
//set value serializer
\!h Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
\!h template.setValueSerializer(jackson2JsonRedisSerializer);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
template.setConnectionFactory(connectionFactory);
template.afterPropertiesSet();
return template;
}
}
以上是关于java RedisConfig的主要内容,如果未能解决你的问题,请参考以下文章
spring data redis的配置类RedisConfig
SpringBoot整合Redis,RedisConfig启动出错ror creating bean with name ‘redisTemplate‘ defined in class path