1111//
1212
1313using System ;
14- using System . Collections . Generic ;
1514using ServiceStack . Redis . Pipeline ;
1615
1716namespace ServiceStack . Redis
1817{
19- /// <summary>
20- /// Adds support for Redis Transactions (i.e. MULTI/EXEC/DISCARD operations).
21- /// </summary>
22- public class RedisTransaction
23- : RedisAllPurposePipeline , IRedisTransaction , IRedisQueueCompletableOperation
24- {
25- private int _numCommands = 0 ;
26- public RedisTransaction ( RedisClient redisClient ) : base ( redisClient )
27- {
28-
29- }
18+ /// <summary>
19+ /// Adds support for Redis Transactions (i.e. MULTI/EXEC/DISCARD operations).
20+ /// </summary>
21+ public class RedisTransaction
22+ : RedisAllPurposePipeline , IRedisTransaction , IRedisQueueCompletableOperation
23+ {
24+ private int numCommands = 0 ;
25+ public RedisTransaction ( RedisClient redisClient )
26+ : base ( redisClient ) { }
3027
3128 protected override void Init ( )
3229 {
33- //start pipelining
34- base . Init ( ) ;
35- //queue multi command
36- RedisClient . Multi ( ) ;
37- //set transaction
38- RedisClient . Transaction = this ;
30+ //start pipelining
31+ base . Init ( ) ;
32+ //queue multi command
33+ RedisClient . Multi ( ) ;
34+ //set transaction
35+ RedisClient . Transaction = this ;
3936 }
4037
4138 /// <summary>
@@ -44,56 +41,49 @@ protected override void Init()
4441 /// <param name="queued"></param>
4542 private void QueueExpectQueued ( )
4643 {
47- QueuedCommands . Insert ( 0 , new QueuedRedisOperation ( )
48- {
49- VoidReadCommand = RedisClient . ExpectQueued
50- } ) ;
44+ QueuedCommands . Insert ( 0 , new QueuedRedisOperation
45+ {
46+ VoidReadCommand = RedisClient . ExpectQueued
47+ } ) ;
5148 }
5249
53-
5450 /// <summary>
5551 /// Issue exec command (not queued)
5652 /// </summary>
5753 private void Exec ( )
5854 {
5955 RedisClient . Exec ( ) ;
6056 RedisClient . FlushSendBuffer ( ) ;
61-
6257 }
6358
64- public bool Commit ( )
59+ public bool Commit ( )
6560 {
66- bool rc = true ;
61+ bool rc = true ;
6762 try
6863 {
69- _numCommands = QueuedCommands . Count / 2 ;
64+ numCommands = QueuedCommands . Count / 2 ;
7065
7166 //insert multi command at beginning
72- QueuedCommands . Insert ( 0 , new QueuedRedisCommand ( )
73- {
67+ QueuedCommands . Insert ( 0 , new QueuedRedisCommand {
7468 VoidReturnCommand = r => Init ( ) ,
7569 VoidReadCommand = RedisClient . ExpectOk ,
7670 } ) ;
7771
78-
7972 //the first half of the responses will be "QUEUED",
8073 // so insert reading of multiline after these responses
81- QueuedCommands . Insert ( _numCommands + 1 , new QueuedRedisOperation ( )
82- {
74+ QueuedCommands . Insert ( numCommands + 1 , new QueuedRedisOperation {
8375 IntReadCommand = RedisClient . ReadMultiDataResultCount ,
8476 OnSuccessIntCallback = handleMultiDataResultCount
8577 } ) ;
8678
8779 // add Exec command at end (not queued)
88- QueuedCommands . Add ( new RedisCommand ( )
89- {
80+ QueuedCommands . Add ( new RedisCommand {
9081 VoidReturnCommand = r => Exec ( )
9182 } ) ;
9283
9384 //execute transaction
9485 Exec ( ) ;
95-
96- /////////////////////////////
86+
9787 //receive expected results
9888 foreach ( var queuedCommand in QueuedCommands )
9989 {
@@ -110,7 +100,7 @@ public bool Commit()
110100 ClosePipeline ( ) ;
111101 RedisClient . AddTypeIdsRegisteredDuringPipeline ( ) ;
112102 }
113- return rc ;
103+ return rc ;
114104 }
115105
116106 /// <summary>
@@ -122,21 +112,21 @@ private void handleMultiDataResultCount(int count)
122112 // transaction failed due to WATCH condition
123113 if ( count == - 1 )
124114 throw new RedisTransactionFailedException ( ) ;
125- if ( count != _numCommands )
115+ if ( count != numCommands )
126116 throw new InvalidOperationException ( string . Format (
127117 "Invalid results received from 'EXEC', expected '{0}' received '{1}'"
128118 + "\n Warning: Transaction was committed" ,
129- _numCommands , count ) ) ;
119+ numCommands , count ) ) ;
130120 }
131121
132- public void Rollback ( )
133- {
134- if ( RedisClient . Transaction == null )
135- throw new InvalidOperationException ( "There is no current transaction to Rollback" ) ;
122+ public void Rollback ( )
123+ {
124+ if ( RedisClient . Transaction == null )
125+ throw new InvalidOperationException ( "There is no current transaction to Rollback" ) ;
136126
137- RedisClient . Transaction = null ;
138- RedisClient . ClearTypeIdsRegisteredDuringPipeline ( ) ;
139- }
127+ RedisClient . Transaction = null ;
128+ RedisClient . ClearTypeIdsRegisteredDuringPipeline ( ) ;
129+ }
140130
141131 public bool Replay ( )
142132 {
@@ -145,7 +135,6 @@ public bool Replay()
145135 {
146136 Execute ( ) ;
147137
148- /////////////////////////////
149138 //receive expected results
150139 foreach ( var queuedCommand in QueuedCommands )
151140 {
@@ -154,9 +143,9 @@ public bool Replay()
154143 }
155144 catch ( RedisTransactionFailedException e )
156145 {
157- rc = false ;
146+ rc = false ;
158147 }
159- finally
148+ finally
160149 {
161150 RedisClient . Transaction = null ;
162151 ClosePipeline ( ) ;
@@ -165,20 +154,17 @@ public bool Replay()
165154 return rc ;
166155 }
167156
168- public void Dispose ( )
169- {
157+ public void Dispose ( )
158+ {
170159 base . Dispose ( ) ;
171160 if ( RedisClient . Transaction == null ) return ;
172- Rollback ( ) ;
161+ Rollback ( ) ;
173162 }
174163
175- #region Overrides of RedisQueueCompletableOperation methods
176-
177164 protected override void AddCurrentQueuedOperation ( )
178165 {
179166 base . AddCurrentQueuedOperation ( ) ;
180167 QueueExpectQueued ( ) ;
181168 }
182- #endregion
183169 }
184170}
0 commit comments