第一篇中介绍了Redis是一个强大的键-值仓储,支持五种灵活的数据结构。事实上,Redis还支持其它的一些高级:事务、发布与订阅、管道、脚本等,本篇我们来看一下事务。
127.0.0.1:6379> set balance 100 OK 127.0.0.1:6379> set debt 0 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby balance 25 QUEUED 127.0.0.1:6379> incrby debt 25 QUEUED 127.0.0.1:6379> exec 1) (integer) 75 2) (integer) 25 127.0.0.1:6379> get balance "75" 127.0.0.1:6379> get debt "25"
redis.multi() balance = redis.get('balance') if (balance < amtToSubtract) { redis.discard() } else { redis.decrby('balance', amtToSubtract) redis.incrby('debt', amtToSubtract) redis.exec() }
redis.watch('balance') balance = redis.get('balance') if (balance < amtToSubtract) { redis.unwatch() } else { redis.multi() redis.decrby('balance', amtToSubtract) redis.incrby('debt', amtToSubtract) redis.exec() }
127.0.0.1:6379> set value 1 OK 127.0.0.1:6379> set value2 abc OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> incr value QUEUED 127.0.0.1:6379> incr value2 QUEUED 127.0.0.1:6379> exec 1) (integer) 2 2) (error) ERR value is not an integer or out of range 127.0.0.1:6379> get value "2" 127.0.0.1:6379>
原文地址:http://blog.csdn.net/oyl822/article/details/44337119