Redis操作的封装类

http://blog.wx6.org/2013/349.htm

公司的项目用到的了Redis,还是很好用的,引用了,但是还是有些麻烦的地方,自己写了一个类封装了一些操作,相对方便一点,记录一下Code

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  ServiceStack.Redis;
       
namespace  Com.QFGame.QNX.Community.Redis
{
     public  class  RedisBase
     {
       
         private  static  string  RedisPath = System.Configuration.ConfigurationSettings.AppSettings[ "RedisPath" ];
       
         #region -- 连接信息 --
         //10.0.18.8:6379
         public  static  PooledRedisClientManager prcm = CreateManager( new  string [] { RedisPath },  new  string [] { RedisPath });
         private  static  PooledRedisClientManager CreateManager( string [] readWriteHosts,  string [] readOnlyHosts)
        
             // 支持读写分离,均衡负载 
             return  new  PooledRedisClientManager(readWriteHosts, readOnlyHosts,  new  RedisClientManagerConfig
             {
                 MaxWritePoolSize = 5,  // “写”链接池链接数 
                 MaxReadPoolSize = 5,  // “读”链接池链接数 
                 AutoStart =  true ,
             });
         }
         #endregion
       
       
       
       
       
       
       
       
       
       
         #region -- Item --
         /// <summary>
         /// 设置单体
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="t"></param>
         /// <param name="timeSpan"></param>
         /// <returns></returns>
         public  static  bool  Item_Set<T>( string  key, T t)
         {
             try
             {
                 using  (IRedisClient redis = prcm.GetClient())
                 {
                     return  redis.Set<T>(key, t,  new  TimeSpan(1, 0, 0));
                 }
             }
             catch  (Exception ex)
             {
                 // LogInfo
             }
             return  false ;
         }
       
