目录
一、前置工作
1.1、引入 Spring Data Redis 依赖
1.2、编写配置文件
二、Spring Data Redis
2.1、前置知识
2.2、演示 Demo
spring:
redis:
host: 127.0.0.1
port: 8888
之前使用 Jedis 是通过 Jedis 对象里的各种方法来操作 redis 的,此处 Spring 则是通过 SpringRedisTemplate 来操作 redis.
最原始的提供类是 RedisTemplate ,SpringRedisTemplate 是 RedisTemplate 的子类,专门用来处理文本数据的,相比于 Jedis ,还是存在较大差异的,如下:
RedisTemplate 是把 Redis 的方法,分成了几个类别来进行组织,和原生的 Redis 命令有一定的差异~ 作者的初心是希望通过上述封装,让接口看起来更简单,但在我看来,并没有达成,反而因为与 Redis 原生命令差异,提高了使用者的学习成本.
值得注意的是, RedisTemplate 留了一个后手,让我们能随时执行到 Redis 原生的命令 ——execute方法:
这里的 RedisConnection 就代表了 Redis 连接,对标 Jedis 对象
Ps:execute 要求回调方法中必须写 return 语句,这个回调的对象,就会作为 execute 本身的返回值.
这里 redis 测试的各种方法,都是通过 Controller 提供的 http 接口来触发的.
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("/string")
public String testString() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForValue().set("key1", "111");
redisTemplate.opsForValue().set("key2", "222");
redisTemplate.opsForValue().set("key3", "333");
String value1 = redisTemplate.opsForValue().get("key2");
String value2 = redisTemplate.opsForValue().get("key100");
System.out.println("--------------------------------");
System.out.println("value1: " + value1);
System.out.println("value2: " + value2);
return "OK";
}
@RequestMapping("/hash")
public String testHash() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForHash().put("key1", "f1", "v1");
redisTemplate.opsForHash().put("key1", "f2", "v2");
redisTemplate.opsForHash().put("key1", "f3", "v3");
String value1 = (String) redisTemplate.opsForHash().get("key1", "f1");
String value2 = (String) redisTemplate.opsForHash().get("key1", "f2");
System.out.println("--------------------------------");
System.out.println("value1: " + value1);
System.out.println("value2: " + value2);
redisTemplate.opsForHash().delete("key1", "f1", "f2");
Long size = redisTemplate.opsForHash().size("key1");
System.out.println("size: " + size);
return "OK";
}
@RequestMapping("/list")
public String testList() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForList().leftPush("key1", "111");
redisTemplate.opsForList().leftPush("key1", "222");
redisTemplate.opsForList().leftPush("key1", "333");
String value1 = redisTemplate.opsForList().rightPop("key1");
String value2 = redisTemplate.opsForList().rightPop("key1");
String value3 = redisTemplate.opsForList().rightPop("key1");
System.out.println("--------------------------------");
System.out.println("value1: " + value1);
System.out.println("value2: " + value2);
System.out.println("value3: " + value3);
return "OK";
}
@RequestMapping("/set")
public String testSet() {
System.out.println("--------------------------------");
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForSet().add("key1", "111", "222", "333");
Set result = redisTemplate.opsForSet().members("key1");
System.out.println("result: " + result);
Boolean exists = redisTemplate.opsForSet().isMember("key1", "111");
System.out.println("exists: " + exists);
Long count = redisTemplate.opsForSet().size("key1");
System.out.println("count: " + count);
redisTemplate.opsForSet().remove("key", "111", "222");
result = redisTemplate.opsForSet().members("key1");
System.out.println("result: " + result);
return "OK";
}
@RequestMapping("/zset")
public String testZset() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForZSet().add("key1", "f1", 100);
redisTemplate.opsForZSet().add("key1", "f2", 200);
redisTemplate.opsForZSet().add("key1", "f3", 300);
Set members = redisTemplate.opsForZSet().range("key1", 0 ,-1);
Set> membersWithScore = redisTemplate.opsForZSet().rangeByScoreWithScores("key", 0 , -1);
Double score = redisTemplate.opsForZSet().score("key1", "f1");
Long size = redisTemplate.opsForZSet().remove("key1", "f1");
Long rank = redisTemplate.opsForZSet().rank("key1", "f2");
System.out.println("--------------------------------");
System.out.println("members: " + members);
System.out.println("membersWithScores: " + membersWithScore);
System.out.println("score: " + score);
System.out.println("size: " + size);
System.out.println("rank: " + rank);
return "OK";
}
}