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教程】 数据库隔离的四个级别分别为: Read Uncommitted(读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果。本隔离级别很少用于实际应用,因为它的性能也不比其他级别好多少。读取未…

    2019年4月9日
    3.5K00
  • 如何在Mac上安装MySQL?-mysql教程-学派吧

    Oracle的MySQL是一个流行的基于结构化查询语言(SQL)的开源关系数据库管理系统。它经常与PHP一起使用,以增强网站的功能。PHP预装在Mac电脑上,而MySQL没有。 当您创建和测试需要MySQL数据库的软件或网站时,在您的计算机上安装MySQL非常方便。在Mac上安装MySQL比您预期的要容易,特别是如果您使用本机安装包而不是TAR包,这需要在终…

    数据库运维 2019年4月9日
    2.4K00
  • 修改mysql数据库导入sql文件大小限制

    1、打开PHP.ini。找到 upload_max_filesize memory_limit post_max_size 这三个参数! (在默认的情况下,php只允许最大的上传数据为2M,也就是2048KB,而极限的最大使用内存memory_limit也仅为128M,Post的最大也为2M) 2、按您的服务器的实际性能配置进行如下改动:(注意:以下是按我的…

    数据库运维 2018年7月12日
    3.9K00
  • mysql数据库无法启动-进行数据备份搬迁的教程

    今天看到一个案例:数据库服务无法启动,也导致了不能备份数据库、所以只能使用了笨方法、直接打包DATA目录 我们的服务器是宝塔环境、  直接备份 /www/server/data 这个目录备份 下载下来  在新的服务器上安装好环境、把data压缩包上传上去、进行解压覆盖、注意的是 版本最好一样。 重启一下数据库、 [root@VM_166_22_centos …

    2019年4月27日
    5.7K00
  • Redis与Memcached有何区别 ?redis和Memcached的区别比较

    本篇文章给大家带来的内容是关于Redis与Memcached有何区别 ?redis和Memcached的区别比较,有一定的参考价值,有需要的朋友可以参考一下, memcached和redis,作为近些年最常用的缓存服务器,相信大家对它们再熟悉不过了。前两年还在学校时,我曾经读过它们的主要源码,如今写篇笔记从个人角度简单对比一下它们的实现方式,权当做复习,有理…

    数据库运维 2018年11月24日
    3.5K00

发表回复

登录后才能评论
联系我们

联系我们

18838889666

在线咨询: QQ交谈

邮件:xinyun@88.com

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

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