浏览代码

修改缓存为redis

zhoufan 7 年之前
父节点
当前提交
54b74385f3

+ 5 - 0
CallCenterApi.Common/CallCenterApi.Common.csproj

@@ -49,9 +49,13 @@
49 49
     <Reference Include="NPOI.OpenXmlFormats, Version=2.3.0.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
50 50
       <HintPath>..\packages\NPOI.2.3.0\lib\net40\NPOI.OpenXmlFormats.dll</HintPath>
51 51
     </Reference>
52
+    <Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
53
+      <HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll</HintPath>
54
+    </Reference>
52 55
     <Reference Include="System" />
53 56
     <Reference Include="System.Configuration" />
54 57
     <Reference Include="System.Core" />
58
+    <Reference Include="System.IO.Compression" />
55 59
     <Reference Include="System.Runtime.Serialization" />
56 60
     <Reference Include="System.Web" />
57 61
     <Reference Include="System.Web.Services" />
@@ -75,6 +79,7 @@
75 79
     <Compile Include="Json.cs" />
76 80
     <Compile Include="NPOIHelper.cs" />
77 81
     <Compile Include="Properties\AssemblyInfo.cs" />
82
+    <Compile Include="RedisHelper.cs" />
78 83
     <Compile Include="RequestString.cs" />
79 84
     <Compile Include="TypeConverter.cs" />
80 85
     <Compile Include="Utils.cs" />

+ 789 - 0
CallCenterApi.Common/RedisHelper.cs

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

+ 1 - 0
CallCenterApi.Common/packages.config

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

+ 26 - 18
CallCenterApi.Interface/App_Start/AuthorizeAttribute.cs

@@ -29,24 +29,32 @@ namespace CallCenterApi.Interface
29 29
                 var action = actionDescriptor.ActionName;
30 30
                 var token = filterContext.HttpContext.Request["token"];
31 31
 
32
-                var userData = CacheHelper.Get<Dictionary<string, string>>(token);
33
-                var roleId = Utils.StrToInt(userData["F_RoleID"], 0);
34
-                //var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
35
-                //if (role != null)
36
-                //{
37
-                //    isAuth = true;
38
-                //    //var roleFunctionList = roleFunctionBLL.GetModelList(" F_RoleId=" + role.F_RoleId);
39
-                //    ////var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
40
-                //    //var moduleFunction = new BLL.T_Sys_Function().GetModel(roleId);
41
-                //    //if (moduleFunction != null)
42
-                //    //{
43
-                //    //    var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
44
-                //    //    if (single != null)
45
-                //    //    {
46
-                //    //        isAuth = true;
47
-                //    //    }
48
-                //    //}
49
-                //}
32
+                //var userData = CacheHelper.Get<Dictionary<string, string>>(token);
33
+                var userDatastr = RedisHelper.StringGet(token);
34
+                if (userDatastr != null)
35
+                {
36
+                    Dictionary<string, string> userData = new Dictionary<string, string>();
37
+                    userData = userDatastr.ToString().ToObject<Dictionary<string, string>>();
38
+                    var roleId = Utils.StrToInt(userData["F_RoleID"], 0);
39
+
40
+                    //var roleId = Utils.StrToInt(userData["F_RoleID"], 0);
41
+                    //var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
42
+                    //if (role != null)
43
+                    //{
44
+                    //    isAuth = true;
45
+                    //    //var roleFunctionList = roleFunctionBLL.GetModelList(" F_RoleId=" + role.F_RoleId);
46
+                    //    ////var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
47
+                    //    //var moduleFunction = new BLL.T_Sys_Function().GetModel(roleId);
48
+                    //    //if (moduleFunction != null)
49
+                    //    //{
50
+                    //    //    var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
51
+                    //    //    if (single != null)
52
+                    //    //    {
53
+                    //    //        isAuth = true;
54
+                    //    //    }
55
+                    //    //}
56
+                    //}
57
+                }
50 58
             }
51 59
             else
52 60
             {

+ 3 - 0
CallCenterApi.Interface/Configs/system.config

@@ -9,5 +9,8 @@
9 9
   <add key="Version" value="5.0" />
10 10
   <!-- 软件授权码-->
11 11
   <add key="LicenceKey" value="" />
12
+  <!-- ================== 2:Redis配置 ================== -->
13
+  <add key="Redis_Server" value="192.168.4.18"/>
14
+  <add key="Redis_Port" value="6379"/>
12 15
 
13 16
 </appSettings>

+ 5 - 2
CallCenterApi.Interface/Controllers/Login/LoginController.cs

@@ -50,7 +50,9 @@ namespace CallCenterApi.Interface.Controllers
50 50
                             var token = FormsPrincipal<Dictionary<string, string>>.GetCookieValue(Dic["F_UserCode"], Dic);
51 51
                            
52 52
                             //放入缓存
53
-                            CacheHelper.Insert(token, Dic, 1440, System.Web.Caching.CacheItemPriority.NotRemovable, null);
53
+                            //CacheHelper.Insert(token, Dic, 1440, System.Web.Caching.CacheItemPriority.NotRemovable, null);
54
+                            //放入redis缓存
55
+                            RedisHelper.StringSet(token, Dic.ToJson(), new TimeSpan(24, 0, 0));
54 56
 
55 57
                             return Success("登录成功", new
56 58
                             {
@@ -92,7 +94,8 @@ namespace CallCenterApi.Interface.Controllers
92 94
         {
93 95
             if (Request.IsAuthenticated)
94 96
             {
95
-                CacheHelper.Remove(token);
97
+                //CacheHelper.Remove(token);
98
+                RedisHelper.KeyDelete(token);
96 99
             }
97 100
             return Success("退出成功");
98 101
         }

+ 4 - 2
CallCenterApi.Interface/Global.asax.cs

@@ -77,8 +77,10 @@ namespace CallCenterApi.Interface
77 77
             }
78 78
             try
79 79
             {
80
-                //获取缓存
81
-                var dict = CacheHelper.Get(token);
80
+                ////获取缓存
81
+                //var dict = CacheHelper.Get(token);
82
+                //获取redis缓存
83
+                var dict = RedisHelper.StringGet(token);
82 84
                 if (dict == null)
83 85
                 {
84 86
                     //log.Debug(Params.ToJson());