Repository navigation
Expand file tree
/
Copy pathPipeTransExample.cs
More file actions
125 lines (104 loc) · 3.6 KB
/
Copy pathPipeTransExample.cs
File metadata and controls
125 lines (104 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// EXAMPLE: pipe_trans_tutorial
using NRedisStack;
using StackExchange.Redis;
//REMOVE_START
using NRedisStack.Tests;
namespace Doc;
[Collection("DocsTests")]
//REMOVE_END
public class PipeTransExample
// REMOVE_START
: AbstractNRedisStackTest, IDisposable
// REMOVE_END
{
// REMOVE_START
public PipeTransExample(EndpointsFixture fixture) : base(fixture) { }
[Fact]
// REMOVE_END
public void Run()
{
//REMOVE_START
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
_ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
//REMOVE_END
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
// REMOVE_START
db.KeyDelete([
"counter:1", "counter:2", "counter:3",
"seat:0", "seat:1", "seat:2", "seat:3", "seat:4",
"customer:39182",
"Details"
]);
// REMOVE_END
// STEP_START basic_pipe
var pipeline = new Pipeline(db);
for (int i = 0; i < 5; i++)
{
_ = pipeline.Db.StringSetAsync($"seat:{i}", $"#{i}");
}
pipeline.Execute();
var resp1 = db.StringGet("seat:0");
Console.WriteLine(resp1); // >>> #0
var resp2 = db.StringGet("seat:3");
Console.WriteLine(resp2); // >>> #3
var resp3 = db.StringGet("seat:4");
Console.WriteLine(resp2); // >>> #4
// STEP_END
// REMOVE_START
Assert.Equal("#0", resp1);
Assert.Equal("#3", resp2);
Assert.Equal("#4", resp3);
// REMOVE_END
// STEP_START basic_trans
var trans = new Transaction(db);
_ = trans.Db.StringIncrementAsync("counter:1", 1);
_ = trans.Db.StringIncrementAsync("counter:2", 2);
_ = trans.Db.StringIncrementAsync("counter:3", 3);
trans.Execute();
var resp4 = db.StringGet("counter:1");
Console.WriteLine(resp4); // >>> 1
var resp5 = db.StringGet("counter:2");
Console.WriteLine(resp5); // >>> 2
var resp6 = db.StringGet("counter:3");
Console.WriteLine(resp6); // >>> 3
// STEP_END
// REMOVE_START
Assert.Equal("1", resp4);
Assert.Equal("2", resp5);
Assert.Equal("3", resp6);
// REMOVE_END
// STEP_START trans_watch
var watchedTrans = new Transaction(db);
watchedTrans.AddCondition(Condition.KeyNotExists("customer:39182"));
_ = watchedTrans.Db.HashSetAsync(
"customer:39182",
[
new("name", "David"),
new("age", "27")
]
);
bool succeeded = watchedTrans.Execute();
Console.WriteLine(succeeded); // >>> true
// STEP_END
// REMOVE_START
Assert.True(succeeded);
// REMOVE_END
// STEP_START when_condition
bool resp7 = db.HashSet("Details", "SerialNumber", "12345");
Console.WriteLine(resp7); // >>> true
db.HashSet("Details", "SerialNumber", "12345A", When.NotExists);
string resp8 = db.HashGet("Details", "SerialNumber")!;
Console.WriteLine(resp8); // >>> 12345
db.HashSet("Details", "SerialNumber", "12345A");
string resp9 = db.HashGet("Details", "SerialNumber")!;
Console.WriteLine(resp9); // >>> 12345A
// STEP_END
// REMOVE_START
Assert.True(resp7);
Assert.Equal("12345", resp8);
Assert.Equal("12345A", resp9);
// REMOVE_END
}
}