久久精品综合-欧美精品久久久久久-www一区二区-韩国三级中文字幕hd久久精品-国产一区二区三区在线-日韩免费视频

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

Redis的事務設置的命令與執行設置(代碼)

[摘要]本篇文章給大家帶來的內容是關于Redis的事務操作的命令與執行操作(代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。序本文主要研究一下redis的事務操作命令multi與exe...
本篇文章給大家帶來的內容是關于Redis的事務操作的命令與執行操作(代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

本文主要研究一下redis的事務操作

命令

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實例

    @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
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實例

    @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實例

    @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的事務操作的命令與執行操作(代碼)的詳細內容,更多請關注php中文網其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。




主站蜘蛛池模板: 青娱极品盛宴国产精品分类 | 天天性综合 | 日韩免费福利视频 | 色久视频 | 最新网址在线观看 | 色小说亚洲 | 午夜老司机在线观看免费 | 香蕉人人超人人超免费看视频 | 亚洲精品中文字幕无乱码麻豆 | 日韩欧美在线免费观看 | 一本久道久久综合多人 | 日本高清在线一区二区三区 | 欧美亚洲日本一区 | 青青青草视频在线 | 天天综合天天添夜夜添狠狠添 | 在线观看 一区二区 麻豆 | 亚洲成人免费网站 | 亚洲一区在线免费 | 日韩国产欧美在线观看 | 日韩伦理在线视频 | 亚洲欧美成人 | 日本高清在线视频www色下载 | 四只虎免费永久观看地址 | 奇米综合 | 色妹子在线 | 婷婷在线成人免费观看搜索 | 亚洲欧美日韩在线播放 | 日韩精品在线观看免费 | 日日操日日干 | 在线观看黄免费 | 日韩欧美h| 欧美综合图片区 | 婷婷中文 | 天天干天天色天天干 | 亚洲免费观看网站 | 亚洲成人h | 亚洲欧美激情综合第一区 | 欧美一级一片 | 在线视频中文字幕 | 中文字幕精品波多野结衣 | 日本人成免费大片 |