SpringBoot整合
在SpringBoot2.x之后,原来使用的Jedis被替换成了Lettuce
Jedis:采用的直连,多个线程操作,是不安全的,如果想要避免需要使用Jedis pool连接池!更像 BIO
Lettuce:采用entty,实例可以在多个线程中进行共享,不存在线程不安全的情况,可以减少线程数据,更像 NIO
源码分析:
RedisAutoConfiguration.java
@Bean
@ConditionalOnMissingBean(name = "redisTemplate") //可以自定义redisTemplate来替换默认的
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
//默认的RedisTemplate没有过的设置,redis对象都是需要序列化的
//两个泛型都是<Object, Object>的类型,使用的时候要强制转化<String, Object>
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String 是Redis中常用的类型,所以单独设置一个Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
整合测试
-
导入依赖
<!--Redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置连接
相关配置可以在
RedisProperties
类中查看# 配置Redis spring.redis.host=127.0.0.1 spring.redis.port=6379
-
连接测试
@SpringBootTest class Redis02SpringbootApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads() { /* * redisTemplate 操作不同的数据类型,api和指令是一样的 * opsForValue 操作字符串,类似String * opsForList 操作list,类型list * opsForSet * opsForHash * opsForZSet * opsForGeo * opsForHyperLogLog * * 常用方法可以直接通过redisTemplate操作。比如:事务、CRUD */ //获取Redis连接对象 RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); System.out.println(connection.ping()); connection.flushDb(); //redisTemplate.opsForValue().set("name","Beloved"); redisTemplate.opsForValue().set("name","张三"); System.out.println(redisTemplate.opsForValue().get("name")); } }
关于序列化
Redis存入所以的对象都要序列化,否则会产生中文乱码
编写RedisTemplate
//编写RedisTemplate
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
//一般使用<String, Object>类型
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
//Json序列化配置
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);
//String序列化配置
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash的key采用String的序列化
template.setHashKeySerializer(stringRedisSerializer);
//value采用jackson序列化方式
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value采用jackson序列化方式
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
测试