欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Redis中事務(wù)操作的命令有哪些

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

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鳳臺免費(fèi)建站歡迎大家使用!

命令

multi與exec

  • 命令行

127.0.0.1:6379> multi
OK
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
"2"
127.0.0.1:6379> get len
"2"
  • lettuce實(shí)例

    @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"));
    }

部分執(zhí)行

  • 命令行

127.0.0.1:6379> multi
OK
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實(shí)例

    @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
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incr sum
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get sum
"1"
  • lettuce實(shí)例

    @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才可以執(zhí)行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遇到類型操作等錯(cuò)誤時(shí)不會滾,該成功執(zhí)行的命令還是成功執(zhí)行,該失敗的還是失敗

  • multi exec保證的是,只要exec命令有執(zhí)行成功,則事務(wù)中一系列的命令都能執(zhí)行,如果exec因?yàn)榫W(wǎng)絡(luò)等問題,server端沒有接收到,則事務(wù)中的一系列命令都不會被執(zhí)行

  • discard需要在有調(diào)用multi的前提下才能使用,該命令會清空事務(wù)隊(duì)列等待執(zhí)行的命令

  • redis提供watch指令,可以配合multi exec來使用,可以實(shí)現(xiàn)類似數(shù)據(jù)庫的樂觀鎖的機(jī)制,一旦watch的key被其他client有更新,則整個(gè)exec操作失敗

看完了這篇文章,相信你對“Redis中事務(wù)操作的命令有哪些”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文題目:Redis中事務(wù)操作的命令有哪些
標(biāo)題鏈接:http://aaarwkj.com/article22/jpogjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站收錄、Google靜態(tài)網(wǎng)站、ChatGPT、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
国产极品美女视频福利| 国产精品一区二区毛卡片| 日本免费熟女一区二区| 日韩在线中文字幕三区| 日韩一区二区三区高清免费视频成人| 丰满人妻被黑人猛烈进入免费| 精品人妻中文字幕在线| 中文字幕乱码人妻一区| 中文字幕乱码亚洲美女精品| 最新国产激情福利网站| 日韩视频在线不卡观看| 91蜜臀在线视频播放| 午夜视频在线观看91| 久久国产精品99亚洲| 亚洲精品一区久久狠狠欧美| 风韵丰满熟妇老熟女啪啪| 亚洲淫婷婷久久一区二区| 中文字幕中出亚洲精品| 人妻丝袜中文字幕在线| 国产一区在线视频无卡顿| 欧美中文字幕精在线不卡| 日日夜夜久久一二三区| 久久九特黄的免费大片| 青青草原精品视频在线| 精品人妻中文字幕在线| 一区二区三区午夜激情| 日本午夜在线观看视频| 国产在线精品专区第一页| 国产男生午夜福利网站| 国产日韩欧美另类综合| 夫妻性生活视频全过程| 黄色三级欧美一区二区| 日本成年网站在线观看| 久久国产精品午夜亚洲欧美| 激情内射日本一区二区三区| 国产系列在线播放一区二区三区| 国产丝袜在线福利观看| 就去吻色综合一二三四| 少妇二区三区精品视频| 尤物在线免费观看视频| 91黄色国产在线播放|