事务

1.开启事务,提交事务

  1. 127.0.0.1:6379> multi
  2. OK
  3. 127.0.0.1:6379> set k1 v1
  4. QUEUED
  5. 127.0.0.1:6379> set k2 v2
  6. QUEUED
  7. 127.0.0.1:6379> exec
  8. 1) OK
  9. 2) OK
  10. 127.0.0.1:6379> keys *
  11. 1) "k2"
  12. 2) "k1"

2.开启事务,放弃事务

  1. 127.0.0.1:6379> multi
  2. OK
  3. 127.0.0.1:6379> set k3 v3
  4. QUEUED
  5. 127.0.0.1:6379> set k4 v4
  6. QUEUED
  7. 127.0.0.1:6379> discard
  8. OK
  9. 127.0.0.1:6379> keys *
  10. 1) "k2"
  11. 2) "k1"

3.开启事务,放到队列报错,执行,全部失效

  1. 127.0.0.1:6379> multi
  2. OK
  3. 127.0.0.1:6379> set k1 v1
  4. QUEUED
  5. 127.0.0.1:6379> set k2
  6. (error) ERR wrong number of arguments for 'set' command
  7. 127.0.0.1:6379> exec
  8. (error) EXECABORT Transaction discarded because of previous errors.
  9. 127.0.0.1:6379>
  10. 127.0.0.1:6379> keys *
  11. (empty list or set)

4.开启事务,放到队列没报错,执行期间报错,执行后,正确的依然生效

  1. 127.0.0.1:6379> multi
  2. OK
  3. 127.0.0.1:6379> set k1 v1
  4. QUEUED
  5. 127.0.0.1:6379> incr k1
  6. QUEUED
  7. 127.0.0.1:6379> set k2 v2
  8. QUEUED
  9. 127.0.0.1:6379> exec
  10. 1) OK
  11. 2) (error) ERR value is not an integer or out of range
  12. 3) OK
  13. 127.0.0.1:6379> keys *
  14. 1) "k2"
  15. 2) "k1"
  16. 127.0.0.1:6379>

真实案例
image.png

乐观锁vs悲观锁
image.png
image.png
image.png
watch命令
image.png
image.png

秒杀案例

image.png