您好,欢迎访问一九零五行业门户网

Redis的事务操作的命令与执行操作(代码)

本篇文章给大家带来的内容是关于redis的事务操作的命令与执行操作(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
序本文主要研究一下redis的事务操作
命令multi与exec命令行
127.0.0.1:6379> multiok127.0.0.1:6379> incr totalqueued127.0.0.1:6379> incr lenqueued127.0.0.1:6379> exec1) (integer) 22) (integer) 2127.0.0.1:6379> get total2127.0.0.1:6379> get len2
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> multiok127.0.0.1:6379> set a helloqueued127.0.0.1:6379> set b worldqueued127.0.0.1:6379> incr aqueued127.0.0.1:6379> set c partqueued127.0.0.1:6379> exec1) ok2) ok3) (error) err value is not an integer or out of range4) ok127.0.0.1:6379> get ahello127.0.0.1:6379> get bworld127.0.0.1:6379> get cpart
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 1ok127.0.0.1:6379> multiok127.0.0.1:6379> incr sumqueued127.0.0.1:6379> discardok127.0.0.1:6379> get sum1
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的事务操作的命令与执行操作(代码)的详细内容。
其它类似信息

推荐信息