Browse Source

缓存换成redis

zhoufan 4 years ago
parent
commit
f8c8d5227c

+ 4 - 1
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/App_Start/AuthorizeAttribute.cs

@@ -13,6 +13,7 @@ namespace CallCenterApi.Interface
13 13
     public class AuthorityAttribute : AuthorizeAttribute
14 14
     {
15 15
         private BLL.T_Sys_RoleFunction roleFunctionBLL = new BLL.T_Sys_RoleFunction();
16
+        private BLL.T_Sys_UserAccount uaBll = new BLL.T_Sys_UserAccount();
16 17
         /// <summary>
17 18
         /// 权限验证
18 19
         /// </summary>
@@ -29,7 +30,9 @@ namespace CallCenterApi.Interface
29 30
                 var action = actionDescriptor.ActionName;
30 31
                 var token = filterContext.HttpContext.Request["token"];
31 32
 
32
-                var userData = CacheHelper.Get<Cache.Models.CurrentUserInfo>(token);
33
+                //var userData = CacheHelper.Get<Cache.Models.CurrentUserInfo>(token);
34
+                var userCode= RedisHelper.StringGet(CommonHelper.MD5(token));
35
+                var userData = uaBll.GetModel(userCode.ToString());
33 36
                 var roleId = userData.F_RoleId;
34 37
                 var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
35 38
                 if (role != null)

+ 9 - 3
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/Login/LoginController.cs

@@ -71,8 +71,12 @@ namespace CallCenterApi.Interface.Controllers
71 71
                     };
72 72
                     var token = FormsPrincipal<Cache.Models.CurrentUserInfo>.GetCookieValue(currentUserInfo.F_UserCode, currentUserInfo);
73 73
 
74
-                    //放入缓存
75
-                    CacheHelper.Insert(token, currentUserInfo, 1440*7);
74
+                    ////放入缓存
75
+                    //CacheHelper.Insert(token, currentUserInfo, 1440*7);
76
+
77
+                    //放入redis缓存
78
+                    RedisHelper.StringSet(CommonHelper.MD5(token), currentUserInfo.F_UserCode, new TimeSpan(336, 0, 0));
79
+
76 80
                     return Success("登录成功", new
77 81
                     {
78 82
                         token = token
@@ -123,7 +127,9 @@ namespace CallCenterApi.Interface.Controllers
123 127
                     F_Remark = "",
124 128
                     F_State = 0
125 129
                 });
126
-                CacheHelper.Remove(token);
130
+                //CacheHelper.Remove(token);
131
+
132
+                RedisHelper.KeyDelete(CommonHelper.MD5(token));
127 133
             }
128 134
             return Success("退出成功");
129 135
         }

+ 7 - 4
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Global.asax.cs

@@ -1,4 +1,5 @@
1
-using CallCenterApi.Common;
1
+using CallCenter.Utility;
2
+using CallCenterApi.Common;
2 3
 using CallCenterApi.Interface.Models.Common;
3 4
 using System;
4 5
 using System.Collections.Generic;
@@ -31,14 +32,16 @@ namespace CallCenterApi.Interface
31 32
             if (string.IsNullOrWhiteSpace(token)) return;
32 33
             try
33 34
             {
34
-                //获取缓存
35
-                var dict = CacheHelper.Get(token);
35
+                ////获取缓存
36
+                //var dict = CacheHelper.Get(token);
37
+                //获取redis缓存
38
+                var dict = RedisHelper.StringGet(CommonHelper.MD5(token));
36 39
                 if (dict == null) return;
37 40
                 Cache.Models.CurrentUserInfo userData = null;
38 41
                 //获取FormsAuthenticationTicket对象
39 42
                 FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
40 43
                 if (ticket != null && string.IsNullOrEmpty(ticket.UserData) == false)
41
-                    userData = (new JavaScriptSerializer()).Deserialize<Cache.Models.CurrentUserInfo>(ticket.UserData); //还原用户数据
44
+                    userData =ticket.UserData.ToObject<Cache.Models.CurrentUserInfo>(); //还原用户数据
42 45
                 if (ticket != null && userData != null)
43 46
                     context.User = new FormsPrincipal<Cache.Models.CurrentUserInfo> (ticket, userData);//重新给context.User赋值。
44 47
             }

