Skip to content

Commit 4a503d2

Browse files
DOC-4093 HGET/HSET command code samples (#330)
* DOC-4093 added hash examples * DOC-4093 dotnet format changes
1 parent 0ae51b4 commit 4a503d2

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

tests/Doc/CmdsHashExample.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// EXAMPLE: cmds_hash
2+
// HIDE_START
3+
4+
using NRedisStack.Tests;
5+
using StackExchange.Redis;
6+
7+
// HIDE_END
8+
9+
// REMOVE_START
10+
namespace Doc;
11+
[Collection("DocsTests")]
12+
// REMOVE_END
13+
14+
// HIDE_START
15+
public class CmdsHashExample
16+
{
17+
18+
[SkipIfRedis(Is.OSSCluster)]
19+
public void run()
20+
{
21+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
22+
var db = muxer.GetDatabase();
23+
//REMOVE_START
24+
// Clear any keys here before using them in tests.
25+
db.KeyDelete("myhash");
26+
//REMOVE_END
27+
// HIDE_END
28+
29+
30+
// STEP_START hget
31+
bool res1 = db.HashSet("myhash", "field1", "foo");
32+
33+
RedisValue res2 = db.HashGet("myhash", "field1");
34+
Console.WriteLine(res2); // >>> foo
35+
36+
RedisValue res3 = db.HashGet("myhash", "field2");
37+
Console.WriteLine(res3); // >>> Null
38+
// STEP_END
39+
40+
// Tests for 'hget' step.
41+
// REMOVE_START
42+
Assert.True(res1);
43+
Assert.Equal("foo", res2);
44+
Assert.Equal(RedisValue.Null, res3);
45+
db.KeyDelete("myhash");
46+
// REMOVE_END
47+
48+
// STEP_START hset
49+
bool res4 = db.HashSet("myhash", "field1", "Hello");
50+
RedisValue res5 = db.HashGet("myhash", "field1");
51+
Console.WriteLine(res5); // >>> Hello
52+
53+
db.HashSet(
54+
"myhash",
55+
new HashEntry[] {
56+
new HashEntry("field2", "Hi"),
57+
new HashEntry("field3", "World")
58+
}
59+
);
60+
61+
RedisValue res6 = db.HashGet("myhash", "field2");
62+
Console.WriteLine(res6); // >>> Hi
63+
64+
RedisValue res7 = db.HashGet("myhash", "field3");
65+
Console.WriteLine(res7); // >>> World
66+
67+
HashEntry[] res8 = db.HashGetAll("myhash");
68+
Console.WriteLine($"{string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))}");
69+
// >>> field1: Hello, field2: Hi, field3: World
70+
// STEP_END
71+
72+
// Tests for 'hset' step.
73+
// REMOVE_START
74+
Assert.True(res4);
75+
Assert.Equal("Hello", res5);
76+
Assert.Equal("Hi", res6);
77+
Assert.Equal("World", res7);
78+
Assert.Equal(
79+
"field1: Hello, field2: Hi, field3: World",
80+
string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))
81+
);
82+
// REMOVE_END
83+
// HIDE_START
84+
}
85+
}
86+
// HIDE_END
87+

0 commit comments

Comments
 (0)