Redis的Spring客戶端使用小結(jié)
前面使用 Jedis 時(shí), 是借助 Jedis 對(duì)象中的各種方法來對(duì) Redis 進(jìn)行操作. 而在 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 對(duì)象.
@RestController
public class MyController {
@Autowired
StringRedisTemplate stringRedisTemplate;
}
[!NOTE]
這里的 RedisTemplate 將 Redis 中的命令, 又做了進(jìn)一步封裝, 分成了幾個(gè)類別 (每個(gè)類別操作特定的數(shù)據(jù)類型)
- opsForValue(): 專門操作 string 類型.
- opsForList(): 專門操作 list 類型.
- opsForSet(): 專門操作 set 類型.
- opsForHash(): 專門操作 hash 類型.
- opsForZSet(): 專門操作 zset 類型.
這樣一來, 它提供的一些接口風(fēng)格和原生的Redis命令就存在一定差異.
還有一點(diǎn)要注意的是: Spring 并沒有封裝 Redis 的所有命令 (如 flushAll 就沒有封裝), 此時(shí)我們可以使用 execute 方法來使用 Redis 的原始命令.
例如:
stringRedisTemplate.execute((RedisConnection connection) -> {
// execute 要求回調(diào)方法中必須寫 return 語句,返回個(gè)東西
// 這個(gè)回調(diào)返回的對(duì)象,就會(huì)作為 execute 本身的返回值
connection.flushAll();
return null;
});
//這里的RedisConnection對(duì)象, 就相當(dāng)于Jedis里的Jedis對(duì)象.
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";
}
}
- 請(qǐng)求結(jié)果:
postman:

日志:

2. 使用 list
@GetMapping("/testList")
public String testList() {
// 先清除之前的數(shù)據(jù)
tringRedisTemplate.execute((RedisConnection connection) -> {
// execute 要求回調(diào)方法中必須寫 return 語句,返回個(gè)東西
// 這個(gè)回調(diào)返回的對(duì)象,就會(huì)作為 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";
}
運(yùn)行結(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";
}
運(yùn)行結(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";
}
運(yùn)行結(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";
}
運(yùn)行結(jié)果:

到此這篇關(guān)于Redis的Spring客戶端使用小結(jié)的文章就介紹到這了,更多相關(guān)Redis Spring客戶端使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用JMeter插件Redis Data Set如何實(shí)現(xiàn)高性能數(shù)據(jù)驅(qū)動(dòng)測(cè)試
RedisDataSet插件是JMeter的一個(gè)插件,可以實(shí)現(xiàn)從Redis中動(dòng)態(tài)加載數(shù)據(jù),并將其用作測(cè)試參數(shù),本文詳細(xì)介紹如何在JMeter中使用RedisDataSet插件,幫助你實(shí)現(xiàn)高效的數(shù)據(jù)驅(qū)動(dòng)測(cè)試2025-01-01
Redis實(shí)現(xiàn)Session持久化的示例代碼
Redis是內(nèi)存數(shù)據(jù)庫,數(shù)據(jù)都是存儲(chǔ)在內(nèi)存中,為了避免服務(wù)器斷電等原因?qū)е翿edis進(jìn)程異常退出后數(shù)據(jù)的永久丟失,本文主要介紹了Redis實(shí)現(xiàn)Session持久化的示例代碼,感興趣的可以了解一下2023-09-09
Redis中pipeline(管道)的實(shí)現(xiàn)示例
Redis管道(Pipeline)技術(shù)是一種提高數(shù)據(jù)處理效率的機(jī)制,允許客戶端通過一次網(wǎng)絡(luò)往返(RTT)發(fā)送多個(gè)命令到服務(wù)端,并一次性接收所有響應(yīng),本文就來實(shí)現(xiàn)管道,感興趣的可以了解一下2024-10-10
Redis中秒殺場(chǎng)景下超時(shí)與超賣問題的解決方案
當(dāng)我們?cè)趌inux中使用ab來模擬高并發(fā)秒殺時(shí)可能會(huì)遇到兩種問題,“超時(shí)和超賣”,本文就詳細(xì)介紹了Redis中秒殺場(chǎng)景下超時(shí)與超賣問題的解決方案,感興趣的可以了解一下2022-05-05
redis requires ruby version2.2.2的解決方案
本文主要介紹了redis requires ruby version2.2.2的解決方案,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07

