第一步,需要在添加依赖的时候,选中这两个依赖。
第二步,修改配置文件
此处可以用properties也可以用yml。
第三步,完善controller
之前使用jedis,是通过jedis对象里面的各种方法来操作redis的,此处Spring中则是通过StringRedisTemplate来操作redis。
此处的RedisTemplate是把这些操作Redis的方法,分成了几个类别,做了进一步的封装。
比如此处的opsForValue就可以获取到对象,操作字符串类型。
但是,redisTemplate,并不是什么方法都有的,比如我们想要删库,flushAll操作就没有,如果我们想要完成一些原生redis才有的操作,怎么办呢?
RedisTemplate留了一个后手,让我们随时能够执行到原生Redis的命令:execute方法
能够直接在Redis连接上执行命令,这在需要执行复杂或自定义的Redis操作时非常有用。
String List Hash Set Zset
五种类型,大同小异,用法具体来说可以通过官方文档查询。
package com.example.springredisdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
@RestController
public class MyController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/testString")
@ResponseBody
public String testString(){
redisTemplate.opsForValue().set("key1", "111");
redisTemplate.opsForValue().set("key2", "222");
redisTemplate.opsForValue().set("key3", "333");
String value = redisTemplate.opsForValue().get("key");
System.out.println("value: "+ value);
return "OK";
}
@GetMapping("/testList")
@ResponseBody
public String testList(){
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForList().leftPush("key", "111");
redisTemplate.opsForList().leftPush("key2", "222");
redisTemplate.opsForList().leftPush("key3", "333");
String value;
value = redisTemplate.opsForList().rightPop("key");
System.out.println("value: "+ value);
value = redisTemplate.opsForList().rightPop("key2");
System.out.println("value: "+ value);
value = redisTemplate.opsForList().rightPop("key3");
System.out.println("value: "+ value);
return "List";
}
@GetMapping("/testSet")
@ResponseBody
public String testSet() {
redisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
redisTemplate.opsForSet().add("key", "111", "222", "333");
Set<String> result = redisTemplate.opsForSet().members("key");
System.out.println("result: " + result);
Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
System.out.println("exists: " + exists);
Long count = redisTemplate.opsForSet().size("key");
System.out.println("count: " + count);
redisTemplate.opsForSet().remove("key", "111", "222");
result = redisTemplate.opsForSet().members("key");
System.out.println("result: " + result);
return "OK";
}
@GetMapping("/testHash")
@ResponseBody
public String 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");
String value = (String) redisTemplate.opsForHash().get("key", "f1");
System.out.println("value: " + value);
Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
System.out.println("exists: " + exists);
redisTemplate.opsForHash().delete("key", "f1", "f2");
Long size = redisTemplate.opsForHash().size("key");
System.out.println("size: " + size);
return "OK";
}
@GetMapping("/testZSet")
@ResponseBody
public String 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: " + members);
Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
System.out.println("membersWithScore: " + membersWithScore);
Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
System.out.println("score: " + score);
redisTemplate.opsForZSet().remove("key", "zhangsan");
Long size = redisTemplate.opsForZSet().size("key");
System.out.println("size: " + size);
Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
System.out.println("rank: " + rank);
return "OK";
}
}