1. Redis 之管道(pipeline)
執行一個Redis命令,Redis客戶端和Redis服務器就需要執行以下步驟:
- 客戶端發送命令到服務器;
- 服務器接受命令請求,執行命令,產生相應的結果;
- 服務器返回結果給客戶端;
- 客戶端接受命令的執行結果,并向用戶展示。
Redis命令所消耗的大部分時間都用在了發送命令請求和接收命令結果上面,把任意多條Redis命令請求打包在一起,然后一次性地將它們全部發送給服務器,而服務器則會把所有命令請求都處理完畢之后,一次性地將它們的執行結果全部返回給客戶端。
注意事項:
Redis服務器并不會限制客戶端在管道中包含的命令數量,但是卻會為客戶端的輸入緩沖區設置默認值為1GB的體積上限:當客戶端發送的數據量超過這一限制時,Redis服務器將強制關閉該客戶端。因此最好不要一下把大量命令或者一些體積非常龐大的命令放到同一個管道中執行。
除此之外,很多客戶端本身也帶有隱含的緩沖區大小限制,如果你在使用流水線特性的過程中,發現某些流水線命令沒有被執行,或者流水線返回的結果不完整,那么很可能就是你的程序觸碰到了客戶端內置的緩沖區大小限制。
2. SpringBoot 整合 Redis 管道實例
使用單個的 increment 命令,處理 200w個key:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class RedisPipelineStudy extends BaseTest { @Autowired private StringRedisTemplate stringRedisTemplate; private static final String PREFIX = "test0:" ; @Test public void test() { StopWatch stopWatch = new StopWatch(); stopWatch.start( "test0" ); for ( int times = 0 ; times < 2 ; times++) { for ( int i = 0 ; i < 1000000 ; i++) { stringRedisTemplate.opsForValue().increment(PREFIX + i, 1L); } } stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); } } |
耗時如下所示:是 12 位 ,單位ns
使用管道 incrBy 處理 200w個key,每次打包300條命令發送給服務器,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public class RedisPipelineStudy extends BaseTest { @Autowired private StringRedisTemplate stringRedisTemplate; private static final String PREFIX = "test1:" ; @Test public void test() { StopWatch stopWatch = new StopWatch(); stopWatch.start( "test1" ); List<Integer> recordList = new ArrayList<>(); for ( int times = 0 ; times < 2 ; times++) { for ( int i = 0 ; i < 1000000 ; i++) { try { recordList.add(i); if (recordList.size() > 300 ) { incrByPipeline(recordList); recordList = new ArrayList<>(); } } catch (Exception e) { System.out.println(e); } } if (!CollectionUtils.isEmpty(recordList)) { incrByPipeline(recordList); recordList = new ArrayList<>(); } } stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); } private void incrByPipeline(List<Integer> recordList) { stringRedisTemplate.executePipelined( new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { try { for (Integer record : recordList) { byte [] key = (PREFIX + record).getBytes(); connection.incrBy(key, 1 ); } } catch (Exception e) { System.out.println(e); } return null ; } }); } } |
耗用時間: 11 位 ,單位 :ns,是單個命令耗時的 1/6。
到此這篇關于SpringBoot整合Redis管道的示例代碼的文章就介紹到這了,更多相關SpringBoot整合Redis管道內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_38737992/article/details/118469059