-
Notifications
You must be signed in to change notification settings - Fork 129
/
CBRedis.cs
278 lines (218 loc) · 8.72 KB
/
CBRedis.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @file CBRedis.cs
* @brief Processing CloudBread redis cache related task. \n
* @author Dae Woo Kim
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using CloudBread.globals;
using StackExchange.Redis;
using Newtonsoft.Json;
using Microsoft.Practices.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
//using Logger.Logging;
namespace CloudBreadRedis
{
public class CBRedis
{
// compose connection string for service
static string redisConnectionStringSocket = globalVal.CloudBreadSocketRedisServer;
static string redisConnectionStringRank = globalVal.CloudBreadRankRedisServer;
static string CloudBreadGameLogRedisServer = globalVal.CloudBreadGameLogRedisServer;
/// @brief save socket auth key in redis db0
public static bool SetRedisKey(string key, string value, int? expTimeMin) // todo: value as oject or ...?
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringSocket);
// try to connect database
try
{
// StringSet task
IDatabase cache = connection.GetDatabase(0);
if (expTimeMin == null)
{
// save without expire time
cache.StringSet(key, value);
}
else
{
cache.StringSet(key, value, TimeSpan.FromMinutes(Convert.ToDouble(expTimeMin)));
}
connection.Close();
connection.Dispose();
return true;
}
catch (Exception)
{
throw;
}
}
/// @brief get socket auth key redis data by key value
public static string GetRedisKeyValue(string key)
{
string result = "";
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringSocket);
// try to connect database
try
{
// StringGet task
IDatabase cache = connection.GetDatabase(0);
result = cache.StringGet(key);
connection.Close();
connection.Dispose();
return result;
}
catch (Exception)
{
throw;
}
}
/// @brief Set point value at Redis sorted set
public static bool SetSortedSetRank(string sid, double point)
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringRank);
try
{
IDatabase cache = connection.GetDatabase(1);
cache.SortedSetAdd(globalVal.CloudBreadRankSortedSet, sid, point);
connection.Close();
connection.Dispose();
}
catch (Exception)
{
throw;
}
return true;
}
/// @brief Get rank value from Redis sorted set
public static long GetSortedSetRank(string sid)
{
long rank = 0;
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringRank);
try
{
IDatabase cache = connection.GetDatabase(1);
rank = cache.SortedSetRank(globalVal.CloudBreadRankSortedSet, sid, Order.Descending) ?? 0;
connection.Close();
connection.Dispose();
}
catch (Exception)
{
throw;
}
return rank;
}
/// @brief Get selected rank range members.
/// Get my rank and then call this method to fetch +-10 rank(total 20) rank
public static SortedSetEntry[] GetSortedSetRankByRange(long startRank, long endRank)
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringRank);
try
{
IDatabase cache = connection.GetDatabase(1);
//SortedSetEntry[] rank = cache.SortedSetRangeByScoreWithScores(globalVal.CloudBreadRankSortedSet, startRank, endRank, Exclude.None, Order.Descending);
SortedSetEntry[] se = cache.SortedSetRangeByRankWithScores(globalVal.CloudBreadRankSortedSet, startRank, endRank, Order.Descending);
//return JsonConvert.SerializeObject(se);
connection.Close();
connection.Dispose();
return se;
}
catch (Exception)
{
throw;
}
}
/// @brief Get top rank point and info from Redis sorted set
public static SortedSetEntry[] GetTopSortedSetRank(int countNumber)
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringRank);
try
{
IDatabase cache = connection.GetDatabase(1);
SortedSetEntry[] sse = cache.SortedSetRangeByScoreWithScores(globalVal.CloudBreadRankSortedSet, order: Order.Descending, take: countNumber);
connection.Close();
connection.Dispose();
return sse;
}
catch (Exception)
{
throw;
}
}
/// fill out all rank redis cache from db
/// @todo: huge amount of data processing - split 10,000 or ...
/// dt.Rows check. if bigger than 10,000, seperate as another loop
/// dt.Rows / 10,000 = mod value + 1 = loop count...........
/// call count query first and then paging processing at query side to prevent DB throttling?
public static bool FillAllRankFromDB()
{
try
{
// redis connection
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnectionStringRank);
IDatabase cache = connection.GetDatabase(1);
// delete rank sorted set - caution. this process remove all rank set data
cache.KeyDelete(globalVal.CloudBreadRankSortedSet);
// data table fill for easy count number
RetryPolicy retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(globalVal.conRetryCount, TimeSpan.FromSeconds(globalVal.conRetryFromSeconds));
SqlConnection conn = new SqlConnection(globalVal.DBConnectionString);
conn.Open();
string strQuery = "SELECT Members.Name1, MemberGameInfoes.Points FROM Members inner join MemberGameInfoes on Members.MemberID = MemberGameInfoes.MemberID";
SqlCommand command = new SqlCommand(strQuery, conn);
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(command))
{
da.Fill(dt);
}
/// make SortedSetEntry to fill out
SortedSetEntry[] sse = new SortedSetEntry[dt.Rows.Count];
Int64 i = 0;
foreach (DataRow dr in dt.Rows)
{
// fill rank row to redis struct array
sse[i] = new SortedSetEntry(dr[0].ToString(), Int64.Parse(dr[1].ToString()));
i++;
}
// fill out all rank data
cache.SortedSetAdd(globalVal.CloudBreadRankSortedSet, sse);
connection.Close();
connection.Dispose();
return true;
}
catch (Exception)
{
throw;
}
}
/// save log to redis db2 and keep 365 days
public static void saveRedisLog(string key, string message, int? expTimeDays)
{
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(CloudBreadGameLogRedisServer);
// try to connect database
try
{
// StringSet task
IDatabase cache = connection.GetDatabase(2);
if (expTimeDays == null)
{
// save without expire time
cache.StringSetAsync(key, message);
}
else
{
cache.StringSetAsync(key, message, TimeSpan.FromDays(Convert.ToDouble(expTimeDays)));
}
connection.Close();
connection.Dispose();
}
catch (Exception)
{
throw;
}
}
}
}