+ 5 - 0
codegit/CallCenterCommon/CallCenter.Utility/CallCenter.Utility.csproj

@@ -54,10 +54,14 @@
54 54
       <HintPath>..\..\packages\NPOI.2.3.0\lib\net40\NPOI.OpenXmlFormats.dll</HintPath>
55 55
       <Private>True</Private>
56 56
     </Reference>
57
+    <Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
58
+      <HintPath>..\..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll</HintPath>
59
+    </Reference>
57 60
     <Reference Include="System" />
58 61
     <Reference Include="System.Configuration" />
59 62
     <Reference Include="System.Core" />
60 63
     <Reference Include="System.Drawing" />
64
+    <Reference Include="System.IO.Compression" />
61 65
     <Reference Include="System.Management" />
62 66
     <Reference Include="System.Web" />
63 67
     <Reference Include="System.Xml.Linq" />
@@ -77,6 +81,7 @@
77 81
     <Compile Include="Extend\ExtLinq.cs" />
78 82
     <Compile Include="FileUp.cs" />
79 83
     <Compile Include="Linq\PredicateExtensionses.cs" />
84
+    <Compile Include="RedisHelper.cs" />
80 85
     <Compile Include="SaltAndHashHelper.cs" />
81 86
     <Compile Include="SysInformationHelper.cs" />
82 87
     <Compile Include="Time\DateTimeConvert.cs" />

+ 801 - 0
codegit/CallCenterCommon/CallCenter.Utility/RedisHelper.cs

