forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueTypeExamples.cs
More file actions
138 lines (109 loc) · 3.91 KB
/
Copy pathValueTypeExamples.cs
File metadata and controls
138 lines (109 loc) · 3.91 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
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ServiceStack.Redis.Generic;
namespace ServiceStack.Redis.Tests
{
[TestFixture, Category("Integration")]
public class ValueTypeExamples
{
[SetUp]
public void SetUp()
{
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
redisClient.FlushAll();
}
}
[Test]
public void Working_with_int_values()
{
const string intKey = "intkey";
const int intValue = 1;
//STORING AN INT USING THE BASIC CLIENT
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
redisClient.SetEntry(intKey, intValue.ToString());
string strGetIntValue = redisClient.GetValue(intKey);
int toIntValue = int.Parse(strGetIntValue);
Assert.That(toIntValue, Is.EqualTo(intValue));
}
//STORING AN INT USING THE GENERIC CLIENT
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
//Create a generic client that treats all values as ints:
IRedisTypedClient<int> intRedis = redisClient.GetTypedClient<int>();
intRedis.SetEntry(intKey, intValue);
var toIntValue = intRedis.GetValue(intKey);
Assert.That(toIntValue, Is.EqualTo(intValue));
}
}
[Test]
public void Working_with_int_list_values()
{
const string intListKey = "intListKey";
var intValues = new List<int> { 2, 4, 6, 8 };
//STORING INTS INTO A LIST USING THE BASIC CLIENT
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
IList<string> strList = redisClient.Lists[intListKey];
//storing all int values in the redis list 'intListKey' as strings
intValues.ForEach(x => strList.Add(x.ToString()));
//retrieve all values again as strings
List<string> strListValues = strList.ToList();
//convert back to list of ints
List<int> toIntValues = strListValues.ConvertAll(x => int.Parse(x));
Assert.That(toIntValues, Is.EqualTo(intValues));
//delete all items in the list
strList.Clear();
}
//STORING INTS INTO A LIST USING THE GENERIC CLIENT
using (var redisClient = new RedisClient(TestConfig.SingleHost))
{
//Create a generic client that treats all values as ints:
IRedisTypedClient<int> intRedis = redisClient.GetTypedClient<int>();
IRedisList<int> intList = intRedis.Lists[intListKey];
//storing all int values in the redis list 'intListKey' as ints
intValues.ForEach(x => intList.Add(x));
List<int> toIntListValues = intList.ToList();
Assert.That(toIntListValues, Is.EqualTo(intValues));
}
}
public class IntAndString
{
public int Id { get; set; }
public string Letter { get; set; }
}
[Test]
public void Working_with_Generic_types()
{
using (var redisClient = new RedisClient())
{
//Create a typed Redis client that treats all values as IntAndString:
var typedRedis = redisClient.GetTypedClient<IntAndString>();
var pocoValue = new IntAndString { Id = 1, Letter = "A" };
typedRedis.SetEntry("pocoKey", pocoValue);
IntAndString toPocoValue = typedRedis.GetValue("pocoKey");
Assert.That(toPocoValue.Id, Is.EqualTo(pocoValue.Id));
Assert.That(toPocoValue.Letter, Is.EqualTo(pocoValue.Letter));
var pocoListValues = new List<IntAndString> {
new IntAndString {Id = 2, Letter = "B"},
new IntAndString {Id = 3, Letter = "C"},
new IntAndString {Id = 4, Letter = "D"},
new IntAndString {Id = 5, Letter = "E"},
};
IRedisList<IntAndString> pocoList = typedRedis.Lists["pocoListKey"];
//Adding all IntAndString objects into the redis list 'pocoListKey'
pocoListValues.ForEach(x => pocoList.Add(x));
List<IntAndString> toPocoListValues = pocoList.ToList();
for (var i = 0; i < pocoListValues.Count; i++)
{
pocoValue = pocoListValues[i];
toPocoValue = toPocoListValues[i];
Assert.That(toPocoValue.Id, Is.EqualTo(pocoValue.Id));
Assert.That(toPocoValue.Letter, Is.EqualTo(pocoValue.Letter));
}
}
}
}
}