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