导入对应版本的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
修改配置文件中的信息
spring:
redis:
host: 127.0.0.1
port: 8888
注意: 我这里 xshell 中将 redis 的端口映射到了本机的 8888 端口, 你们映射了啥, 这里的 port 就写啥
写代码
SpringBoot 是通过 redisTemplate 对象
对 Redis 进行操作
redisTemplate
对 Redis 中的每个类型进行了封装, 使用对应类型的对应方法要先选择操作类型
String 类型
@Autowired
privateStringRedisTemplate redisTemplate;
@RequestMapping("/testString")
public void testString() {
redisTemplate.opsForValue(). set("key1", "value1");
redisTemplate.opsForValue(). set("key2", "value2");
redisTemplate.opsForValue(). set("key3", "value3");
System.out.println(redisTemplate.opsForValue().get("key1"));
System.out.println(redisTemplate.opsForValue().get("key2"));
System.out.println(redisTemplate.opsForValue().get("key3"));
}
List 类型
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("/testList")
public void testList() {
//删库操作
redisTemplate.execute((RedisConnection connection) -> {
// execute 要求我们的回调方法中, 必须返回一个内容
// 该回调返回的对象, 就是 execute 返回的内容
connection.flushAll();
return null;
});
redisTemplate.opsForList().leftPush("key", "val");
redisTemplate.opsForList().leftPush("key", "val2");
redisTemplate.opsForList().leftPush("key", "val3");
System.out.println(redisTemplate.opsForList().rightPop("key"));
System.out.println(redisTemplate.opsForList().rightPop("key"));
System.out.println(redisTemplate.opsForList().rightPop("key"));
}
Set 类型
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("testSet")
public void testSet() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForSet().add("key", "111", "222","333");
System.out.println(redisTemplate.opsForSet().members("key"));
System.out.println(redisTemplate.opsForSet().isMember("key", "111"));
System.out.println(redisTemplate.opsForSet().isMember("key", "444"));
System.out.println(redisTemplate.opsForSet().size("key"));
redisTemplate.opsForSet().remove("key", "111","222");
System.out.println(redisTemplate.opsForSet().members("key"));
}
Hash 类型
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("/testHash")
public void testHash() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForHash().put("key", "f1", "111");
redisTemplate.opsForHash().put("key", "f2", "222");
redisTemplate.opsForHash().put("key", "f3", "333");
System.out.println((String) redisTemplate.opsForHash().get("key", "f1"));
System.out.println((String) redisTemplate.opsForHash().get("key", "f2"));
System.out.println((String) redisTemplate.opsForHash().get("key", "f3"));
System.out.println(redisTemplate.opsForHash().hasKey("key", "f1"));
redisTemplate.opsForHash().delete("key", "f1");
redisTemplate.opsForHash().delete("key", "f2");
System.out.println(redisTemplate.opsForHash().size("key"));
}
ZSet 类型
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("/testZSet")
public void testZSet() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForZSet().add("key", "zhangsan", 10);
redisTemplate.opsForZSet().add("key", "lisi", 20);
redisTemplate.opsForZSet().add("key", "wangwu", 30);
Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
System.out.println(members);
Set<ZSetOperations.TypedTuple<String>> membersWithScore =
redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
System.out.println(membersWithScore);
System.out.println(redisTemplate.opsForZSet().score("key", "zhangsan"));
redisTemplate.opsForZSet().remove("key", "zhangsan");
System.out.println(redisTemplate.opsForZSet().size("key"));
System.out.println(redisTemplate.opsForZSet().rank("key", "lisi"));
}