Redis part of the support services

Affairs

  Performing a set of commands sequentially executed without being inserted serially other commands

Transaction action

  A queue, a disposable, sequential, exclusive execute a series of commands

Command and transaction-related operations  

. 1            discard       # canceled 
2            Exec          # Run all transactions within the block 
. 3            Multi         # marks the start of a block transaction 
. 4            unwatch    # cancellation monitor all the key 
. 5            Watch       # monitoring one or more key    

  Normal operation of its services, to abandon the operation, all the guilt, injustice head creditors

 1 # ---------------正常操作-------------------------
 2     127.0.0.1:6379> keys *
 3     (empty list or set)
 4     127.0.0.1:6379> MULTI
 5     OK
 6     127.0.0.1:6379> set k1 v1
 7     QUEUED
 8     127.0.0.1:6379> set k2 v2
 9     QUEUED
10     127.0.0.1:6379> get k2
11     QUEUED
12     127.0.0.1:6379> set k3 v3
13     QUEUED
14     127.0.0.1:6379> EXEC
15     1) OK
16     2) OK
17     3) "v2"
18     4) OK
19     127.0.0.1:6379> keys *
20     1) "k2"
21     2) "k1"
22     3) "k3"
23 # -------------------取消操作------------------
24     127.0.0.1:6379> MULTI
25     OK
26     127.0.0.1:6379> set k1 v1
27     QUEUED
28     127.0.0.1:6379> set k2 22
29     QUEUED
30     127.0.0.1:6379> DISCARD
31     OK
32     127.0.0.1:6379> get k2
33     "v2"
34 # --------------------全体连坐-------------------
35     127.0.0.1:6379> MULTI
36     OK
37     127.0.0.1:6379> set k1 v1
38     QUEUED
39     127.0.0.1:6379> set k2 v2
40     QUEUED
41     127.0.0.1:6379> set k3 v3
42     QUEUED
43     127.0.0.1:6379> getset k3
44     (error) ERR wrong number of arguments for 'getset' command
45     127.0.0.1:6379> set k4 v4
46     QUEUED
47     127.0.0.1:6379> EXEC
48     (error) EXECABORT Transaction discarded because of previous errors.
49     127.0.0.1:6379> get k4
50     (nil)
51     127.0.0.1:6379> set k1 aa
52     OK
53     127.0.0.1:6379> get k1
54     "aa"
55     127.0.0.1:6379> * Keys
 56 is      . 1) " K2 " 
57 is      2) " K1 " 
58      . 3) " K3 " 
59  #   --------------------- injustice creditors head (support portion of the transaction can be seen) ------------- 
60      127.0.0.1:6379> the MULTI
 61 is      the OK
 62 is      127.0.0.1:6379> INCR K1
 63 is      the QUEUED
 64      127.0.0.1:6379 > K2 SET 22 is
 65      the QUEUED
 66      127.0.0.1:6379> K3 SET 33 is
 67      the QUEUED
 68      127.0.0.1:6379> SET K4 44 is
 69     QUEUED
70     127.0.0.1:6379> get k4
71     QUEUED
72     127.0.0.1:6379> EXEC
73     1) (error) ERR value is not an integer or out of range
74     2) OK
75     3) OK
76     4) OK
77     5) "44"
78     127.0.0.1:6379> get k4
79     "44"

  watch monitor    

Pessimistic locking: Some people think that a certain change an entire table lock poor concurrency, consistency
optimistic lock: I think the data will not be modified without the lock table to ensure the consistency of its concurrent version Version plus a number after the data if there are two operations this data, then the person submitting the database will then get the new version number of the person submitting prior to the submission of CAS (Check And Set) after making changes

Table lock: Lock the entire table, poor concurrency, consistency
row locks: narrow range, only the row lock

  watch command similar optimistic locking 

 1 # 第一个终端
 2 127.0.0.1:6379> keys *
 3 1) "debt"
 4 2) "k1"
 5 3) "k3"
 6 4) "k4"
 7 5) "k2"
 8 6) "balance"
 9 127.0.0.1:6379> set debt 0
10 OK
11 127.0.0.1:6379> set balance 100
12 OK
13 127.0.0.1:6379> WATCH balance
14 OK
15 127.0.0.1:6379> MULTI
16 OK
17 127.0.0.1:6379> DECRBY balance 20
18 QUEUED
19 127.0.0.1:6379> INCRBY debt 20
20 QUEUED
21 127.0.0.1:6379> EXEC
22 1) (integer) 80
23 2) (integer) 20
24 # ---------------------------加锁-----------------------
25 127.0.0.1:6379> WATCH balance
26 OK
27 127.0.0.1:6379> MULTI
28 OK
29 127.0.0.1:6379> DECRBY balance 20
30 QUEUED
31 127.0.0.1:6379> INCRBY debt 20
32 QUEUED
33 127.0.0.1:6379> EXEC
34 (nil)
35 127.0.0.1:6379> get balance
36 "800"
37 127.0.0.1:6379> get debt
38 "20"
39 # --------------------------------unwatch不加锁----------------
40 127.0.0.1:6379> WATCH balance
41 OK
42 127.0.0.1:6379> set balance 500
43 OK
44 127.0.0.1:6379> UNWATCH
45 OK
46127.0.0.1:6379> WATCH Balance
 47  the OK
 48 127.0.0.1:6379> the MULTI
 49  the OK
 50 127.0.0.1:6379> SET Balance 80
 51 is  the QUEUED
 52 is 127.0.0.1:6379> SET Debt 20 is
 53 is  the QUEUED
 54 is 127.0.0.1:6379 > EXEC
 55 . 1 ) the OK
 56 is 2 ) the OK
 57 is  
58  # The second terminal 
59 [the root @ localhost ~] # Redis CLI- 
60 127.0.0.1:6379> * Keys
 61 is . 1) " Debt " 
62 is 2) "k1"
63 3) "k3"
64 4) "k4"
65 5) "k2"
66 6) "balance"
67 127.0.0.1:6379> get debt
68 "20"
69 127.0.0.1:6379> get balance
70 "80"
71 # ------------------------加锁--------------------------
72 127.0.0.1:6379> set balance 800
73 OK
74 127.0.0.1:6379> 
75 #----------------------- ------------------ unlocked unwatch 
76 127.0.0.1: 6379> GET Balance
 77  " 500 "

 

Guess you like

Origin www.cnblogs.com/Alexephor/p/11460928.html
Recommended