|
| 1 | +// EXAMPLE: topk_tutorial |
| 2 | +// HIDE_START |
| 3 | + |
| 4 | +using NRedisStack.RedisStackCommands; |
| 5 | +using NRedisStack.Tests; |
| 6 | +using StackExchange.Redis; |
| 7 | + |
| 8 | +// HIDE_END |
| 9 | + |
| 10 | +// REMOVE_START |
| 11 | +namespace Doc; |
| 12 | +[Collection("DocsTests")] |
| 13 | +// REMOVE_END |
| 14 | + |
| 15 | +// HIDE_START |
| 16 | +public class Topk_tutorial |
| 17 | +{ |
| 18 | + |
| 19 | + [SkipIfRedis(Is.OSSCluster)] |
| 20 | + public void run() |
| 21 | + { |
| 22 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 23 | + var db = muxer.GetDatabase(); |
| 24 | + //REMOVE_START |
| 25 | + // Clear any keys here before using them in tests. |
| 26 | + db.KeyDelete("bikes:keywords"); |
| 27 | + //REMOVE_END |
| 28 | + // HIDE_END |
| 29 | + |
| 30 | + |
| 31 | + // STEP_START topk |
| 32 | + bool res1 = db.TOPK().Reserve("bikes:keywords", 5, 2000, 7, 0.925); |
| 33 | + Console.WriteLine(res1); // >>> True |
| 34 | + |
| 35 | + RedisResult[]? res2 = db.TOPK().Add("bikes:keywords", |
| 36 | + "store", |
| 37 | + "seat", |
| 38 | + "handlebars", |
| 39 | + "handles", |
| 40 | + "pedals", |
| 41 | + "tires", |
| 42 | + "store", |
| 43 | + "seat" |
| 44 | + ); |
| 45 | + |
| 46 | + if (res2 is not null) |
| 47 | + { |
| 48 | + Console.WriteLine(string.Join(", ", string.Join(", ", res2.Select(r => $"{(r.IsNull ? "Null" : r)}")))); |
| 49 | + // >>> Null, Null, Null, Null, Null, handlebars, Null, Null |
| 50 | + } |
| 51 | + |
| 52 | + RedisResult[] res3 = db.TOPK().List("bikes:keywords"); |
| 53 | + |
| 54 | + if (res3 is not null) |
| 55 | + { |
| 56 | + Console.WriteLine(string.Join(", ", string.Join(", ", res3.Select(r => $"{(r.IsNull ? "Null" : r)}")))); |
| 57 | + // >>> store, seat, pedals, tires, handles |
| 58 | + } |
| 59 | + |
| 60 | + bool[] res4 = db.TOPK().Query("bikes:keywords", "store", "handlebars"); |
| 61 | + Console.WriteLine(string.Join(", ", res4)); // >>> True, False |
| 62 | + // STEP_END |
| 63 | + |
| 64 | + // Tests for 'topk' step. |
| 65 | + // REMOVE_START |
| 66 | + Assert.True(res1); |
| 67 | + Assert.NotNull(res2); |
| 68 | + Assert.Equal( |
| 69 | + "Null, Null, Null, Null, Null, handlebars, Null, Null", |
| 70 | + string.Join(", ", string.Join(", ", res2.Select(r => $"{(r.IsNull ? "Null" : r)}"))) |
| 71 | + ); |
| 72 | + Assert.NotNull(res3); |
| 73 | + Assert.Equal( |
| 74 | + "store, seat, pedals, tires, handles", |
| 75 | + string.Join(", ", string.Join(", ", res3.Select(r => $"{(r.IsNull ? "Null" : r)}"))) |
| 76 | + ); |
| 77 | + Assert.Equal("True, False", string.Join(", ", res4)); |
| 78 | + // REMOVE_END |
| 79 | + |
| 80 | + |
| 81 | + // HIDE_START |
| 82 | + } |
| 83 | +} |
| 84 | +// HIDE_END |
| 85 | + |
0 commit comments