stringRedisTemplate
SpringDataRedis中提供了RedisTemplate工具类,其中封装了各种对Redis的操作,并且将不同数据类型的操作api封装到了不同的类型中。
导入依赖:
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
String类型:
//注入StringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void testString() {
log.info("测试String类型:");
//插入一条String类型的数据
stringRedisTemplate.opsForValue().set("name3", "天才韦彦祖");
//获取一条String类型的数据
String name = stringRedisTemplate.opsForValue().get("name3");
System.out.println(name);
}
[root@VM-12-3-centos ~]# redis-cli --raw -a 1234 #要加--raw,不然中文会乱码
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get name
天才韦彦祖
存入对象:
加入ObjectMapper的相关依赖:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
实体类:
package com.starQeem.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private String name;
private Integer age;
}
测试:
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void testUser() throws IOException {
log.info("测试存入对象:");
//创建对象
User user = new User("天才韦彦祖韦彦祖真帅",18);
//手动序列化
String json = mapper.writeValueAsString(user);
//写入数据
stringRedisTemplate.opsForValue().set("user:300",json);
//获取数据
String s = stringRedisTemplate.opsForValue().get("user:300");
//手动反序列化
User user1 = mapper.readValue(s, User.class);
System.out.println("user:300 = "+user1);
}
127.0.0.1:6379> get user:300
{"name":"天才韦彦祖韦彦祖真帅","age":18}
127.0.0.1:6379>
其它类型:
//操作Hash类型
@Test
void testHash(){
log.info("操作Hash类型:");
//插入数据
stringRedisTemplate.opsForHash().put("dish","name","fish");
stringRedisTemplate.opsForHash().put("dish","price","30");
//获取数据
System.out.println(stringRedisTemplate.opsForHash().get("dish", "name"));
System.out.println(stringRedisTemplate.opsForHash().get("dish", "price"));
}
//操作List类型
@Test
void testList(){
log.info("操作list类型:");
//插入数据
stringRedisTemplate.opsForList().leftPushAll("score","88","99","100","99","88");
//获取数据
System.out.println(stringRedisTemplate.opsForList().range("score", 0, 4));
}
//操作set类型
@Test
void testSet(){
log.info("操作Set类型:");
//插入数据
stringRedisTemplate.opsForSet().add("t5","a","b","c","d");
//获取数据
System.out.println(stringRedisTemplate.opsForSet().members("t5"));
}
//操作sorted set类型
@Test
void testSortedSet(){
log.info("操作Sorted set类型:");
//插入数据
stringRedisTemplate.opsForZSet().add("English","张三",88);
stringRedisTemplate.opsForZSet().add("English","星星",100);
stringRedisTemplate.opsForZSet().add("English","Qeem",99);
//获取数据
System.out.println(stringRedisTemplate.opsForZSet().range("English", 0, 2));
}
评论区