public void Store_and_retrieve_some_blogs() { //Retrieve strongly-typed Redis clients that let's you natively persist POCO's var redisUsers = redis.As<User>(); var redisBlogs = redis.As<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) blogs.PrintDump(); /*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: [] } ] */ }
public void InsertTestData() { using (var redisUsers = redisClient.GetTypedClient<User>()) using (var redisBlogs = redisClient.GetTypedClient<Blog>()) using (var redisBlogPosts = redisClient.GetTypedClient<BlogPost>()) { var ayende = new User { Id = redisUsers.GetNextSequence(), Name = "Oren Eini" }; var mythz = new User { Id = redisUsers.GetNextSequence(), Name = "Demis Bellot" }; var ayendeBlog = new Blog { Id = redisBlogs.GetNextSequence(), UserId = ayende.Id, UserName = ayende.Name, Tags = new List<string> { "Architecture", ".NET", "Databases" }, }; var mythzBlog = new Blog { Id = redisBlogs.GetNextSequence(), UserId = mythz.Id, UserName = mythz.Name, Tags = new List<string> { "Architecture", ".NET", "Databases" }, }; var blogPosts = new List<BlogPost> { new BlogPost { Id = redisBlogPosts.GetNextSequence(), BlogId = ayendeBlog.Id, Title = "RavenDB", Categories = new List<string> { "NoSQL", "DocumentDB" }, Tags = new List<string> {"Raven", "NoSQL", "JSON", ".NET"} , Comments = new List<BlogPostComment> { new BlogPostComment { Content = "First Comment!", CreatedDate = DateTime.UtcNow,}, new BlogPostComment { Content = "Second Comment!", CreatedDate = DateTime.UtcNow,}, } }, new BlogPost { Id = redisBlogPosts.GetNextSequence(), BlogId = mythzBlog.Id, Title = "Redis", Categories = new List<string> { "NoSQL", "Cache" }, Tags = new List<string> {"Redis", "NoSQL", "Scalability", "Performance"}, Comments = new List<BlogPostComment> { new BlogPostComment { Content = "First Comment!", CreatedDate = DateTime.UtcNow,} } }, new BlogPost { Id = redisBlogPosts.GetNextSequence(), BlogId = ayendeBlog.Id, Title = "Cassandra", Categories = new List<string> { "NoSQL", "Cluster" }, Tags = new List<string> {"Cassandra", "NoSQL", "Scalability", "Hashing"}, Comments = new List<BlogPostComment> { new BlogPostComment { Content = "First Comment!", CreatedDate = DateTime.UtcNow,} } }, new BlogPost { Id = redisBlogPosts.GetNextSequence(), BlogId = mythzBlog.Id, Title = "Couch Db", Categories = new List<string> { "NoSQL", "DocumentDB" }, Tags = new List<string> {"CouchDb", "NoSQL", "JSON"}, Comments = new List<BlogPostComment> { new BlogPostComment {Content = "First Comment!", CreatedDate = DateTime.UtcNow,} } }, }; ayende.BlogIds.Add(ayendeBlog.Id); ayendeBlog.BlogPostIds.AddRange(blogPosts.Where(x => x.BlogId == ayendeBlog.Id).ConvertAll(x => x.Id)); mythz.BlogIds.Add(mythzBlog.Id); mythzBlog.BlogPostIds.AddRange(blogPosts.Where(x => x.BlogId == mythzBlog.Id).ConvertAll(x => x.Id)); redisUsers.Store(ayende); redisUsers.Store(mythz); redisBlogs.StoreAll(new[] { ayendeBlog, mythzBlog }); redisBlogPosts.StoreAll(blogPosts); } }