1+ // EXAMPLE: pipe_trans_tutorial
2+ using NRedisStack ;
3+ using StackExchange . Redis ;
4+ //REMOVE_START
5+ using NRedisStack . Tests ;
6+ using System . Threading . Tasks ;
7+
8+ namespace Doc ;
9+ [ Collection ( "DocsTests" ) ]
10+ //REMOVE_END
11+ public class PipeTransExample
12+ // REMOVE_START
13+ : AbstractNRedisStackTest , IDisposable
14+ // REMOVE_END
15+ {
16+ // REMOVE_START
17+ public PipeTransExample ( EndpointsFixture fixture ) : base ( fixture ) { }
18+
19+ [ SkippableFact ]
20+ // REMOVE_END
21+ public async Task run ( )
22+ {
23+ //REMOVE_START
24+ // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
25+ SkipIfTargetConnectionDoesNotExist ( EndpointsFixture . Env . Standalone ) ;
26+ var _ = GetCleanDatabase ( EndpointsFixture . Env . Standalone ) ;
27+ //REMOVE_END
28+ var muxer = ConnectionMultiplexer . Connect ( "localhost:6379" ) ;
29+ var db = muxer . GetDatabase ( ) ;
30+ // REMOVE_START
31+ db . KeyDelete ( new RedisKey [ ] {
32+ "counter:1" , "counter:2" , "counter:3" ,
33+ "seat:0" , "seat:1" , "seat:2" , "seat:3" , "seat:4" ,
34+ "customer:39182" ,
35+ "Details"
36+ } ) ;
37+ // REMOVE_END
38+
39+ // STEP_START basic_pipe
40+ var pipeline = new Pipeline ( db ) ;
41+
42+ for ( int i = 0 ; i < 5 ; i ++ ) {
43+ pipeline . Db . StringSetAsync ( $ "seat:{ i } ", $ "#{ i } ") ;
44+ }
45+ pipeline . Execute ( ) ;
46+
47+ var resp1 = db . StringGet ( "seat:0" ) ;
48+ Console . WriteLine ( resp1 ) ; // >>> #0
49+
50+ var resp2 = db . StringGet ( "seat:3" ) ;
51+ Console . WriteLine ( resp2 ) ; // >>> #3
52+
53+ var resp3 = db . StringGet ( "seat:4" ) ;
54+ Console . WriteLine ( resp2 ) ; // >>> #4
55+ // STEP_END
56+ // REMOVE_START
57+ Assert . Equal ( "#0" , resp1 ) ;
58+ Assert . Equal ( "#3" , resp2 ) ;
59+ Assert . Equal ( "#4" , resp3 ) ;
60+ // REMOVE_END
61+
62+ // STEP_START basic_trans
63+ var trans = new Transaction ( db ) ;
64+
65+ trans . Db . StringIncrementAsync ( "counter:1" , 1 ) ;
66+ trans . Db . StringIncrementAsync ( "counter:2" , 2 ) ;
67+ trans . Db . StringIncrementAsync ( "counter:3" , 3 ) ;
68+
69+ trans . Execute ( ) ;
70+
71+ var resp4 = db . StringGet ( "counter:1" ) ;
72+ Console . WriteLine ( resp4 ) ; // >>> 1
73+
74+ var resp5 = db . StringGet ( "counter:2" ) ;
75+ Console . WriteLine ( resp5 ) ; // >>> 2
76+
77+ var resp6 = db . StringGet ( "counter:3" ) ;
78+ Console . WriteLine ( resp6 ) ; // >>> 3
79+ // STEP_END
80+ // REMOVE_START
81+ Assert . Equal ( "1" , resp4 ) ;
82+ Assert . Equal ( "2" , resp5 ) ;
83+ Assert . Equal ( "3" , resp6 ) ;
84+ // REMOVE_END
85+
86+ // STEP_START trans_watch
87+ var watchedTrans = new Transaction ( db ) ;
88+
89+ watchedTrans . AddCondition ( Condition . KeyNotExists ( "customer:39182" ) ) ;
90+
91+ watchedTrans . Db . HashSetAsync (
92+ "customer:39182" ,
93+ new HashEntry [ ] {
94+ new HashEntry ( "name" , "David" ) ,
95+ new HashEntry ( "age" , "27" )
96+ }
97+ ) ;
98+
99+ bool succeeded = watchedTrans . Execute ( ) ;
100+ Console . WriteLine ( succeeded ) ; // >>> true
101+ // STEP_END
102+ // REMOVE_START
103+ Assert . True ( succeeded ) ;
104+ // REMOVE_END
105+
106+ // STEP_START when_condition
107+ bool resp7 = db . HashSet ( "Details" , "SerialNumber" , "12345" ) ;
108+ Console . WriteLine ( resp7 ) ; // >>> true
109+
110+ db . HashSet ( "Details" , "SerialNumber" , "12345A" , When . NotExists ) ;
111+ string resp8 = db . HashGet ( "Details" , "SerialNumber" ) ;
112+ Console . WriteLine ( resp8 ) ; // >>> 12345
113+
114+ db . HashSet ( "Details" , "SerialNumber" , "12345A" ) ;
115+ string resp9 = db . HashGet ( "Details" , "SerialNumber" ) ;
116+ Console . WriteLine ( resp9 ) ; // >>> 12345A
117+ // STEP_END
118+ // REMOVE_START
119+ Assert . True ( resp7 ) ;
120+ Assert . Equal ( "12345" , resp8 ) ;
121+ Assert . Equal ( "12345A" , resp9 ) ;
122+ // REMOVE_END
123+ }
124+ }
0 commit comments