forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleExamples.cs
More file actions
121 lines (111 loc) · 2.65 KB
/
Copy pathSimpleExamples.cs
File metadata and controls
121 lines (111 loc) · 2.65 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ServiceStack.Text;
namespace ServiceStack.Redis.Tests.Examples
{
[TestFixture, Explicit, Category("Integration")]
public class SimpleExamples
{
readonly RedisClient redisClient = new RedisClient(TestConfig.SingleHost);
[SetUp]
public void OnBeforeEachTest()
{
redisClient.FlushAll();
}
[Test]
public void Store_and_retrieve_users()
{
using (var redisUsers = redisClient.GetTypedClient<User>())
{
redisUsers.Store(new User { Id = redisUsers.GetNextSequence(), Name = "ayende" });
redisUsers.Store(new User { Id = redisUsers.GetNextSequence(), Name = "mythz" });
var allUsers = redisUsers.GetAll();
Debug.WriteLine(allUsers.Dump());
}
/*Output
[
{
Id: 1,
Name: ayende,
BlogIds: []
},
{
Id: 2,
Name: mythz,
BlogIds: []
}
]
*/
}
[Test]
public void Store_and_retrieve_some_blogs()
{
//Retrieve strongly-typed Redis clients that let's you natively persist POCO's
using (var redisUsers = redisClient.GetTypedClient<User>())
using (var redisBlogs = redisClient.GetTypedClient<Blog>())
{
//Create the user, getting a unique User Id from the User sequence.
var mythz = new User { Id = redisUsers.GetNextSequence(), Name = "Demis Bellot" };
//create some blogs using unique Ids from the Blog sequence. Also adding references
var mythzBlogs = new List<Blog>
{
new Blog
{
Id = redisBlogs.GetNextSequence(),
UserId = mythz.Id,
UserName = mythz.Name,
Tags = new List<string> { "Architecture", ".NET", "Redis" },
},
new Blog
{
Id = redisBlogs.GetNextSequence(),
UserId = mythz.Id,
UserName = mythz.Name,
Tags = new List<string> { "Music", "Twitter", "Life" },
},
};
//Add the blog references
mythzBlogs.ForEach(x => mythz.BlogIds.Add(x.Id));
//Store the user and their blogs
redisUsers.Store(mythz);
redisBlogs.StoreAll(mythzBlogs);
//retrieve all blogs
var blogs = redisBlogs.GetAll();
//Recursively print the values of the POCO (For T.Dump() Extension method see: http://www.servicestack.net/mythz_blog/?p=202)
Debug.WriteLine(blogs.Dump());
}
/*Output
[
{
Id: 1,
UserId: 1,
UserName: Demis Bellot,
Tags:
[
Architecture,
.NET,
Redis
],
BlogPostIds: []
},
{
Id: 2,
UserId: 1,
UserName: Demis Bellot,
Tags:
[
Music,
Twitter,
Life
],
BlogPostIds: []
}
]
*/
}
}
}