Redis读写帮助类

  1     /// <summary>
  2     /// Redis读写帮助类
  3     /// </summary>
  4     public class RedisHelper
  5     {
  6         private ConnectionMultiplexer redis { get; set; }
  7         private IDatabase db { get; set; }
  8         public RedisHelper(string connection)
  9         {
 10             redis = ConnectionMultiplexer.Connect(connection);
 11             db = redis.GetDatabase(1);
 12         }
 13         #region string类型操作
 14         /// <summary>
 15         /// set or update the value for string key 
 16         /// </summary>
 17         /// <param name="key"></param>
 18         /// <param name="value"></param>
 19         /// <returns></returns>
 20         public bool SetStringValue(string key, string value)
 21         {
 22             return db.StringSet(key, value);
 23         }
 24         /// <summary>
 25         /// 保存单个key value
 26         /// </summary>
 27         /// <param name="key">Redis Key</param>
 28         /// <param name="value">保存的值</param>
 29         /// <param name="expiry">过期时间</param>
 30         /// <returns></returns>
 31         public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
 32         {
 33             return db.StringSet(key, value, expiry);
 34         }
 35         /// <summary>
 36         /// 保存一个对象
 37         /// </summary>
 38         /// <typeparam name="T"></typeparam>
 39         /// <param name="key"></param>
 40         /// <param name="obj"></param>
 41         /// <returns></returns>
 42         public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
 43         {
 44             string json = JsonConvert.SerializeObject(obj);
 45             return db.StringSet(key, json, expiry);
 46         }
 47         /// <summary>
 48         /// 获取一个key的对象
 49         /// </summary>
 50         /// <typeparam name="T"></typeparam>
 51         /// <param name="key"></param>
 52         /// <returns></returns>
 53         public T GetStringKey<T>(string key) where T : class
 54         {
 55             var result = db.StringGet(key);
 56             if (string.IsNullOrEmpty(result))
 57             {
 58                 return null;
 59             }
 60             return JsonConvert.DeserializeObject<T>(result);
 61         }
 62         /// <summary>
 63         /// get the value for string key 
 64         /// </summary>
 65         /// <param name="key"></param>
 66         /// <returns></returns>
 67         public string GetStringValue(string key)
 68         {
 69             return db.StringGet(key);
 70         }
 71 
 72         /// <summary>
 73         /// Delete the value for string key 
 74         /// </summary>
 75         /// <param name="key"></param>
 76         /// <returns></returns>
 77         public bool DeleteStringKey(string key)
 78         {
 79             return db.KeyDelete(key);
 80         }
 81         #endregion
 82 
 83         #region 哈希类型操作
 84         /// <summary>
 85         /// set or update the HashValue for string key 
 86         /// </summary>
 87         /// <param name="key"></param>
 88         /// <param name="hashkey"></param>
 89         /// <param name="value"></param>
 90         /// <returns></returns>
 91         public bool SetHashValue(string key, string hashkey, string value)
 92         {
 93             return db.HashSet(key, hashkey, value);
 94         }
 95         /// <summary>
 96         /// set or update the HashValue for string key 
 97         /// </summary>
 98         /// <typeparam name="T"></typeparam>
 99         /// <param name="key"></param>
100         /// <param name="hashkey"></param>
101         /// <param name="t">defined class</param>
102         /// <returns></returns>
103         public bool SetHashValue<T>(String key, string hashkey, T t) where T : class
104         {
105             var json = JsonConvert.SerializeObject(t);
106             return db.HashSet(key, hashkey, json);
107         }
108         /// <summary>
109         /// 保存一个集合
110         /// </summary>
111         /// <typeparam name="T"></typeparam>
112         /// <param name="key">Redis Key</param>
113         /// <param name="list">数据集合</param>
114         /// <param name="getModelId"></param>
115         public void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
116         {
117             List<HashEntry> listHashEntry = new List<HashEntry>();
118             foreach (var item in list)
119             {
120                 string json = JsonConvert.SerializeObject(item);
121                 listHashEntry.Add(new HashEntry(getModelId(item), json));
122             }
123             db.HashSet(key, listHashEntry.ToArray());
124         }
125         /// <summary>
126         /// 获取hashkey所有的值
127         /// </summary>
128         /// <typeparam name="T"></typeparam>
129         /// <param name="key"></param>
130         /// <returns></returns>
131         public List<T> HashGetAll<T>(string key) where T : class
132         {
133             List<T> result = new List<T>();
134             HashEntry[] arr = db.HashGetAll(key);
135             foreach (var item in arr)
136             {
137                 if (!item.Value.IsNullOrEmpty)
138                 {
139                     result.Add(JsonConvert.DeserializeObject<T>(item.Value));
140 
141                 }
142             }
143             return result;
144             //result =JsonHelper.DeserializeJsonToList<T>(arr.ToString());                        
145             //return result;
146         }
147         /// <summary>
148         /// get the HashValue for string key  and hashkey
149         /// </summary>
150         /// <param name="key">Represents a key that can be stored in redis</param>
151         /// <param name="hashkey"></param>
152         /// <returns></returns>
153         public RedisValue GetHashValue(string key, string hashkey)
154         {
155             RedisValue result = db.HashGet(key, hashkey);
156             return result;
157         }
158         /// <summary>
159         /// get the HashValue for string key  and hashkey
160         /// </summary>
161         /// <param name="key">Represents a key that can be stored in redis</param>
162         /// <param name="hashkey"></param>
163         /// <returns></returns>
164         public T GetHashValue<T>(string key, string hashkey) where T : class
165         {
166             RedisValue result = db.HashGet(key, hashkey);
167             if (string.IsNullOrEmpty(result))
168             {
169                 return null;
170             }
171             return JsonConvert.DeserializeObject<T>(result);
172         }
173         /// <summary>
174         /// delete the HashValue for string key  and hashkey
175         /// </summary>
176         /// <param name="key"></param>
177         /// <param name="hashkey"></param>
178         /// <returns></returns>
179         public bool DeleteHashValue(string key, string hashkey)
180         {
181             return db.HashDelete(key, hashkey);
182         }
183         #endregion
184     }
原文地址:https://www.cnblogs.com/baiqjh/p/11316505.html