|
| 1 | +// EXAMPLE: cmds_hash |
| 2 | +using StackExchange.Redis; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Doc; |
| 6 | + |
| 7 | +public class CmdsHashExample |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Run() |
| 11 | + { |
| 12 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 13 | + var db = muxer.GetDatabase(); |
| 14 | + // Clear any keys here before using them in tests. |
| 15 | + db.KeyDelete("myhash"); |
| 16 | + |
| 17 | + // STEP_START hdel |
| 18 | + bool hdelRes1 = db.HashSet("myhash", "field1", "foo"); |
| 19 | + |
| 20 | + RedisValue hdelRes2 = db.HashDelete("myhash", "field1"); |
| 21 | + Console.WriteLine(hdelRes2); // >>> 1 |
| 22 | + |
| 23 | + RedisValue hdelRes3 = db.HashDelete("myhash", "field1"); |
| 24 | + Console.WriteLine(hdelRes3); // >>> 0 |
| 25 | + |
| 26 | + // STEP_END |
| 27 | + Assert.True(hdelRes1); |
| 28 | + Assert.Equal(1, hdelRes2); |
| 29 | + Assert.Equal(0, hdelRes3); |
| 30 | + db.KeyDelete("myhash"); |
| 31 | + |
| 32 | + // STEP_START hget |
| 33 | + bool hgetRes1 = db.HashSet("myhash", "field1", "foo"); |
| 34 | + |
| 35 | + RedisValue hgetRes2 = db.HashGet("myhash", "field1"); |
| 36 | + Console.WriteLine(hgetRes2); // >>> foo |
| 37 | + |
| 38 | + RedisValue hgetRes3 = db.HashGet("myhash", "field2"); |
| 39 | + Console.WriteLine(hgetRes3); // >>> Null |
| 40 | + |
| 41 | + // STEP_END |
| 42 | + Assert.True(hgetRes1); |
| 43 | + Assert.Equal("foo", hgetRes2); |
| 44 | + Assert.Equal(RedisValue.Null, hgetRes3); |
| 45 | + db.KeyDelete("myhash"); |
| 46 | + |
| 47 | + // STEP_START hset |
| 48 | + bool hsetRes1 = db.HashSet("myhash", "field1", "Hello"); |
| 49 | + RedisValue hsetRes2 = db.HashGet("myhash", "field1"); |
| 50 | + Console.WriteLine(hsetRes2); // >>> Hello |
| 51 | + |
| 52 | + db.HashSet( |
| 53 | + "myhash", |
| 54 | + [ |
| 55 | + new("field2", "Hi"), |
| 56 | + new("field3", "World") |
| 57 | + ] |
| 58 | + ); |
| 59 | + |
| 60 | + RedisValue hsetRes3 = db.HashGet("myhash", "field2"); |
| 61 | + Console.WriteLine(hsetRes3); // >>> Hi |
| 62 | + |
| 63 | + RedisValue hsetRes4 = db.HashGet("myhash", "field3"); |
| 64 | + Console.WriteLine(hsetRes4); // >>> World |
| 65 | + |
| 66 | + HashEntry[] hsetRes5 = db.HashGetAll("myhash"); |
| 67 | + Console.WriteLine($"{string.Join(", ", hsetRes5.Select(h => $"{h.Name}: {h.Value}"))}"); |
| 68 | + // >>> field1: Hello, field2: Hi, field3: World |
| 69 | + |
| 70 | + // STEP_END |
| 71 | + Assert.True(hsetRes1); |
| 72 | + Assert.Equal("Hello", hsetRes2); |
| 73 | + Assert.Equal("Hi", hsetRes3); |
| 74 | + Assert.Equal("World", hsetRes4); |
| 75 | + Assert.Equal( |
| 76 | + "field1: Hello, field2: Hi, field3: World", |
| 77 | + string.Join(", ", hsetRes5.Select(h => $"{h.Name}: {h.Value}")) |
| 78 | + ); |
| 79 | + db.KeyDelete("myhash"); |
| 80 | + |
| 81 | + // STEP_START hgetall |
| 82 | + db.HashSet("myhash", |
| 83 | + [ |
| 84 | + new("field1", "Hello"), |
| 85 | + new("field2", "World") |
| 86 | + ] |
| 87 | + ); |
| 88 | + |
| 89 | + HashEntry[] hGetAllResult = db.HashGetAll("myhash"); |
| 90 | + Array.Sort(hGetAllResult, (a1, a2) => a1.Name.CompareTo(a2.Name)); |
| 91 | + Console.WriteLine( |
| 92 | + string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}")) |
| 93 | + ); |
| 94 | + // >>> field1: Hello, field2: World |
| 95 | + // STEP_END |
| 96 | + Assert.Equal("field1: Hello, field2: World", string.Join(", ", hGetAllResult.Select(e => $"{e.Name}: {e.Value}"))); |
| 97 | + db.KeyDelete("myhash"); |
| 98 | + |
| 99 | + // STEP_START hvals |
| 100 | + db.HashSet("myhash", |
| 101 | + [ |
| 102 | + new("field1", "Hello"), |
| 103 | + new("field2", "World") |
| 104 | + ] |
| 105 | + ); |
| 106 | + |
| 107 | + RedisValue[] hValsResult = db.HashValues("myhash"); |
| 108 | + Array.Sort(hValsResult); |
| 109 | + Console.WriteLine(string.Join(", ", hValsResult)); |
| 110 | + // >>> Hello, World |
| 111 | + // STEP_END |
| 112 | + Assert.Equal("Hello, World", string.Join(", ", hValsResult)); |
| 113 | + } |
| 114 | +} |
0 commit comments