StackExchange.Redis的使用

StackExchange.Redis介绍

有需要了解的和基础的使用可以参考:http://www.cnblogs.com/bnbqian/p/4962855.html

 

StackExchange.Redis的简单封装

  1  /// <summary>
  2     /// Redis操作类
  3     /// </summary>
  4     public class SERedisHelper
  5     {
  6         private static string _conn = ConfigurationManager.AppSettings["SERedis"] ?? "127.0.0.1:6379";
  7 
  8         #region string类型
  9         /// <summary>
 10         /// 根据Key获取值
 11         /// </summary>
 12         /// <param name="key">键值</param>
 13         /// <returns>System.String.</returns>
 14         public static string StringGet(string key)
 15         {
 16             try
 17             {
 18                 using (var client = ConnectionMultiplexer.Connect(_conn))
 19                 {
 20                     return client.GetDatabase().StringGet(key);
 21                 }
 22             }
 23             catch (Exception)
 24             {
 25                 return null;
 26             }
 27         }
 28 
 29         /// <summary>
 30         /// 批量获取值
 31         /// </summary>
 32         public static string[] StringGetMany(string[] keyStrs)
 33         {
 34             var count = keyStrs.Length;
 35             var keys = new RedisKey[count];
 36             var addrs = new string[count];
 37 
 38             for (var i = 0; i < count; i++)
 39             {
 40                 keys[i] = keyStrs[i];
 41             }
 42             try
 43             {
 44                 using (var client = ConnectionMultiplexer.Connect(_conn))
 45                 {
 46                     var values = client.GetDatabase().StringGet(keys);
 47                     for (var i = 0; i < values.Length; i++)
 48                     {
 49                         addrs[i] = values[i];
 50                     }
 51                     return addrs;
 52                 }
 53             }
 54             catch (Exception)
 55             {
 56                 return null;
 57             }
 58         }
 59 
 60 
 61         /// <summary>
 62         /// 单条存值
 63         /// </summary>
 64         /// <param name="key">key</param>
 65         /// <param name="value">The value.</param>
 66         /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 67         public static bool StringSet(string key, string value)
 68         {
 69 
 70             using (var client = ConnectionMultiplexer.Connect(_conn))
 71             {
 72                 return client.GetDatabase().StringSet(key, value);
 73             }
 74         }
 75 
 76 
 77         /// <summary>
 78         /// 批量存值
 79         /// </summary>
 80         /// <param name="keysStr">key</param>
 81         /// <param name="valuesStr">The value.</param>
 82         /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
 83         public static bool StringSetMany(string[] keysStr, string[] valuesStr)
 84         {
 85             var count = keysStr.Length;
 86             var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
 87             for (int i = 0; i < count; i++)
 88             {
 89                 keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(keysStr[i], valuesStr[i]);
 90             }
 91             using (var client = ConnectionMultiplexer.Connect(_conn))
 92             {
 93                 return client.GetDatabase().StringSet(keyValuePair);
 94             }
 95         }
 96 
 97         #endregion
 98 
 99         #region 泛型
100         /// <summary>
101         /// 存值并设置过期时间
102         /// </summary>
103         /// <typeparam name="T"></typeparam>
104         /// <param name="key">key</param>
105         /// <param name="t">实体</param>
106         /// <param name="ts">过期时间间隔</param>
107         /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
108         public static bool Set<T>(string key, T t, TimeSpan ts)
109         {
110             var str = JsonConvert.SerializeObject(t);
111             using (var client = ConnectionMultiplexer.Connect(_conn))
112             {
113                 return client.GetDatabase().StringSet(key, str, ts);
114             }
115         }
116 
117         /// <summary>
118         /// 
119         /// 根据Key获取值
120         /// </summary>
121         /// <typeparam name="T"></typeparam>
122         /// <param name="key">The key.</param>
123         /// <returns>T.</returns>
124         public static T Get<T>(string key) where T : class
125         {
126             using (var client = ConnectionMultiplexer.Connect(_conn))
127             {
128                 var strValue = client.GetDatabase().StringGet(key);
129                 return string.IsNullOrEmpty(strValue) ? null : JsonConvert.DeserializeObject<T>(strValue);
130             }
131         }
132         #endregion
133     }

 StackExchange.Redis的使用经验

1、对于与Redis连接的配置,写法很宽松,最简单的写法写个IP和端口就可以了,如果有特殊配置,可以参考:https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Configuration.md

 

2、StackExchange.Redis中的核心是ConnectionMultiplexer,可以好好研究。

 

3、在StackExchange.Redis没必要多New链接:ConnectionMultiplexer.Connect,New多个链接反而会报错,ConnectionMultiplexer内部有自己的一套调度方法。粗略的大致看作者的GitHub主页,StackExchage最多与Redis客户端建立6个连接。比较深入的,暂时吃不动。。。就以上这些了

 

你可能感兴趣的:(StackExchange.Redis的使用)