         /// <summary>
         /// 获取单体
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <returns></returns>
         public  static  T Item_Get<T>( string  key) where T :  class
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.Get<T>(key);
             }
         }
       
         /// <summary>
         /// 移除单体
         /// </summary>
         /// <param name="key"></param>
         public  static  bool  Item_Remove( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.Remove(key);
             }
         }
       
         #endregion
       
         #region -- List --
       
         public  static  void  List_Add<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
             }
         }
       
               
       
         public  static  bool  List_Remove<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 return  redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0;
             }
         }
         public  static  void  List_RemoveAll<T>( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 redisTypedClient.Lists[key].RemoveAll();
             }
         }
       
         public  static  int  List_Count( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.GetListCount(key);
            
         }
       
         public  static  List<T> List_GetRange<T>( string  key,  int  start,  int  count)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var c = redis.GetTypedClient<T>();
                 return  c.Lists[key].GetRange(start, start + count - 1);
             }
         }
       
       
         public  static  List<T> List_GetList<T>( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var c = redis.GetTypedClient<T>();
                 return  c.Lists[key].GetRange(0, c.Lists[key].Count);
             }
         }
       
         public  static  List<T> List_GetList<T>( string  key,  int  pageIndex,  int  pageSize)
         {
             int  start = pageSize * (pageIndex - 1);
             return  List_GetRange<T>(key, start, pageSize);
         }
       
         /// <summary>
         /// 设置缓存过期
         /// </summary>
         /// <param name="key"></param>
         /// <param name="datetime"></param>
         public  static  void  List_SetExpire( string  key, DateTime datetime)
         {
                 using  (IRedisClient redis = prcm.GetClient())
                 {
                         redis.ExpireEntryAt(key, datetime);
                 }
         }
         #endregion
       
         #region -- Set --
         public  static  void  Set_Add<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 redisTypedClient.Sets[key].Add(t);
             }
         }
         public  static  bool  Set_Contains<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 return  redisTypedClient.Sets[key].Contains(t);
             }
         }
         public  static  bool  Set_Remove<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var redisTypedClient = redis.GetTypedClient<T>();
                 return  redisTypedClient.Sets[key].Remove(t);
             }
         }
         #endregion
       
       
         #region -- Hash --
         /// <summary>
         /// 判断某个数据是否已经被缓存
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="dataKey"></param>
         /// <returns></returns>
         public  static  bool  Hash_Exist<T>( string  key,  string  dataKey)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.HashContainsEntry(key, dataKey);
             }
         }
       
         /// <summary>
         /// 存储数据到hash表
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="dataKey"></param>
         /// <returns></returns>
         public  static  bool  Hash_Set<T>( string  key,  string  dataKey, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 string  value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
                 return  redis.SetEntryInHash(key, dataKey, value);
             }
         }
         /// <summary>
         /// 移除hash中的某值
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="dataKey"></param>
         /// <returns></returns>
         public  static  bool  Hash_Remove( string  key,  string  dataKey)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.RemoveEntryFromHash(key, dataKey);
             }
         }
         /// <summary>
         /// 移除整个hash
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="dataKey"></param>
         /// <returns></returns>
         public  static  bool  Hash_Remove( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.Remove(key);
             }
         }
         /// <summary>
         /// 从hash表获取数据
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="dataKey"></param>
         /// <returns></returns>
         public  static  T Hash_Get<T>( string  key,  string  dataKey)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 string  value = redis.GetValueFromHash(key, dataKey);
                 return  ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(value);
             }
         }
         /// <summary>
         /// 获取整个hash的数据
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <returns></returns>
         public  static  List<T> Hash_GetAll<T>( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var list = redis.GetHashValues(key);
                 if  (list !=  null  && list.Count > 0)
                 {
                     List<T> result =  new  List<T>();
                     foreach  (var item  in  list)
                     {
                         var value = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
                         result.Add(value);
                     }
                     return  result;
                 }
                 return  null ;
             }
         }
         /// <summary>
         /// 设置缓存过期
         /// </summary>
         /// <param name="key"></param>
         /// <param name="datetime"></param>
         public  static  void  Hash_SetExpire( string  key, DateTime datetime)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 redis.ExpireEntryAt(key, datetime);
             }
         }
         #endregion
       
       
       
         #region -- SortedSet --
         /// <summary>
         ///  添加数据到 SortedSet
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="t"></param>
         /// <param name="score"></param>
         public  static  bool  SortedSet_Add<T>( string  key, T t,  double  score)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 string  value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
                 return  redis.AddItemToSortedSet(key, value, score);
             }
         }
         /// <summary>
         /// 移除数据从SortedSet
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="t"></param>
         /// <returns></returns>
         public  static  bool  SortedSet_Remove<T>( string  key, T t)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 string  value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
                 return  redis.RemoveItemFromSortedSet(key, value);
             }
         }
         /// <summary>
         /// 修剪SortedSet
         /// </summary>
         /// <param name="key"></param>
         /// <param name="size">保留的条数</param>
         /// <returns></returns>
         public  static  int  SortedSet_Trim( string  key,  int  size)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.RemoveRangeFromSortedSet(key, size, 9999999);
             }
         }
         /// <summary>
         /// 获取SortedSet的长度
         /// </summary>
         /// <param name="key"></param>
         /// <returns></returns>
         public  static  int  SortedSet_Count( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 return  redis.GetSortedSetCount(key);
             }
         }
       
         /// <summary>
         /// 获取SortedSet的分页数据
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         /// <returns></returns>
         public  static  List<T> SortedSet_GetList<T>( string  key,  int  pageIndex,  int  pageSize)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var list = redis.GetRangeFromSortedSet(key, (pageIndex - 1) * pageSize, pageIndex * pageSize - 1); 
                 if  (list !=  null  && list.Count > 0)
                 {
                     List<T> result =  new  List<T>();
                     foreach  (var item  in  list)
                     {
                         var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
                         result.Add(data);
                     }
                     return  result;
                 }
             }
             return  null ;
         }
       
       
         /// <summary>
         /// 获取SortedSet的全部数据
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="key"></param>
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         /// <returns></returns>
         public  static  List<T> SortedSet_GetListALL<T>( string  key)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 var list = redis.GetRangeFromSortedSet(key, 0, 9999999);
                 if  (list !=  null  && list.Count > 0)
                 {
                     List<T> result =  new  List<T>();
                     foreach  (var item  in  list)
                     {
                         var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
                         result.Add(data);
                     }
                     return  result;
                 }
             }
             return  null ;
         }
       
         /// <summary>
         /// 设置缓存过期
         /// </summary>
         /// <param name="key"></param>
         /// <param name="datetime"></param>
         public  static  void  SortedSet_SetExpire( string  key, DateTime datetime)
         {
             using  (IRedisClient redis = prcm.GetClient())
             {
                 redis.ExpireEntryAt(key, datetime);
             }
         }
       
         //public static double SortedSet_GetItemScore<T>(string key,T t)
         //{
         //    using (IRedisClient redis = prcm.GetClient())
         //    {
         //        var data = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
         //        return redis.GetItemScoreInSortedSet(key, data);
         //    }
         //    return 0;
         //}
       
         #endregion
       
     }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值