首頁  >  文章  >  資料庫  >  Redis的事務操作的命令與執行操作(代碼)

Redis的事務操作的命令與執行操作(代碼)

不言
不言原創
2018-09-19 16:06:012318瀏覽

這篇文章帶給大家的內容是關於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中文網其他相關文章!

    陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn