Redis的事务操作的命令与执行操作教程-mysql教程-学派吧

本篇文章给大家带来的内容是关于Redis的事务操作的命令与执行操作(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

序1

本文主要研究一下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的事务操作的命令与执行操作(代码)的详细内容,更多请关注学派吧其它相关文章!

主题测试文章,只做测试使用。发布者:云大使,转转请注明出处:https://www.xp8.net/data/2047.html

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 2018年11月24日 下午10:33
下一篇 2018年11月24日 下午10:47

相关推荐

  • mysql数据库中影响性能因素(附数据库架构案例)-mysql教程-学派吧

    本篇文章给大家带来的内容是mysql数据库中影响性能因素的讲解(附数据库架构案例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 关于数据库性能的故事 面试时多多少少会讲到数据库上的事情,“你对数据库的掌握如何?”,什么时候最考验数据库的性能,答应主要方面上讲就是大数据量的读写时,而电商类的大促活动就是考验各自的数据库性能的时候啦。 对于w…

    数据库运维 2018年11月24日
    2.4K00
  • 在MySQL中的explain中的using where和using index-mysql教程-

    本篇文章的主要内容是关于在mysql数据库explain中的using where和using index的使用,感兴趣的朋友可以了解一下。 1. 查看表中的所有索引 show index from modify_passwd_log; 有两个 一个是id的主键索引 , 一个是email_id的普通索引 2. using index表示 使用到了索引 , 并…

    2019年4月24日
    4.2K00
  • SQL中datediff函数怎么用?(代码详解)-mysql教程-学派吧

    在SQL Server中,可以使用T-SQL DATEDIFF()函数返回两个日期之间的差异。它适用于任何可以解析为time、date、smalldatetime、datetime、datetime2或datetimeoffset值的表达式。因此,你也可以得到两次的差值。 本文提供了SQL Server中的DATEDIFF()函数的使用示例。 DATEDIF…

    2019年4月9日
    3.2K00
  • SqlServer2008数据库占用内存一直增长

    前言 最近碰到一个sqlserver数据库占用内存一直增长。直接到挂机 解决方案1 入Sql server 企业管理器(管理数据库和表的,这个都不知道就不用往下看了),在数据库服务器名称上点击【右键】,选择【属性】,然后,找到【内存】选项,在右边的【使用SWE分配内存】左边把对勾打上。在最大服务器内存(MB)上填入适当的大小(具体填多大,肯定不能超过计算机的…

    数据库运维 2018年9月14日
    3.9K00
  • MySQL数据库索引的内容介绍-mysql教程-学派吧

    本篇文章给大家带来的内容是关于MySQL数据库索引的内容介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 存储引擎是数据库的核心,常用的MySQL存储引擎有InnoDB,MyISAM,memory.索引是加速数据的查询的一种数据结构. 1. 索引简介 1.1 索引优点 查询数据块 数据唯一性 加速表之间的连接 1.2 索引缺点 索引会占用…

    数据库运维 2019年4月9日
    2.7K00

发表回复

登录后才能评论
联系我们

联系我们

18838889666

在线咨询: QQ交谈

邮件:xinyun@88.com

工作时间:周一至周五,9:30-18:30,节假日休息

添加微信
添加微信
分享本页
返回顶部