社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
Spring Boot 1.0 默认使用的是 Jedis 客户端,2.0 替换成 Lettuce
Lettuce 和 Jedis 的都是连接Redis Server的客户端程序。
Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。
Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
#redis配置 Lettuce 是一个可伸缩线程安全的 Redis 客户端,多个线程可以共享同一个 RedisConnection,它利用优秀 netty NIO 框架来高效地管理多个连接
spring.redis.host=192.168.0.172
spring.redis.port=6379
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=36000ms
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=-1ms
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0
spring-boot-starter-data-redis启动器已经为我们提供了两种Template,无需任何配置,直接使用即可。
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
RedisTemplate redisTemplate;
方法有:
opsForValue :对应 String(字符串)
opsForZSet:对应 ZSet(有序集合)
opsForHash:对应 Hash(哈希)
opsForList:对应 List(列表)
opsForSet:对应 Set(集合)
opsForGeo:对应 GEO(地理位置)
在实际使用中,我们大多不会直接使用RedisTemplate<Object,Object>,而是会对key,value进行序列化,所以我们还需要新增一个配置类
去RedisSerializer类下查看我们可用的序列化器。
接下来配置所需要的 ,一般使用:Jackson2JsonRedisSerializer
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
template.setConnectionFactory(factory);
return template;
}
}
@Autowired
RedisTemplate<String, Object> redisTemplate;
@RequestMapping(value = "/core/redis/set02", method = RequestMethod.GET)
public String redis03(@RequestParam Integer id) throws Exception {
User user = mapper.getOne(id);
redisTemplate.opsForValue().set("user", user);
return "ok";
}
** Redis Desktop Manager进行结果展示 **
没自定义序列化器Jackson2JsonRedisSerializer 之前的:
个人微信公众号:fbzl95
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!