11
11
//
12
12
13
13
using System ;
14
- using System . Collections . Generic ;
15
14
using ServiceStack . Redis . Pipeline ;
16
15
17
16
namespace ServiceStack . Redis
18
17
{
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 ) { }
30
27
31
28
protected override void Init ( )
32
29
{
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 ;
39
36
}
40
37
41
38
/// <summary>
@@ -44,56 +41,49 @@ protected override void Init()
44
41
/// <param name="queued"></param>
45
42
private void QueueExpectQueued ( )
46
43
{
47
- QueuedCommands . Insert ( 0 , new QueuedRedisOperation ( )
48
- {
49
- VoidReadCommand = RedisClient . ExpectQueued
50
- } ) ;
44
+ QueuedCommands . Insert ( 0 , new QueuedRedisOperation
45
+ {
46
+ VoidReadCommand = RedisClient . ExpectQueued
47
+ } ) ;
51
48
}
52
49
53
-
54
50
/// <summary>
55
51
/// Issue exec command (not queued)
56
52
/// </summary>
57
53
private void Exec ( )
58
54
{
59
55
RedisClient . Exec ( ) ;
60
56
RedisClient . FlushSendBuffer ( ) ;
61
-
62
57
}
63
58
64
- public bool Commit ( )
59
+ public bool Commit ( )
65
60
{
66
- bool rc = true ;
61
+ bool rc = true ;
67
62
try
68
63
{
69
- _numCommands = QueuedCommands . Count / 2 ;
64
+ numCommands = QueuedCommands . Count / 2 ;
70
65
71
66
//insert multi command at beginning
72
- QueuedCommands . Insert ( 0 , new QueuedRedisCommand ( )
73
- {
67
+ QueuedCommands . Insert ( 0 , new QueuedRedisCommand {
74
68
VoidReturnCommand = r => Init ( ) ,
75
69
VoidReadCommand = RedisClient . ExpectOk ,
76
70
} ) ;
77
71
78
-
79
72
//the first half of the responses will be "QUEUED",
80
73
// so insert reading of multiline after these responses
81
- QueuedCommands . Insert ( _numCommands + 1 , new QueuedRedisOperation ( )
82
- {
74
+ QueuedCommands . Insert ( numCommands + 1 , new QueuedRedisOperation {
83
75
IntReadCommand = RedisClient . ReadMultiDataResultCount ,
84
76
OnSuccessIntCallback = handleMultiDataResultCount
85
77
} ) ;
86
78
87
79
// add Exec command at end (not queued)
88
- QueuedCommands . Add ( new RedisCommand ( )
89
- {
80
+ QueuedCommands . Add ( new RedisCommand {
90
81
VoidReturnCommand = r => Exec ( )
91
82
} ) ;
92
83
93
84
//execute transaction
94
85
Exec ( ) ;
95
-
96
- /////////////////////////////
86
+
97
87
//receive expected results
98
88
foreach ( var queuedCommand in QueuedCommands )
99
89
{
@@ -110,7 +100,7 @@ public bool Commit()
110
100
ClosePipeline ( ) ;
111
101
RedisClient . AddTypeIdsRegisteredDuringPipeline ( ) ;
112
102
}
113
- return rc ;
103
+ return rc ;
114
104
}
115
105
116
106
/// <summary>
@@ -122,21 +112,21 @@ private void handleMultiDataResultCount(int count)
122
112
// transaction failed due to WATCH condition
123
113
if ( count == - 1 )
124
114
throw new RedisTransactionFailedException ( ) ;
125
- if ( count != _numCommands )
115
+ if ( count != numCommands )
126
116
throw new InvalidOperationException ( string . Format (
127
117
"Invalid results received from 'EXEC', expected '{0}' received '{1}'"
128
118
+ "\n Warning: Transaction was committed" ,
129
- _numCommands , count ) ) ;
119
+ numCommands , count ) ) ;
130
120
}
131
121
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" ) ;
136
126
137
- RedisClient . Transaction = null ;
138
- RedisClient . ClearTypeIdsRegisteredDuringPipeline ( ) ;
139
- }
127
+ RedisClient . Transaction = null ;
128
+ RedisClient . ClearTypeIdsRegisteredDuringPipeline ( ) ;
129
+ }
140
130
141
131
public bool Replay ( )
142
132
{
@@ -145,7 +135,6 @@ public bool Replay()
145
135
{
146
136
Execute ( ) ;
147
137
148
- /////////////////////////////
149
138
//receive expected results
150
139
foreach ( var queuedCommand in QueuedCommands )
151
140
{
@@ -154,9 +143,9 @@ public bool Replay()
154
143
}
155
144
catch ( RedisTransactionFailedException e )
156
145
{
157
- rc = false ;
146
+ rc = false ;
158
147
}
159
- finally
148
+ finally
160
149
{
161
150
RedisClient . Transaction = null ;
162
151
ClosePipeline ( ) ;
@@ -165,20 +154,17 @@ public bool Replay()
165
154
return rc ;
166
155
}
167
156
168
- public void Dispose ( )
169
- {
157
+ public void Dispose ( )
158
+ {
170
159
base . Dispose ( ) ;
171
160
if ( RedisClient . Transaction == null ) return ;
172
- Rollback ( ) ;
161
+ Rollback ( ) ;
173
162
}
174
163
175
- #region Overrides of RedisQueueCompletableOperation methods
176
-
177
164
protected override void AddCurrentQueuedOperation ( )
178
165
{
179
166
base . AddCurrentQueuedOperation ( ) ;
180
167
QueueExpectQueued ( ) ;
181
168
}
182
- #endregion
183
169
}
184
170
}
0 commit comments