Skip to content

Commit 9164439

Browse files
committed
Add latest libs to use IRedisClientsManager new extension methods for easy access of a redisTypedClient
1 parent 0672ce4 commit 9164439

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

Lib

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

libs/ServiceStack.dll

6 KB
Binary file not shown.

src/ServiceStack.Questions/ServiceStack.Questions.ServiceInterface/IRepository.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using ServiceStack.Common.Extensions;
@@ -44,63 +45,59 @@ public Repository(IRedisClientsManager redisManager)
4445
public User GetOrCreateUser(User user)
4546
{
4647
if (user.DisplayName.IsNullOrEmpty())
47-
throw new ArgumentNullException("user.DisplayName");
48+
throw new ArgumentNullException("DisplayName");
4849

49-
var userIdNameMapKey = "id:User:DisplayName:" + user.DisplayName.ToLower();
50+
var userIdAliasKey = "id:User:DisplayName:" + user.DisplayName.ToLower();
5051

5152
using (var redis = RedisManager.GetClient())
5253
using (var redisUsers = redis.GetTypedClient<User>())
5354
{
54-
var userKey = redis.GetValue(userIdNameMapKey);
55+
//Find user by DisplayName
56+
var userKey = redis.GetValue(userIdAliasKey);
5557
if (userKey != null)
5658
return redisUsers.GetValue(userKey);
5759

60+
//Generate Id for New User
5861
if (user.Id != default(long))
5962
user.Id = redisUsers.GetNextSequence();
6063

64+
//Create or Update New User
6165
redisUsers.Store(user);
62-
redis.SetEntry(userIdNameMapKey, user.CreateUrn());
66+
//Save reference to User key using the DisplayName alias
67+
redis.SetEntry(userIdAliasKey, user.CreateUrn());
6368

69+
//Retrieve the User by Id
6470
return redisUsers.GetById(user.Id);
6571
}
6672
}
6773

68-
//Reduce the boilerplate for common access
69-
T ExecRedisQuestions<T>(Func<IRedisTypedClient<Question>, T> lamda)
70-
{
71-
using (var redis = RedisManager.GetClient())
72-
using (var redisQuestions = redis.GetTypedClient<Question>())
73-
{
74-
return lamda(redisQuestions);
75-
}
76-
}
77-
7874
public List<Question> GetAllQuestions()
7975
{
80-
return ExecRedisQuestions(q => q.GetAll().ToList());
76+
//Use 'Exec<T>' extension method to easy access to: 'redis.GetTypedClient<Question>()'
77+
return RedisManager.Exec<Question>(q => q.GetAll()).ToList();
8178
}
8279

8380
public List<Question> GetRecentQuestions(int skip, int take)
8481
{
85-
return ExecRedisQuestions(q => q.GetLatestFromRecentsList(skip, take));
82+
//Use 'Exec<T>' extension method to easy access to: 'redis.GetTypedClient<Question>()'
83+
return RedisManager.Exec<Question>(q => q.GetLatestFromRecentsList(skip, take));
8684
}
8785

8886
public void StoreQuestion(Question question)
8987
{
90-
using (var redis = RedisManager.GetClient())
91-
using (var redisQuestions = redis.GetTypedClient<Question>())
88+
RedisManager.Exec<Question>(redisQuestions =>
9289
{
9390
if (question.Id == default(long))
9491
question.Id = redisQuestions.GetNextSequence();
9592

9693
redisQuestions.Store(question);
9794
redisQuestions.AddToRecentsList(question);
98-
}
95+
});
9996
}
10097

10198
public void StoreAnswer(Answer answer)
10299
{
103-
using (var redis = RedisManager.GetReadOnlyClient())
100+
using (var redis = RedisManager.GetClient())
104101
using (var redisQuestions = redis.GetTypedClient<Question>())
105102
using (var redisAnswers = redis.GetTypedClient<Answer>())
106103
{
@@ -114,7 +111,11 @@ public void StoreAnswer(Answer answer)
114111

115112
public List<Answer> GetAnswersForQuestion(long questionId)
116113
{
117-
return ExecRedisQuestions(q => q.GetRelatedEntities<Answer>(questionId));
114+
using (var redis = RedisManager.GetClient())
115+
using (var redisQuestions = redis.GetTypedClient<Question>())
116+
{
117+
return redisQuestions.GetRelatedEntities<Answer>(questionId);
118+
}
118119
}
119120

120121
public void VoteQuestionUp(long userId, long questionId)

src/ServiceStack.Questions/ServiceStack.Questions.ServiceInterface/UserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Runtime.Serialization;
23
using ServiceStack.ServiceInterface;
34

0 commit comments

Comments
 (0)