Redis中事务操作的命令有哪些
小编给大家分享一下Redis中事务操作的命令有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
命令
multi与exec命令行
127.0.0.1:6379>multiOK127.0.0.1:6379>incrtotalQUEUED127.0.0.1:6379>incrlenQUEUED127.0.0.1:6379>exec1)(integer)22)(integer)2127.0.0.1:6379>gettotal"2"127.0.0.1:6379>getlen"2"
lettuce实例
@TestpublicvoidtestMultiExec(){RedisClientclient=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]]TransactionResulttransactionResult=syncCommands.exec();System.out.println(transactionResult);System.out.println(syncCommands.get("hello"));System.out.println(syncCommands.get("world"));}部分执行
命令行
127.0.0.1:6379>multiOK127.0.0.1:6379>setahelloQUEUED127.0.0.1:6379>setbworldQUEUED127.0.0.1:6379>incraQUEUED127.0.0.1:6379>setcpartQUEUED127.0.0.1:6379>exec1)OK2)OK3)(error)ERRvalueisnotanintegeroroutofrange4)OK127.0.0.1:6379>geta"hello"127.0.0.1:6379>getb"world"127.0.0.1:6379>getc"part"
lettuce实例
@TestpublicvoidtestMultiExecError(){RedisClientclient=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:ERRvalueisnotanintegeroroutofrange,OK,1]]TransactionResulttransactionResult=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>setsum1OK127.0.0.1:6379>multiOK127.0.0.1:6379>incrsumQUEUED127.0.0.1:6379>discardOK127.0.0.1:6379>getsum"1"
lettuce实例
@TestpublicvoidtestMultiDiscard(){RedisClientclient=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,成功返回OKStringresult=syncCommands.discard();System.out.println(result);System.out.println(syncCommands.get("key1"));}check and set
@TestpublicvoidtestWatch(){RedisClientclient=RedisClient.create("redis://192.168.99.100:6379/0");StatefulRedisConnection<String,String>connection=client.connect();RedisCommands<String,String>syncCommands=connection.sync();Stringkey="key";syncCommands.watch(key);//anotherconnectionStatefulRedisConnection<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]TransactionResulttransactionResult=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中事务操作的命令有哪些”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。