@@ -0,0 +1,801 @@
1
+using System;
2
+using StackExchange.Redis;
3
+using System.Collections.Generic;
4
+using System.Linq;
5
+using Newtonsoft.Json;
6
+using System.Collections;
7
+
8
+namespace CallCenter.Utility
9
+{
10
+    public class RedisHelper
11
+    {
12
+        #region 
13
+        //执行顺序---静态字段---静态构造函数---构造函数
14
+        private static ConnectionMultiplexer redis;
15
+        private static IDatabase database;
16
+        static RedisHelper()
17
+        {
18
+            if (redis == null || !redis.IsConnected)
19
+            {
20
+                redis = ConnectionMultiplexer.Connect(Configs.GetValue("Redis_Server")+":"+Configs.GetValue("Redis_Port"));
21
+                //redis = ConnectionMultiplexer.Connect("192.168.4.18, abortConnect=false");
22
+
23
+                database = redis.GetDatabase();
24
+            }
25
+        }
26
+
27
+        #endregion
28
+
29
+        #region redis 字符串(string)操作
30
+
31
+        /// <summary>
32
+        /// 设置指定键的值
33
+        /// </summary>
34
+        /// <param name="key"></param>
35
+        /// <param name="value"></param>
36
+        /// <returns></returns>
37
+        public static bool StringSet(string key, string value)
38
+        {
39
+            return database.StringSet(key, value);
40
+        }
41
+
42
+        /// <summary>
43
+        /// 获取指定键的值
44
+        /// </summary>
45
+        /// <param name="key"></param>
46
+        /// <returns></returns>
47
+        public static object StringGet(string key)
48
+        {
49
+            if (string.IsNullOrEmpty(key))
50
+            {
51
+                return null;
52
+
53
+            }
54
+            else
55
+            {
56
+                var rt = database.StringGet(key);
57
+
58
+                if (rt.IsNull)
59
+                {
60
+                    return null ;
61
+                }
62
+                else
63
+                {
64
+                    return rt;
65
+                }
66
+            }
67
+          
68
+        }
69
+
70
+        /// <summary>
71
+        /// 获取存储在键上的字符串的子字符串
72
+        /// </summary>
73
+        /// <param name="key"></param>
74
+        /// <param name="start"></param>
75
+        /// <param name="end"></param>
76
+        /// <returns></returns>
77
+        public static object StringGet(string key, int start, int end)
78
+        {
79
+            var rt = database.StringGetRange(key, start, end);
80
+            if (rt.IsNull)
81
+            {
82
+                return null;
83
+            }
84
+            else
85
+            {
86
+                return rt;
87
+            }
88
+        }
89
+
90
+        /// <summary>
91
+        /// 设置键的字符串值并返回其旧值
92
+        /// </summary>
93
+        /// <param name="key"></param>
94
+        /// <param name="value"></param>
95
+        /// <returns></returns>
96
+        public static object StringGetAndSet(string key, string value)
97
+        {
98
+            var rt = database.StringGetSet(key, value);
99
+            if (rt.IsNull)
100
+            {
101
+                return null;
102
+            }
103
+            else
104
+            {
105
+                return rt;
106
+            }
107
+        }
108
+
109
+        /// <summary>
110
+        /// 返回在键处存储的字符串值中偏移处的位值
111
+        /// </summary>
112
+        /// <param name="key"></param>
113
+        /// <param name="offset"></param>
114
+        /// <returns></returns>
115
+        public static bool StringGetBit(string key, long offset)
116
+        {
117
+            return database.StringGetBit(key, offset);
118
+        }
119
+
120
+        /// <summary>
121
+        /// 获取所有给定键的值
122
+        /// </summary>
123
+        /// <param name="keys"></param>
124
+        /// <returns></returns>
125
+        public static List<object> StringMultiGet(string[] keys)
126
+        {
127
+            List<object> list = new List<object>();
128
+            for (int i = 0; i < keys.Length; i++)
129
+            {
130
+                list.Add(database.StringGet(keys[i]));
131
+            }
132
+            return list;
133
+        }
134
+
135
+        /// <summary>
136
+        /// 存储在键上的字符串值中设置或清除偏移处的位
137
+        /// </summary>
138
+        /// <param name="key"></param>
139
+        /// <param name="offset"></param>
140
+        /// <param name="value"></param>
141
+        /// <returns></returns>
142
+        public static bool StringSetBit(string key, long offset)
143
+        {
144
+            return database.StringSetBit(key, offset, true);
145
+        }
146
+
147
+        /// <summary>
148
+        /// 使用键和到期时间来设置值
149
+        /// </summary>
150
+        /// <param name="key"></param>
151
+        /// <param name="value"></param>
152
+        /// <param name="expiry"></param>
153
+        /// <returns></returns>
154
+        public static bool StringSet(string key, string value, TimeSpan expiry)
155
+        {
156
+            return database.StringSet(key, value, expiry);
157
+        }
158
+
159
+        /// <summary>
160
+        /// 设置键的值,仅当键不存在时
161
+        /// </summary>
162
+        /// <param name="key"></param>
163
+        /// <param name="value"></param>
164
+        /// <returns></returns>
165
+        public static void StringSetIfAbsent(string key, string value)
166
+        {
167
+            if (database.StringGet(key) == RedisValue.Null)
168
+            {
169
+                database.StringSet(key, value);
170
+            }
171
+        }
172
+
173
+        /// <summary>
174
+        /// 在指定偏移处开始的键处覆盖字符串的一部分
175
+        /// </summary>
176
+        /// <param name="key">键值</param>
177
+        /// <param name="value">值</param>
178
+        /// <param name="offset">偏移量</param>
179
+        /// <returns></returns>
180
+        public static object StringSet(string key, long offset, string value)
181
+        {
182
+            return database.StringSetRange(key, offset, value);
183
+        }
184
+
185
+        /// <summary>
186
+        /// 获取存储在键中的值的长度
187
+        /// </summary>
188
+        /// <param name="key">键值</param>
189
+        /// <returns></returns>
190
+        public static long StringSize(string key)
191
+        {
192
+            return database.StringLength(key);
193
+        }
194
+
195
+        /// <summary>
196
+        /// 为多个键分别设置它们的值
197
+        /// </summary>
198
+        /// <param name="keys"></param>
199
+        /// <returns></returns>
200
+        public static void StringMultiSet(Dictionary<string, string> dic)
201
+        {
202
+            foreach (KeyValuePair<string, string> key in dic)
203
+            {
204
+                database.StringSet(key.Key, key.Value);
205
+            }
206
+        }
207
+
208
+        /// <summary>
209
+        /// 为多个键分别设置它们的值,仅当键不存在时
210
+        /// </summary>
211
+        /// <param name="keys">键值集合</param>
212
+        /// <returns></returns>
213
+        public static void StringMultiSetIfAbsent(Dictionary<string, string> dic)
214
+        {
215
+            foreach (KeyValuePair<string, string> key in dic)
216
+            {
217
+                //判断键值是否存在
218
+                if (database.StringGet(key.Key) == RedisValue.Null)
219
+                {
220
+                    database.StringSet(key.Key, key.Value);
221
+                }
222
+            }
223
+        }
224
+
225
+        /// <summary>
226
+        /// 将键的整数值按给定的数值增加
227
+        /// </summary>
228
+        /// <param name="key">键值</param>
229
+        /// <param name="value">给定的数值</param>
230
+        /// <returns></returns>
231
+        public static double StringIncrement(string key, double value)
232
+        {
233
+            return database.StringIncrement(key, value);
234
+        }
235
+
236
+        /// <summary>
237
+        /// 在key键对应值的右面追加值value
238
+        /// </summary>
239
+        /// <param name="key"></param>
240
+        /// <param name="value"></param>
241
+        /// <returns></returns>
242
+        public static long StringAppend(string key, string value)
243
+        {
244
+            return database.StringAppend(key, value);
245
+        }
246
+
247
+        /// <summary>
248
+        /// 删除某个键值
249
+        /// </summary>
250
+        /// <param name="key"></param>
251
+        /// <returns></returns>
252
+        public static bool StringDelete(string key)
253
+        {
254
+            return false;
255
+        }
256
+
257
+        #endregion
258
+
259
+        #region redis 哈希/散列/字典(Hash)操作
260
+
261
+        /// <summary>
262
+        /// 删除指定的哈希字段
263
+        /// </summary>
264
+        /// <param name="key"></param>
265
+        /// <param name="field"></param>
266
+        /// <returns></returns>
267
+        public static bool HashDelete(string key, string field)
268
+        {
269
+            return database.HashDelete(key, field);
270
+        }
271
+
272
+        /// <summary>
273
+        /// 判断是否存在散列字段
274
+        /// </summary>
275
+        /// <param name=""></param>
276
+        /// <param name=""></param>
277
+        /// <returns></returns>
278
+        public static bool HashHasKey(string key, string field)
279
+        {
280
+            return database.HashExists(key, field);
281
+        }
282
+
283
+        /// <summary>
284
+        /// 获取存储在指定键的哈希字段的值
285
+        /// </summary>
286
+        /// <param name="key"></param>
287
+        /// <param name="field"></param>
288
+        /// <returns></returns>
289
+        public static object HashGet(string key, string field)
290
+        {
291
+            return database.HashGet(key, field);
292
+        }
293
+
294
+        /// <summary>
295
+        /// 获取存储在指定键的哈希中的所有字段和值
296
+        /// </summary>
297
+        /// <param name="key"></param>
298
+        /// <returns></returns>
299
+        public static Dictionary<string, object> HashGetAll(string key)
300
+        {
301
+            Dictionary<string, object> dic = new Dictionary<string, object>();
302
+            var collection = database.HashGetAll(key);
303
+            foreach (var item in collection)
304
+            {
305
+                dic.Add(item.Name, item.Value);
306
+            }
307
+            return dic;
308
+        }
309
+
310
+        /// <summary>
311
+        /// 将哈希字段的浮点值按给定数值增加
312
+        /// </summary>
313
+        /// <param name="key"></param>
314
+        /// <param name="field"></param>
315
+        /// <param name="value">给定的数值</param>
316
+        /// <returns></returns>
317
+        public static double HashIncrement(string key, string field, double value)
318
+        {
319
+            return database.HashIncrement(key, field, value);
320
+        }
321
+
322
+        /// <summary>
323
+        /// 获取哈希中的所有字段
324
+        /// </summary>
325
+        /// <param name="key"></param>
326
+        /// <returns></returns>
327
+        public static string[] HashKeys(string key)
328
+        {
329
+            return database.HashKeys(key).ToStringArray();
330
+        }
331
+
332
+        /// <summary>
333
+        /// 获取散列中的字段数量
334
+        /// </summary>
335
+        /// <param name="key"></param>
336
+        /// <returns></returns>
337
+        public static long HashSize(string key)
338
+        {
339
+            return database.HashLength(key);
340
+        }
341
+
342
+        /// <summary>
343
+        /// 获取所有给定哈希字段的值
344
+        /// </summary>
345
+        /// <param name="key"></param>
346
+        /// <param name="hashKeys"></param>
347
+        /// <returns></returns>
348
+        public static List<object> HashMultiGet(string key, List<string> hashKeys)
349
+        {
350
+            List<object> result = new List<object>();
351
+            foreach (string field in hashKeys)
352
+            {
353
+                result.Add(database.HashGet(key, field));
354
+            }
355
+            return result;
356
+        }
357
+
358
+        /// <summary>
359
+        /// 为多个哈希字段分别设置它们的值
360
+        /// </summary>
361
+        /// <param name="key"></param>
362
+        /// <param name="dic"></param>
363
+        /// <returns></returns>
364
+        public static void HashPutAll(string key, Dictionary<string, string> dic)
365
+        {
366
+            List<HashEntry> list = new List<HashEntry>();
367
+            for (int i = 0; i < dic.Count; i++)
368
+            {
369
+                KeyValuePair<string, string> param = dic.ElementAt(i);
370
+                list.Add(new HashEntry(param.Key, param.Value));
371
+            }
372
+            database.HashSet(key, list.ToArray());
373
+        }
374
+
375
+        /// <summary>
376
+        /// 设置散列字段的字符串值
377
+        /// </summary>
378
+        /// <param name="key"></param>
379
+        /// <param name="field"></param>
380
+        /// <param name="value"></param>
381
+        /// <returns></returns>
382
+        public static void HashPut(string key, string field, string value)
383
+        {
384
+            database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
385
+        }
386
+
387
+        /// <summary>
388
+        /// 仅当字段不存在时,才设置散列字段的值
389
+        /// </summary>
390
+        /// <param name="key"></param>
391
+        /// <param name="fiels"></param>
392
+        /// <param name="value"></param>
393
+        /// <returns></returns>
394
+        public static void HashPutIfAbsent(string key, string field, string value)
395
+        {
396
+            if (!HashHasKey(key, field))
397
+            {
398
+                database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
399
+            }
400
+        }
401
+
402
+        /// <summary>
403
+        /// 获取哈希中的所有值
404
+        /// </summary>
405
+        /// <param name="key"></param>
406
+        /// <returns></returns>
407
+        public static string[] HashValues(string key)
408
+        {
409
+            return database.HashValues(key).ToStringArray();
410
+        }
411
+
412
+        /// <summary>
413
+        /// redis中获取指定键的值并返回对象
414
+        /// </summary>
415
+        /// <typeparam name="T"></typeparam>
416
+        /// <param name="key"></param>
417
+        /// <returns></returns>
418
+        public static T GetHashValue<T>(string key)
419
+        {
420
+            HashEntry[] array = database.HashGetAll(key);
421
+            Dictionary<string, object> dic = new Dictionary<string, object>();
422
+            for (int i = 0; i < array.Length; i++)
423
+            {
424
+                dic.Add(array[i].Name, array[i].Value);
425
+            }
426
+            if (dic.Count > 0)
427
+            {
428
+                string strJson = JsonConvert.SerializeObject(dic);
429
+                return JsonConvert.DeserializeObject<T>(strJson);
430
+            }
431
+            else
432
+            {
433
+                return default(T);
434
+            }
435
+        }
436
+
437
+        /// <summary>
438
+        /// 把指定对象存储在键值为key的redis中
439
+        /// </summary>
440
+        /// <typeparam name="T"></typeparam>
441
+        /// <param name="t"></param>
442
+        /// <param name="key"></param>
443
+        public static void SetHashValue<T>(T t, string key)
444
+        {
445
+            string strJson = JsonConvert.SerializeObject(t);
446
+            Dictionary<string, string> param = JsonConvert.DeserializeObject<Dictionary<string, string>>(strJson);
447
+            HashPutAll(key, param);
448
+        }
449
+
450
+        #endregion
451
+
452
+        #region redis 列表(List)操作
453
+
454
+        /// <summary>
455
+        /// 从左向右存压栈
456
+        /// </summary>
457
+        /// <param name="key"></param>
458
+        /// <param name="value"></param>
459
+        /// <returns></returns>
460
+        public static long ListLeftPush(string key, string value)
461
+        {
462
+            return database.ListLeftPush(key, value);
463
+        }
464
+
465
+        /// <summary>
466
+        /// 从左出栈
467
+        /// </summary>
468
+        /// <param name="key"></param>
469
+        /// <returns></returns>
470
+        public static object ListLeftPop(string key)
471
+        {
472
+            return database.ListLeftPop(key);
473
+        }
474
+
475
+        /// <summary>
476
+        /// 队/栈长
477
+        /// </summary>
478
+        /// <param name="key"></param>
479
+        /// <returns></returns>
480
+        public static long ListSize(string key)
481
+        {
482
+            return database.ListLength(key);
483
+        }
484
+
485
+        /// <summary>
486
+        /// 范围检索,返回List
487
+        /// </summary>
488
+        /// <param name="key"></param>
489
+        /// <param name="start"></param>
490
+        /// <param name="end"></param>
491
+        /// <returns></returns>
492
+        public static string[] ListRange(string key, int start, int end)
493
+        {
494
+            return database.ListRange(key, start, end).ToStringArray();
495
+        }
496
+
497
+        /// <summary>
498
+        /// 移除key中值为value的i个,返回删除的个数;如果没有这个元素则返回0 
499
+        /// </summary>
500
+        /// <param name="key"></param>
501
+        /// <param name="i"></param>
502
+        /// <param name="value"></param>
503
+        /// <returns></returns>
504
+        public static long ListRemove(string key, string value)
505
+        {
506
+            return database.ListRemove(key, value);
507
+        }
508
+
509
+        /// <summary>
510
+        /// 检索
511
+        /// </summary>
512
+        /// <param name="key"></param>
513
+        /// <param name="index"></param>
514
+        /// <returns></returns>
515
+        public static object ListIndex(string key, long index)
516
+        {
517
+            return database.ListGetByIndex(key, index);
518
+        }
519
+
520
+        /// <summary>
521
+        /// 赋值
522
+        /// </summary>
523
+        /// <param name="key"></param>
524
+        /// <param name="index"></param>
525
+        /// <param name="value"></param>
526
+        /// <returns></returns>
527
+        public static void ListSet(string key, int index, string value)
528
+        {
529
+            database.ListSetByIndex(key, index, value);
530
+        }
531
+
532
+        /// <summary>
533
+        /// 裁剪,删除除了[start,end]以外的所有元素 
534
+        /// </summary>
535
+        /// <param name="key"></param>
536
+        /// <param name="start"></param>
537
+        /// <param name="end"></param>
538
+        /// <returns></returns>
539
+        public static void ListTrim(string key, int start, int end)
540
+        {
541
+            database.ListTrim(key, start, end);
542
+        }
543
+
544
+        /// <summary>
545
+        /// 将源key的队列的右边的一个值删除,然后塞入目标key的队列的左边,返回这个值
546
+        /// </summary>
547
+        /// <param name="sourceKey"></param>
548
+        /// <param name="destinationKey"></param>
549
+        /// <returns></returns>
550
+        public static object ListRightPopAndLeftPush(string sourceKey, string destinationKey)
551
+        {
552
+            return database.ListRightPopLeftPush(sourceKey, destinationKey);
553
+        }
554
+
555
+        #endregion
556
+
557
+        #region redis 集合(Set)操作
558
+
559
+        /// <summary>
560
+        /// 集合添加元素
561
+        /// </summary>
562
+        /// <param name="key"></param>
563
+        /// <param name="value"></param>
564
+        public static void SetAdd(string key, string value)
565
+        {
566
+            database.SetAdd(key, value);
567
+        }
568
+
569
+        /// <summary>
570
+        /// 集合组合操作
571
+        /// </summary>
572
+        /// <param name="point">操作标示:0--并集;1--交集;2--差集</param>
573
+        /// <param name="firstKey">第一个集合的键值</param>
574
+        /// <param name="secondKey">第二个集合的键值</param>
575
+        public static string[] SetCombine(int point, string firstKey, string secondKey)
576
+        {
577
+            RedisValue[] array;
578
+            switch (point)
579
+            {
580
+                case 0:
581
+                    array = database.SetCombine(SetOperation.Union, firstKey, secondKey);
582
+                    break;
583
+                case 1:
584
+                    array = database.SetCombine(SetOperation.Intersect, firstKey, secondKey);
585
+                    break;
586
+                case 2:
587
+                    array = database.SetCombine(SetOperation.Difference, firstKey, secondKey);
588
+                    break;
589
+                default:
590
+                    array = new RedisValue[0];
591
+                    break;
592
+            }
593
+            return array.ToStringArray();
594
+        }
595
+
596
+        /// <summary>
597
+        /// 
598
+        /// </summary>
599
+        /// <param name="key"></param>
600
+        /// <param name="value"></param>
601
+        /// <returns></returns>
602
+        public static bool SetContains(string key, string value)
603
+        {
604
+            return database.SetContains(key, value);
605
+        }
606
+
607
+        /// <summary>
608
+        /// 返回对应键值集合的长度
609
+        /// </summary>
610
+        /// <param name="key"></param>
611
+        /// <returns></returns>
612
+        public static long SetLength(string key)
613
+        {
614
+            return database.SetLength(key);
615
+        }
616
+
617
+        /// <summary>
618
+        /// 根据键值返回集合中所有的value
619
+        /// </summary>
620
+        /// <param name="key"></param>
621
+        /// <returns></returns>
622
+        public static string[] SetMembers(string key)
623
+        {
624
+            return database.SetMembers(key).ToStringArray();
625
+        }
626
+
627
+        /// <summary>
628
+        /// 将成员从源集移动到目标集
629
+        /// </summary>
630
+        /// <param name="sourceKey">源集key</param>
631
+        /// <param name="destinationKey">目标集key</param>
632
+        /// <param name="value"></param>
633
+        public static bool SetMove(string sourceKey, string destinationKey, string value)
634
+        {
635
+            return database.SetMove(sourceKey, destinationKey, value);
636
+        }
637
+
638
+        /// <summary>
639
+        /// 移除集合中指定键值随机元素
640
+        /// </summary>
641
+        /// <param name="key"></param>
642
+        public static string SetPop(string key)
643
+        {
644
+            return database.SetPop(key);
645
+        }
646
+
647
+        /// <summary>
648
+        /// 返回集合中指定键值随机元素
649
+        /// </summary>
650
+        /// <param name="key"></param>
651
+        /// <returns></returns>
652
+        public static string SetRandomMember(string key)
653
+        {
654
+            return database.SetRandomMember(key);
655
+        }
656
+
657
+        /// <summary>
658
+        /// 
659
+        /// </summary>
660
+        /// <param name="key"></param>
661
+        /// <param name="count"></param>
662
+        public static string[] SetRandomMembers(string key, long count)
663
+        {
664
+            return database.SetRandomMembers(key, count).ToStringArray();
665
+        }
666
+
667
+        /// <summary>
668
+        /// 移除集合中指定key值和value
669
+        /// </summary>
670
+        /// <param name="key"></param>
671
+        /// <param name="value"></param>
672
+        public static void SetRemove(string key, string value)
673
+        {
674
+            database.SetRemove(key, value);
675
+        }
676
+
677
+        /// <summary>
678
+        /// 
679
+        /// </summary>
680
+        /// <param name="key"></param>
681
+        public static void SetScan(string key)
682
+        {
683
+            database.SetScan(key);
684
+        }
685
+
686
+        #endregion
687
+
688
+        #region redis 有序集合(sorted set)操作
689
+
690
+        public static void Method(string key, string value, double score)
691
+        {
692
+            database.SortedSetAdd(key, new SortedSetEntry[] { new SortedSetEntry(value, score) });
693
+        }
694
+
695
+        #endregion
696
+
697
+        #region redis 键(Key)操作
698
+
699
+        /// <summary>
700
+        /// 获取 Key
701
+        /// </summary>
702
+        /// <param name="redisKey"></param>
703
+        /// <returns></returns>
704
+        //public static IEnumerable<RedisKey> GetKeyList(string redisKey)
705
+        //{
706
+        //    var server = redis.GetServer(Configs.GetValue("Redis_Server"), Configs.GetValue("Redis_Port"));
707
+        //    return server.Keys(pattern: "*"+ redisKey + "*");
708
+        //}
709
+        public static List<string> GetKeyList(string redisKey)
710
+        {
711
+            var server = redis.GetServer(Configs.GetValue("Redis_Server"), Int32.Parse( Configs.GetValue("Redis_Port")));
712
+            List<string> keylist = new List<string>();
713
+            var redisenum = server.Keys(pattern: "*" + redisKey + "*");
714
+            foreach (var r in redisenum.ToList())
715
+            {
716
+                keylist.Add(r.ToString());
717
+            }
718
+            return keylist;
719
+        }
720
+
721
+        /// <summary>
722
+        /// 移除指定 Key
723
+        /// </summary>
724
+        /// <param name="redisKey"></param>
725
+        /// <returns></returns>
726
+        public static bool KeyDelete(string redisKey)
727
+        {
728
+            return database.KeyDelete(redisKey);
729
+        }
730
+
731
+        /// <summary>
732
+        /// 移除指定 Key
733
+        /// </summary>
734
+        /// <param name="redisKey"></param>
735
+        /// <returns></returns>
736
+        //public static long KeysDelete(IEnumerable<RedisKey> redisKeys)
737
+        //{
738
+        //    return database.KeyDelete(redisKeys.ToArray());
739
+        //}
740
+        public static long KeysDelete(List<string> redisKeys)
741
+        {
742
+            int n = 0;
743
+            foreach (var r in redisKeys)
744
+            {
745
+                if (database.KeyDelete(r))
746
+                {
747
+                    n++;
748
+                }
749
+            }
750
+            return n;
751
+        }
752
+
753
+        /// <summary>
754
+        /// 校验 Key 是否存在
755
+        /// </summary>
756
+        /// <param name="redisKey"></param>
757
+        /// <returns></returns>
758
+        public static bool KeyExists(string redisKey)
759
+        {
760
+            return database.KeyExists(redisKey);
761
+        }
762
+
763
+        /// <summary>
764
+        /// 重命名 Key
765
+        /// </summary>
766
+        /// <param name="redisKey"></param>
767
+        /// <param name="redisNewKey"></param>
768
+        /// <returns></returns>
769
+        public static bool KeyRename(string redisKey, string redisNewKey)
770
+        {
771
+            return database.KeyRename(redisKey, redisNewKey);
772
+        }
773
+
774
+        /// <summary>
775
+        /// 设置 Key 的时间
776
+        /// </summary>
777
+        /// <param name="redisKey"></param>
778
+        /// <param name="expiry"></param>
779
+        /// <returns></returns>
780
+        public static bool KeyExpire(string redisKey, TimeSpan? expiry)
781
+        {
782
+            return database.KeyExpire(redisKey, expiry);
783
+        }
784
+
785
+        #endregion
786
+
787
+        /// <summary>
788
+        /// 获取key过期时间
789
+        /// </summary>
790
+        /// <param name="redisKey"></param>
791
+        /// <returns></returns>
792
+        public static string GetKeyOutTime(string redisKey)
793
+        {
794
+            var server = redis.GetServer(Configs.GetValue("Redis_Server"), Int32.Parse(Configs.GetValue("Redis_Port")));
795
+            var timeNow = server.Time().ToUniversalTime();
796
+            var time = database.KeyTimeToLive(redisKey);
797
+            var expire = time == null ? (DateTime?)null : timeNow.Add(time.Value); //返回UTC时间。
798
+            return expire == null ? "" : expire.Value.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
799
+        }
800
+    }
801
+}

+ 1 - 0
codegit/CallCenterCommon/CallCenter.Utility/packages.config

@@ -4,4 +4,5 @@
4 4
   <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
5 5
   <package id="NPOI" version="2.3.0" targetFramework="net45" />
6 6
   <package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
7
+  <package id="StackExchange.Redis" version="1.2.6" targetFramework="net45" />
7 8
 </packages>