1+ // EXAMPLE: hash_tutorial
2+ // HIDE_START
3+
4+ using NRedisStack . Tests ;
5+ using StackExchange . Redis ;
6+
7+ //REMOVE_START
8+ namespace Doc ;
9+ [ Collection ( "DocsTests" ) ]
10+ //REMOVE_END
11+ public class HashExample
12+ // REMOVE_START
13+ : AbstractNRedisStackTest , IDisposable
14+ // REMOVE_END
15+ {
16+ // REMOVE_START
17+ public HashExample ( EndpointsFixture fixture ) : base ( fixture ) { }
18+
19+ [ SkippableFact ]
20+ // REMOVE_END
21+ public void 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+ db . KeyDelete ( "bike:1" ) ;
31+ //HIDE_END
32+ //STEP_START set_get_all
33+ // Zuggle
34+ db . HashSet ( "bike:1" , new HashEntry [ ]
35+ {
36+ new HashEntry ( "model" , "Deimos" ) ,
37+ new HashEntry ( "brand" , "Ergonom" ) ,
38+ new HashEntry ( "type" , "Enduro bikes" ) ,
39+ new HashEntry ( "price" , 4972 )
40+ } ) ;
41+
42+ Console . WriteLine ( "Hash Created" ) ;
43+ // Hash Created
44+
45+ var model = db . HashGet ( "bike:1" , "model" ) ;
46+ Console . WriteLine ( $ "Model: { model } ") ;
47+ // Model: Deimos
48+
49+ var price = db . HashGet ( "bike:1" , "price" ) ;
50+ Console . WriteLine ( $ "Price: { price } ") ;
51+ // Price: 4972
52+
53+ var bike = db . HashGetAll ( "bike:1" ) ;
54+ Console . WriteLine ( "bike:1" ) ;
55+ Console . WriteLine ( string . Join ( "\n " , bike . Select ( b => $ "{ b . Name } : { b . Value } ") ) ) ;
56+ // Bike:1:
57+ // model: Deimos
58+ // brand: Ergonom
59+ // type: Enduro bikes
60+ // price: 4972
61+ //STEP_END
62+
63+ //REMOVE_START
64+ Assert . Equal ( 4 , bike . Length ) ;
65+ Assert . Equal ( "Deimos" , model ) ;
66+ Assert . Equal ( 4972 , price ) ;
67+ //REMOVE_END
68+
69+ //STEP_START hmget
70+ var values = db . HashGet ( "bike:1" , new RedisValue [ ] { "model" , "price" } ) ;
71+ Console . WriteLine ( string . Join ( " " , values ) ) ;
72+ // Deimos 4972
73+ //REMOVE_START
74+ Assert . Equal ( "Deimos" , values [ 0 ] ) ;
75+ Assert . Equal ( 4972 , values [ 1 ] ) ;
76+ //REMOVE_END
77+ //STEP_END
78+
79+ //STEP_START hincrby
80+ var newPrice = db . HashIncrement ( "bike:1" , "price" , 100 ) ;
81+ Console . WriteLine ( $ "New price: { newPrice } ") ;
82+ //REMOVE_START
83+ Assert . Equal ( 5072 , newPrice ) ;
84+ //REMOVE_END
85+ // New price: 5072
86+
87+ newPrice = db . HashIncrement ( "bike:1" , "price" , - 100 ) ;
88+ Console . WriteLine ( $ "New price: { newPrice } ") ;
89+ //REMOVE_START
90+ Assert . Equal ( 4972 , newPrice ) ;
91+ //REMOVE_END
92+ // New price: 4972
93+ //STEP_END
94+
95+ //STEP_START incrby_get_mget
96+ var rides = db . HashIncrement ( "bike:1" , "rides" ) ;
97+ Console . WriteLine ( $ "Rides: { rides } ") ;
98+ //REMOVE_START
99+ Assert . Equal ( 1 , rides ) ;
100+ //REMOVE_END
101+ // Rides: 1
102+
103+ rides = db . HashIncrement ( "bike:1" , "rides" ) ;
104+ Console . WriteLine ( $ "Rides: { rides } ") ;
105+ //REMOVE_START
106+ Assert . Equal ( 2 , rides ) ;
107+ //REMOVE_END
108+ // Rides: 2
109+
110+ rides = db . HashIncrement ( "bike:1" , "rides" ) ;
111+ Console . WriteLine ( $ "Rides: { rides } ") ;
112+ //REMOVE_START
113+ Assert . Equal ( 3 , rides ) ;
114+ //REMOVE_END
115+ // Rides: 3
116+
117+ var crashes = db . HashIncrement ( "bike:1" , "crashes" ) ;
118+ Console . WriteLine ( $ "Crashes: { crashes } ") ;
119+ //REMOVE_START
120+ Assert . Equal ( 1 , crashes ) ;
121+ //REMOVE_END
122+ // Crashes: 1
123+
124+ var owners = db . HashIncrement ( "bike:1" , "owners" ) ;
125+ Console . WriteLine ( $ "Owners: { owners } ") ;
126+ //REMOVE_START
127+ Assert . Equal ( 1 , owners ) ;
128+ //REMOVE_END
129+ // Owners: 1
130+
131+ var stats = db . HashGet ( "bike:1" , new RedisValue [ ] { "crashes" , "owners" } ) ;
132+ Console . WriteLine ( $ "Bike stats: crashes={ stats [ 0 ] } , owners={ stats [ 1 ] } ") ;
133+ //REMOVE_START
134+ Assert . Equal ( 1 , stats [ 0 ] ) ;
135+ Assert . Equal ( 1 , stats [ 1 ] ) ;
136+ //REMOVE_END
137+ // Bike stats: crashes=1, owners=1
138+ //STEP_END
139+ //HIDE_START
140+ }
141+ }
142+ //HIDE_END
0 commit comments