Redis的Spring客戶端使用小結(jié)
前面使用 Jedis 時, 是借助 Jedis 對象中的各種方法來對 Redis 進行操作. 而在 Spring 框架中, 則是通過 StringRedisTemplate 來操作 Redis. 最開始提供的類是 RedisTemplate, StringRedisTemplate 是 RedisTemplate 的子類, 專門用于處理文本數(shù)據(jù).
0. 配置 Spring 的 Redis環(huán)境
(1) 引入 Redis 的 Spring 依賴
選中 NoSQL 中的 Spring Data Redis (Access+Driver) 依賴.

(2) 寫 Spring 配置文件
application.yml:
spring:
data:
redis:
host: 127.0.0.1
port: 8888
(3) 創(chuàng)建 Controller 類, 并注入 StringRedisTemplate 對象.
@RestController
public class MyController {
@Autowired
StringRedisTemplate stringRedisTemplate;
}
[!NOTE]
這里的 RedisTemplate 將 Redis 中的命令, 又做了進一步封裝, 分成了幾個類別 (每個類別操作特定的數(shù)據(jù)類型)
- opsForValue(): 專門操作 string 類型.
- opsForList(): 專門操作 list 類型.
- opsForSet(): 專門操作 set 類型.
- opsForHash(): 專門操作 hash 類型.
- opsForZSet(): 專門操作 zset 類型.
這樣一來, 它提供的一些接口風(fēng)格和原生的Redis命令就存在一定差異.
還有一點要注意的是: Spring 并沒有封裝 Redis 的所有命令 (如 flushAll 就沒有封裝), 此時我們可以使用 execute 方法來使用 Redis 的原始命令.
例如:
stringRedisTemplate.execute((RedisConnection connection) -> {
// execute 要求回調(diào)方法中必須寫 return 語句,返回個東西
// 這個回調(diào)返回的對象,就會作為 execute 本身的返回值
connection.flushAll();
return null;
});
//這里的RedisConnection對象, 就相當于Jedis里的Jedis對象.
1.使用 string
@RestController
public class MyController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/testString")
public String testString() {
stringRedisTemplate.opsForValue().set("key", "111");
stringRedisTemplate.opsForValue().set("key2", "222");
stringRedisTemplate.opsForValue().set("key3", "333");
String value = stringRedisTemplate.opsForValue().get("key");
System.out.println("value: " + value);
return "OK";
}
}
- 請求結(jié)果:
postman:

日志:

2. 使用 list
@GetMapping("/testList")
public String testList() {
// 先清除之前的數(shù)據(jù)
tringRedisTemplate.execute((RedisConnection connection) -> {
// execute 要求回調(diào)方法中必須寫 return 語句,返回個東西
// 這個回調(diào)返回的對象,就會作為 execute 本身的返回值
connection.flushAll();
return null;
});
stringRedisTemplate.opsForList().leftPush("key", "111");
stringRedisTemplate.opsForList().leftPush("key", "222");
stringRedisTemplate.opsForList().leftPush("key", "333");
String value = stringRedisTemplate.opsForList().rightPop("key");
System.out.println("value: " + value);
value = stringRedisTemplate.opsForList().rightPop("key");
System.out.println("value: " + value);
value = stringRedisTemplate.opsForList().rightPop("key");
System.out.println("value: " + value);
return "OK";
}
運行結(jié)果:

3. 使用 set
@GetMapping("/testSet")
public String testSet() {
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
stringRedisTemplate.opsForSet().add("key", "111", "222", "333");
Set<String> result = stringRedisTemplate.opsForSet().members("key");
System.out.println("result: " + result);
Boolean exists = stringRedisTemplate.opsForSet().isMember("key", "111");
System.out.println("exists: " + exists);
Long count = stringRedisTemplate.opsForSet().size("key");
System.out.println("count: " + count);
stringRedisTemplate.opsForSet().remove("key", "111", "222");
result = stringRedisTemplate.opsForSet().members("key");
System.out.println("result: " + result);
return "OK";
}
運行結(jié)果:

4. 使用 Hash
@GetMapping("/testHash")
public String testHash() {
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
stringRedisTemplate.opsForHash().put("key", "f1", "111");
stringRedisTemplate.opsForHash().put("key", "f2", "222");
stringRedisTemplate.opsForHash().put("key", "f3", "333");
String value = (String) stringRedisTemplate.opsForHash().get("key", "f1");
System.out.println("value: " + value);
Boolean exists = stringRedisTemplate.opsForHash().hasKey("key", "f1");
System.out.println("exists: " + exists);
stringRedisTemplate.opsForHash().delete("key", "f1", "f2");
Long size = stringRedisTemplate.opsForHash().size("key");
System.out.println("size: " + size);
return "OK";
}
運行結(jié)果:

5. 使用 zset
@GetMapping("/testZSet")
public String testZSet() {
stringRedisTemplate.execute((RedisConnection connection) -> {
connection.flushAll();
return null;
});
stringRedisTemplate.opsForZSet().add("key", "zhangsan", 10D);
stringRedisTemplate.opsForZSet().add("key", "lisi", 20D);
stringRedisTemplate.opsForZSet().add("key", "wangwu", 30D);
Set<String> members = stringRedisTemplate.opsForZSet().range("key", 0, -1);
System.out.println("members: " + members);
Set<ZSetOperations.TypedTuple<String>> membersWithScore = stringRedisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
System.out.println("membersWithScore: " + membersWithScore);
Double score = stringRedisTemplate.opsForZSet().score("key", "zhangsan");
System.out.println("score: " + score);
stringRedisTemplate.opsForZSet().remove("key", "zhangsan");
Long size = stringRedisTemplate.opsForZSet().size("key");
System.out.println("size: " + size);
Long rank = stringRedisTemplate.opsForZSet().rank("key", "lisi");
System.out.println("rank: " + rank);
return "OK";
}
運行結(jié)果:

到此這篇關(guān)于Redis的Spring客戶端使用小結(jié)的文章就介紹到這了,更多相關(guān)Redis Spring客戶端使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis整合SpringBoot的RedisTemplate實現(xiàn)類(實例詳解)
這篇文章主要介紹了Redis整合SpringBoot的RedisTemplate實現(xiàn)類,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

