旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

Redis的事件操纵的敕令与实行操纵(代码)【MySQL教程】,redis

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:66评论:0


导读:本篇文章给人人带来的内容是关于Redis的事件操纵的敕令与实行操纵(代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。序本文重要研究一下redis的...
本篇文章给人人带来的内容是关于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的事件操纵的敕令与实行操纵(代码)的细致内容,更多请关注ki4网别的相干文章!

标签:redis


欢迎 发表评论: