久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Redis中事務操作的命令有哪些

156次閱讀
沒有評論

共計 3651 個字符,預計需要花費 10 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

丸趣 TV 小編給大家分享一下 Redis 中事務操作的命令有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

命令

multi 與 exec

命令行

127.0.0.1:6379  multi
127.0.0.1:6379  incr total
QUEUED
127.0.0.1:6379  incr len
QUEUED
127.0.0.1:6379  exec
1) (integer) 2
2) (integer) 2
127.0.0.1:6379  get total
127.0.0.1:6379  get len
 2

lettuce 實例

 @Test
 public void testMultiExec(){
 RedisClient client = RedisClient.create( redis://192.168.99.100:6379/0 
 StatefulRedisConnection String, String  connection = client.connect();
 RedisCommands String, String  syncCommands = connection.sync();
 syncCommands.set( hello , 1 
 syncCommands.set( world , 2 
 syncCommands.multi();
 syncCommands.incr( hello 
 syncCommands.incr( world 
 //DefaultTransactionResult[wasRolledBack=false,result=[1, 2, 1, 3, 1]]
 TransactionResult transactionResult = syncCommands.exec();
 System.out.println(transactionResult);
 System.out.println(syncCommands.get( hello));
 System.out.println(syncCommands.get( world));
 }

部分執行

命令行

127.0.0.1:6379  multi
127.0.0.1:6379  set a hello
QUEUED
127.0.0.1:6379  set b world
QUEUED
127.0.0.1:6379  incr a
QUEUED
127.0.0.1:6379  set c part
QUEUED
127.0.0.1:6379  exec
1) OK
2) OK
3) (error) ERR value is not an integer or out of range
4) OK
127.0.0.1:6379  get a
 hello 
127.0.0.1:6379  get b
 world 
127.0.0.1:6379  get c
 part

lettuce 實例

 @Test
 public void testMultiExecError(){
 RedisClient client = RedisClient.create( redis://192.168.99.100:6379/0 
 StatefulRedisConnection String, String  connection = client.connect();
 RedisCommands String, String  syncCommands = connection.sync();
 syncCommands.multi();
 syncCommands.set( a , hello 
 syncCommands.set( b , world 
 syncCommands.incr( a 
 syncCommands.set( c , part 
 //DefaultTransactionResult[wasRolledBack=false,result=[OK, OK, io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of range, OK, 1]]
 TransactionResult transactionResult = syncCommands.exec();
 System.out.println(transactionResult);
 System.out.println(syncCommands.get( a));
 System.out.println(syncCommands.get( b));
 System.out.println(syncCommands.get( c));
 }

multi 與 discard

命令行

127.0.0.1:6379  set sum 1
127.0.0.1:6379  multi
127.0.0.1:6379  incr sum
QUEUED
127.0.0.1:6379  discard
127.0.0.1:6379  get sum
 1

lettuce 實例

 @Test
 public void testMultiDiscard(){
 RedisClient client = RedisClient.create( redis://192.168.99.100:6379/0 
 StatefulRedisConnection String, String  connection = client.connect();
 RedisCommands String, String  syncCommands = connection.sync();
 syncCommands.incr( key1 
 syncCommands.multi();
 syncCommands.incr( key1 
 // 需要有 multi 才可以執行 discard,成功返回 OK
 String result = syncCommands.discard();
 System.out.println(result);
 System.out.println(syncCommands.get( key1));
 }

check and set

 @Test
 public void testWatch(){
 RedisClient client = RedisClient.create( redis://192.168.99.100:6379/0 
 StatefulRedisConnection String, String  connection = client.connect();
 RedisCommands String, String  syncCommands = connection.sync();
 String key =  key 
 syncCommands.watch(key);
 //another connection
 StatefulRedisConnection String, String  conn2 = client.connect();
 RedisCommands String, String  syncCommands2 = conn2.sync();
 syncCommands2.set(key,  a 
 syncCommands.multi();
 syncCommands.append(key,  b 
 //DefaultTransactionResult [wasRolledBack=true, responses=0]
 TransactionResult transactionResult = syncCommands.exec();
 System.out.println(transactionResult);
 System.out.println(syncCommands.get(key));
 }

reids 提供 multi exec/discard 指令,類似 open commit/rollback transaction,不過 exec 遇到類型操作等錯誤時不會滾,該成功執行的命令還是成功執行,該失敗的還是失敗

multi exec 保證的是,只要 exec 命令有執行成功,則事務中一系列的命令都能執行,如果 exec 因為網絡等問題,server 端沒有接收到,則事務中的一系列命令都不會被執行

discard 需要在有調用 multi 的前提下才能使用,該命令會清空事務隊列等待執行的命令

redis 提供 watch 指令,可以配合 multi exec 來使用,可以實現類似數據庫的樂觀鎖的機制,一旦 watch 的 key 被其他 client 有更新,則整個 exec 操作失敗

看完了這篇文章,相信你對“Redis 中事務操作的命令有哪些”有了一定的了解,如果想了解更多相關知識,歡迎關注丸趣 TV 行業資訊頻道,感謝各位的閱讀!

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-16發表,共計3651字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 汪清县| 安宁市| 琼海市| 武清区| 安泽县| 济宁市| 任丘市| 南漳县| 烟台市| 尉氏县| 崇义县| 额尔古纳市| 绍兴县| 山西省| 冀州市| 南宁市| 太仆寺旗| 天峻县| 温州市| 南川市| 扎赉特旗| 嵊泗县| 大渡口区| 抚顺市| 新密市| 莒南县| 民权县| 罗田县| 霍山县| 吴川市| 泉州市| 海宁市| 临江市| 梁河县| 西畴县| 武冈市| 滨海县| 陇南市| 盐津县| 修水县| 疏勒县|