SpringBoot整合Redis管道的示例代碼
執(zhí)行一個(gè)Redis命令,Redis客戶(hù)端和Redis服務(wù)器就需要執(zhí)行以下步驟:
客戶(hù)端發(fā)送命令到服務(wù)器; 服務(wù)器接受命令請(qǐng)求,執(zhí)行命令,產(chǎn)生相應(yīng)的結(jié)果; 服務(wù)器返回結(jié)果給客戶(hù)端; 客戶(hù)端接受命令的執(zhí)行結(jié)果,并向用戶(hù)展示。Redis命令所消耗的大部分時(shí)間都用在了發(fā)送命令請(qǐng)求和接收命令結(jié)果上面,把任意多條Redis命令請(qǐng)求打包在一起,然后一次性地將它們?nèi)堪l(fā)送給服務(wù)器,而服務(wù)器則會(huì)把所有命令請(qǐng)求都處理完畢之后,一次性地將它們的執(zhí)行結(jié)果全部返回給客戶(hù)端。
注意事項(xiàng):
Redis服務(wù)器并不會(huì)限制客戶(hù)端在管道中包含的命令數(shù)量,但是卻會(huì)為客戶(hù)端的輸入緩沖區(qū)設(shè)置默認(rèn)值為1GB的體積上限:當(dāng)客戶(hù)端發(fā)送的數(shù)據(jù)量超過(guò)這一限制時(shí),Redis服務(wù)器將強(qiáng)制關(guān)閉該客戶(hù)端。因此最好不要一下把大量命令或者一些體積非常龐大的命令放到同一個(gè)管道中執(zhí)行。
除此之外,很多客戶(hù)端本身也帶有隱含的緩沖區(qū)大小限制,如果你在使用流水線(xiàn)特性的過(guò)程中,發(fā)現(xiàn)某些流水線(xiàn)命令沒(méi)有被執(zhí)行,或者流水線(xiàn)返回的結(jié)果不完整,那么很可能就是你的程序觸碰到了客戶(hù)端內(nèi)置的緩沖區(qū)大小限制。
2. SpringBoot 整合 Redis 管道實(shí)例SpringBoot 整合 redis 的實(shí)例
使用單個(gè)的 increment 命令,處理 200w個(gè)key:
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()); }}
耗時(shí)如下所示:是 12 位 ,單位ns
使用管道 incrBy 處理 200w個(gè)key,每次打包300條命令發(fā)送給服務(wù)器,如下所示:
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; }}); }}
耗用時(shí)間: 11 位 ,單位 :ns,是單個(gè)命令耗時(shí)的 1/6。
到此這篇關(guān)于SpringBoot整合Redis管道的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot整合Redis管道內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法2. jsp文件下載功能實(shí)現(xiàn)代碼3. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理6. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令7. phpstudy apache開(kāi)啟ssi使用詳解8. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器9. 詳解瀏覽器的緩存機(jī)制10. 如何在jsp界面中插入圖片
