Spring Data Redis
一、spring data redis的介绍?
1、什么是spring data?
spring data是用于简化数据库访问的开源框架,主要包括:redis、jdbc、elasticsearch
2、什么是spring data redis?
spring data redis是spring data下的一个子模块,作用:简化redis的操作
3、为什么用redisTemplate而不用jedis?
springboot 2.x后redisTemplate默认底层是lettuce(nio),大并发下lettuce效率更高
redisTemplate可以设置通用序列化器
4、spring data redis的启动器
spring-boot-starter-data-redis
二、spring data redis入门
1、pom.xml
spring-boot-starter-web
spring-boot-starter-data-redis
spring-boot-starter-test
2、application.yml
spring:
redis:
cluster:
nodes:
- 192.168.204.132:7001
- 192.168.204.132:7002
- 192.168.204.132:7003
- 192.168.204.132:7004
- 192.168.204.132:7005
- 192.168.204.132:7006
jedis:
pool:
max-active: 20 #连接池最大连接数
max-idle: 10 #连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
3、config
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置通用序列化器...
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
4、util
@Component
public class RedisClient {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
... ...
}