1
+ // EXAMPLE: set_tutorial
2
+ // HIDE_START
3
+
4
+ //REMOVE_START
5
+
6
+ using NRedisStack . Tests ;
7
+ using StackExchange . Redis ;
8
+
9
+ namespace NRedisStack . Doc ;
10
+ [ Collection ( "DocsTests" ) ]
11
+ //REMOVE_END
12
+ public class StringSnippets
13
+ {
14
+ //REMOVE_START
15
+ [ SkipIfRedis ( Is . OSSCluster ) ]
16
+ //REMOVE_END
17
+ public void run ( )
18
+ {
19
+ var redis = ConnectionMultiplexer . Connect ( "localhost:6379" ) ;
20
+ var db = redis . GetDatabase ( ) ;
21
+
22
+ //HIDE_END
23
+
24
+ //REMOVE_START
25
+ db . KeyDelete ( new RedisKey [ ] { "bike:1" , "bike:2" , "bike:3" , "total_crashes" } ) ;
26
+ //REMOVE_END
27
+
28
+ // STEP_START set_get
29
+ var res1 = db . StringSet ( "bike:1" , "Deimos" ) ;
30
+ Console . WriteLine ( res1 ) ; // true
31
+ var res2 = db . StringGet ( "bike:1" ) ;
32
+ Console . WriteLine ( res2 ) ; // Deimos
33
+ // STEP_END
34
+
35
+ //REMOVE_START
36
+ Assert . True ( res1 ) ;
37
+ Assert . Equal ( "Deimos" , res2 ) ;
38
+ //REMOVE_END
39
+
40
+ //STEP_START setnx_xx
41
+ var res3 = db . StringSet ( "bike:1" , "bike" , when : When . NotExists ) ;
42
+ Console . WriteLine ( res3 ) ; // false
43
+ Console . WriteLine ( db . StringGet ( "bike:1" ) ) ;
44
+ var res4 = db . StringSet ( "bike:1" , "bike" , when : When . Exists ) ;
45
+ Console . WriteLine ( res4 ) ; // true
46
+ //STEP_END
47
+
48
+ //REMOVE_START
49
+ Assert . False ( res3 ) ;
50
+ Assert . True ( res4 ) ;
51
+ //REMOVE_END
52
+
53
+ //STEP_START mset
54
+ var res5 = db . StringSet ( new KeyValuePair < RedisKey , RedisValue > [ ]
55
+ {
56
+ new ( "bike:1" , "Deimos" ) , new ( "bike:2" , "Ares" ) , new ( "bike:3" , "Vanth" )
57
+ } ) ;
58
+ Console . WriteLine ( res5 ) ;
59
+ var res6 = db . StringGet ( new RedisKey [ ] { "bike:1" , "bike:2" , "bike:3" } ) ;
60
+ Console . WriteLine ( res6 ) ;
61
+ //STEP_END
62
+
63
+ //REMOVE_START
64
+ Assert . True ( res5 ) ;
65
+ Assert . Equal ( new [ ] { "Deimos" , "Ares" , "Vanth" } , res6 . Select ( x => x . ToString ( ) ) . ToArray ( ) ) ;
66
+ //REMOVE_END
67
+
68
+ //STEP_START incr
69
+ db . StringSet ( "total_crashes" , 0 ) ;
70
+ var res7 = db . StringIncrement ( "total_crashes" ) ;
71
+ Console . WriteLine ( res7 ) ; // 1
72
+ var res8 = db . StringIncrement ( "total_crashes" , 10 ) ;
73
+ Console . WriteLine ( res8 ) ;
74
+ //STEP_END
75
+
76
+ //REMOVE_START
77
+ Assert . Equal ( 1 , res7 ) ;
78
+ Assert . Equal ( 11 , res8 ) ;
79
+ //REMOVE_END
80
+ }
81
+ }
0 commit comments