Spring Data是用于简化数据库访问的开源框架,包括:jdbc,jpa,elasticsearch,redis.......
Spring Data Redis是Spring Data旗下的一个子模块,用于简化redis操作。
其中提供了一个封装好的操作模板RedisTemplate,这里说一下RedisTemplate和Jedis的区别:
1.SpringBoot2.x后RedisTemplate默认底层lettuce,大并发下lettuce(底层为netty,是非阻塞性的nio)效率更高,而Jedis底层是阻塞性的IO,效率相对低
2.RedisTemplate可以设置通用序列化器,而Jedis不行
Spring Data Redis的启动器为:spring-boot-starter-data-redis
在application.yml配置文件中配置(集群模式):
spring:
redis:
cluster:
nodes:
- 192.168.116.134:7001
- 192.168.116.134:7002
- 192.168.116.134:7003
- 192.168.116.134:7004
- 192.168.116.134:7005
- 192.168.116.134:7006
jedis:
pool:
max-active: 20 #连接池最大连接数
max-idle: 10 #连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
非集群模式:
spring:
redis:
host: 192.168.116.134
port: 6379
jedis:
pool:
max-active: 20 #连接池最大连接数
max-idle: 10 #连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
创建配置类,配置通用序列化器
@Configuration
public class RedisConfig{
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//设置通用序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new
GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
创建启动类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
创建实体类:
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {RedisApp.class})
public class RedisTest {
//默认key-value的序列化器是JdkSerializationRedisSerializer
@Autowired
private RedisTemplate redisTemplate;
/**
* 1、StringRedisSerializer:key或value是字符串时使用
*/
@Test
public void testSetStr(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set("str", "张三丰");
}
@Test
public void testGetStr(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
String str = (String) redisTemplate.opsForValue().get("str");
System.out.println(str);
}
/**
* 2、JdkSerializationRedisSerializer:pojo<----->字节序列,大小243,乱码
*/
@Test
public void testSetPojo(){
User user = new User();
user.setId(1);
user.setName("张三丰");
user.setAge(140);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.opsForValue().set("user", user);
}
@Test
public void testGetPojo(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
User user = (User) redisTemplate.opsForValue().get("user");
System.out.println(user);
}
/**
* GenericJackson2JsonRedisSerializer:pojo<------>json,大小74,不乱码
*/
@Test
public void testSetPojo2(){
User user = new User();
user.setId(1);
user.setName("张三丰");
user.setAge(140);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.opsForValue().set("user2", user);
}
@Test
public void testGetPojo2(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
User user2 = (User) redisTemplate.opsForValue().get("user2");
System.out.println(user2);
}
/**
* GenericJackson2JsonRedisSerializer处理String
*/
@Test
public void testSetStr2(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.opsForValue().set("str2", "张三丰");
}
@Test
public void testGetStr2(){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
String str2 = (String) redisTemplate.opsForValue().get("str2");
System.out.println(str2);
}
/**
* 使用通用的序列化器
*/
@Test
public void testSetPojo3(){
User user = new User();
user.setId(1);
user.setName("张三丰");
user.setAge(140);
redisTemplate.opsForValue().set("user3", user);
}
@Test
public void testGetPojo3(){
User user3 = (User) redisTemplate.opsForValue().get("user3");
System.out.println(user3);
}
}