(integer) 25 127.0.0.1:6379> pfadd my a b c (integer) 1 127.0.0.1:6379> pfadd my2 b c d (integer) 1 127.0.0.1:6379> pfcount my (integer) 3 127.0.0.1:6379> pfcount my2 (integer) 3 127.0.0.1:6379> pfmerge myc my my2 # 合并为myc OK 127.0.0.1:6379> pfcount myc # 合并后长度 (integer) 4
127.0.0.1:6379> multi # 开启事务 OK # 命令入队 127.0.0.1:6379(TX)> set k1 v1 QUEUED 127.0.0.1:6379(TX)> set k2 v2 QUEUED 127.0.0.1:6379(TX)> get k2 QUEUED 127.0.0.1:6379(TX)> set k3 v3 QUEUED 127.0.0.1:6379(TX)> exec # 执行命令 1) OK 2) OK 3) "v2" 4) OK
放弃事务
1 2 3 4 5 6 7 8 9 10 11 12 13
127.0.0.1:6379> multi # 开启事务 OK # 命令入队 127.0.0.1:6379(TX)> set k1 v1 QUEUED 127.0.0.1:6379(TX)> set k2 v2 QUEUED 127.0.0.1:6379(TX)> set k4 v4 QUEUED 127.0.0.1:6379(TX)> discard # 放弃事务 OK 127.0.0.1:6379> get k4 (nil) # 说明命令没有被执行
编译型异常
事务中所有命令都不会执行;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
127.0.0.1:6379> multi OK 127.0.0.1:6379(TX)> set k1 v1 QUEUED 127.0.0.1:6379(TX)> set k2 v2 QUEUED 127.0.0.1:6379(TX)> set k3 v3 QUEUED 127.0.0.1:6379(TX)> getset k3 # 错误的命令 (error) ERR wrong number of arguments for 'getset' command 127.0.0.1:6379(TX)> set k4 v5 QUEUED 127.0.0.1:6379(TX)> set k5 v5 QUEUED 127.0.0.1:6379(TX)> exec # 执行事务直接报错 (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> get k5 # 事务中的所有命令都未执行 (nil)
运行时异常
错误命令不会执行并抛出异常,其他命令不受影响;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
127.0.0.1:6379> multi OK 127.0.0.1:6379(TX)> set k1 "v1" QUEUED 127.0.0.1:6379(TX)> incr k1 # k1为字符串,对它+1一定不会成功 QUEUED 127.0.0.1:6379(TX)> set k2 v2 QUEUED 127.0.0.1:6379(TX)> set k3 v3 QUEUED 127.0.0.1:6379(TX)> get k3 QUEUED 127.0.0.1:6379(TX)> exec 1) OK 2) (error) ERR value is not an integer or out of range # 可以看到只有这一项报错了,其他事务没受到影响 3) OK 4) OK 5) "v3"
127.0.0.1:6379> set money 100 OK 127.0.0.1:6379> set out 0 OK 127.0.0.1:6379> watch money # 监视money对象 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379(TX)> decrby money 20 QUEUED 127.0.0.1:6379(TX)> incrby out 20 QUEUED 127.0.0.1:6379(TX)> exec 1) (integer) 80 2) (integer) 20