forked from redis/NRedisStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC-4093 HGET/HSET command code samples (redis#330)
* DOC-4093 added hash examples * DOC-4093 dotnet format changes
- Loading branch information
1 parent
0ae51b4
commit 4a503d2
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// EXAMPLE: cmds_hash | ||
// HIDE_START | ||
|
||
using NRedisStack.Tests; | ||
using StackExchange.Redis; | ||
|
||
// HIDE_END | ||
|
||
// REMOVE_START | ||
namespace Doc; | ||
[Collection("DocsTests")] | ||
// REMOVE_END | ||
|
||
// HIDE_START | ||
public class CmdsHashExample | ||
{ | ||
|
||
[SkipIfRedis(Is.OSSCluster)] | ||
public void run() | ||
{ | ||
var muxer = ConnectionMultiplexer.Connect("localhost:6379"); | ||
var db = muxer.GetDatabase(); | ||
//REMOVE_START | ||
// Clear any keys here before using them in tests. | ||
db.KeyDelete("myhash"); | ||
//REMOVE_END | ||
// HIDE_END | ||
|
||
|
||
// STEP_START hget | ||
bool res1 = db.HashSet("myhash", "field1", "foo"); | ||
|
||
RedisValue res2 = db.HashGet("myhash", "field1"); | ||
Console.WriteLine(res2); // >>> foo | ||
|
||
RedisValue res3 = db.HashGet("myhash", "field2"); | ||
Console.WriteLine(res3); // >>> Null | ||
// STEP_END | ||
|
||
// Tests for 'hget' step. | ||
// REMOVE_START | ||
Assert.True(res1); | ||
Assert.Equal("foo", res2); | ||
Assert.Equal(RedisValue.Null, res3); | ||
db.KeyDelete("myhash"); | ||
// REMOVE_END | ||
|
||
// STEP_START hset | ||
bool res4 = db.HashSet("myhash", "field1", "Hello"); | ||
RedisValue res5 = db.HashGet("myhash", "field1"); | ||
Console.WriteLine(res5); // >>> Hello | ||
|
||
db.HashSet( | ||
"myhash", | ||
new HashEntry[] { | ||
new HashEntry("field2", "Hi"), | ||
new HashEntry("field3", "World") | ||
} | ||
); | ||
|
||
RedisValue res6 = db.HashGet("myhash", "field2"); | ||
Console.WriteLine(res6); // >>> Hi | ||
|
||
RedisValue res7 = db.HashGet("myhash", "field3"); | ||
Console.WriteLine(res7); // >>> World | ||
|
||
HashEntry[] res8 = db.HashGetAll("myhash"); | ||
Console.WriteLine($"{string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))}"); | ||
// >>> field1: Hello, field2: Hi, field3: World | ||
// STEP_END | ||
|
||
// Tests for 'hset' step. | ||
// REMOVE_START | ||
Assert.True(res4); | ||
Assert.Equal("Hello", res5); | ||
Assert.Equal("Hi", res6); | ||
Assert.Equal("World", res7); | ||
Assert.Equal( | ||
"field1: Hello, field2: Hi, field3: World", | ||
string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}")) | ||
); | ||
// REMOVE_END | ||
// HIDE_START | ||
} | ||
} | ||
// HIDE_END | ||
|