当前位置:首页 > MySQL教程 > 正文内容

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

搜教程4年前 (2019-12-01)MySQL教程196
本篇文章给人人带来的内容是关于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网别的相干文章!

扫描二维码推送至手机访问。

版权声明:本文由搜教程网发布,如需转载请注明出处。

本文链接:https://www.sojiaocheng.cn/15582.html

标签: redis
分享给朋友:

“Redis的事件操纵的敕令与实行操纵(代码)【MySQL教程】,redis” 的相关文章

mysql群集索引的有哪些瑕玷【MySQL教程】,mysql,缺点,哪些

 聚簇索引并非一种零丁的索引范例,而是一种数据存储体式格局(不是数据构造,而是存储构造),细致细节依赖于其完成体式格局,但innodb的聚簇索引实际上是在同一个构造中保留了btree索引和数据行。   当表有索引时,它的数据行实际上存放在索引的叶子页中,属于聚簇示意数据行和相邻的键值紧凑地存储在一...

MySQL适配器之PyMySQL的细致引见【MySQL教程】,PyMySQL,MySQL,适配器

这篇文章主要为人人细致引见了MySQL适配器PyMySQL的相干材料,具有肯定的参考价值,感兴趣的小伙伴们能够参考一下 本文我们为人人引见 Python3 运用 PyMySQL 衔接数据库,并完成简朴的增编削查。 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本顶用...

mysql数据库在Centos7下没法长途衔接的缘由以及处理详解【MySQL教程】,Centos7,mysql,数据库

MySQL是由Oracle公司开辟的开源SQL数据库治理体系,下面这篇文章重要给人人引见了关于在Centos7下没法长途衔接mysql数据库的缘由与处理要领,文中经由过程示例代码引见的异常细致,须要的朋侪能够参考自创,下面来一同看看吧。 媒介 近来在工作中遇到一个题目,发如今Centos7体...

MYSQL完成防备增加购物车反复的代码实例【MySQL教程】,MYSQL,购物车,添加

在向mysql中插进去数据的时刻最须要注意的就是防备反复发增加数据,下面这篇文章主要给人人引见了关于MYSQL怎样完成增加购物车的时刻防备反复增加的相干材料,文中经由历程示例代码引见的异常细致,须要的朋侪能够参考自创,下面来一同看看吧。 媒介 近来由于事情的缘由,在做APP购物车下单付出这一...

MySQL挑选适宜的引擎及引擎转换的详解【MySQL教程】,MySQL,引擎,选择

我们怎样挑选适宜的引擎?这里简朴归结一句话:“ 除非须要用到某些InnoDB不具备的特征,而且没有其他要领能够替换,不然都应该优先挑选InnoDB引擎。” 除非万不得已,不然不发起夹杂运用多种存储引擎,不然能够带来一系列庞杂的题目以及一些潜伏的BUG。 运用差别引擎斟酌的几大要素:...

MySQL高可用解决方案MMM详解【MySQL教程】,MySQL,解决方案,详解

MySQL高可用解决方案MMM详解【MySQL教程】,MySQL,解决方案,详解

MySQL自身没有供应replication failover的解决计划,经由过程MMM计划能完成效劳器的毛病转移,从而完成mysql的高可用。MMM不仅能供应浮动IP的功用,假如当前的主效劳器挂掉后,会将你后端的从效劳器自动转向新的主效劳器举行同步复制,不必手工变动同步设置 一、MMM简介:...