本文版權歸博客園和作者本人共同所有,轉載和爬蟲請註明原文鏈接 http://www.cnblogs.com/tdws/tag/NoSql/ 目錄 一、基礎配置封裝 二、String字元串類型數據操作封裝 三、Hash散列類型數據操作封裝 四、List列表類型數據操作封裝 五、Set集合類型數據操作封 ...
本文版權歸博客園和作者本人共同所有,轉載和爬蟲請註明原文鏈接 http://www.cnblogs.com/tdws/tag/NoSql/
目錄一、基礎配置封裝
三、Hash散列類型數據操作封裝
四、List列表類型數據操作封裝
五、Set集合類型數據操作封裝
六、Sort Set集合數據類型操作封裝
七、主從配置,哨兵相關配置
二、String字元串類型數據操作封裝下麵這段畫如果看一遍沒看懂,請看過代碼後再次來閱讀:
我們有必要先提到ConnectionMultiplexer類,它是StackExchange提供給我們的一個類,它將多伺服器的詳細信息隱藏,因為這個類為我們做了很多事情,它的設計目的是為了在調用者間共用和重用。你不用每次操作都創建這樣一個ConnectionMultiplexer,它是完全線程安全的。它擁有ConnectionMultiplexer.Connect和onnectionMultiplexer.ConnectAsync來鏈接Redis。鏈接 參數是一個字元串或者一個ConfigurationOptions對象。這個類實現了IDisposable介面,你可以在你不需要的時候釋放對象,通過using或者dispose。但是你不用經常來釋放它,因為我們要經常復用。你有三種需求時,需要使用ConnectionMultiplexer,鏈接Redis,發佈訂閱模式,訪問一個單獨的伺服器或者監控的目的。除了基本使用,更多的請看github文檔,https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Basics.md
我們在IRedisClient中定義如下String數據類型方法:
如果你覺得代碼太多,當VS按下快捷鍵ctrl+M+O吧
1 #region 程式集 RedisRepository, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 2 // Author:吳雙 2016.8.28 聯繫郵箱 [email protected] 3 #endregion 4 using System; 5 using System.Collections.Generic; 6 using StackExchange.Redis; 7 8 namespace RedisRepository 9 { 10 public interface IRedisClient 11 { 12 #region Redis String類型操作 13 /// <summary> 14 /// Redis String類型 新增一條記錄 15 /// </summary> 16 /// <typeparam name="T">generic refrence type</typeparam> 17 /// <param name="key">unique key of value</param> 18 /// <param name="value">value of key of type object</param> 19 /// <param name="expiresAt">time span of expiration</param> 20 /// <param name= "when">枚舉類型</param> 21 /// <param name="commandFlags"></param> 22 /// <returns>true or false</returns> 23 bool StringSet<T>(string key, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; 24 25 /// <summary> 26 /// Redis String類型 新增一條記錄 27 /// </summary> 28 /// <typeparam name="T">generic refrence type</typeparam> 29 /// <param name="key">unique key of value</param> 30 /// <param name="value">value of key of type object</param> 31 /// <param name="expiresAt">time span of expiration</param> 32 /// <param name= "when">枚舉類型</param> 33 /// <param name="commandFlags"></param> 34 /// <returns>true or false</returns> 35 bool StringSet<T>(string key, T value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; 36 37 /// <summary> 38 /// 更新時應使用此方法,代碼更可讀。 39 /// </summary> 40 /// <typeparam name="T"></typeparam> 41 /// <param name="key"></param> 42 /// <param name="value"></param> 43 /// <param name="expiresAt"></param> 44 /// <param name="when"></param> 45 /// <param name="commandFlags"></param> 46 /// <returns></returns> 47 bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class; 48 49 /// <summary> 50 /// Redis String類型 Get 51 /// </summary> 52 /// <typeparam name="T"></typeparam> 53 /// <param name="key"></param> 54 /// <param name="commandFlags"></param> 55 /// <returns>T</returns> 56 T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class; 57 58 /// <summary> 59 /// Redis String數據類型 獲取指定key中字元串長度 60 /// </summary> 61 /// <param name="key"></param> 62 /// <param name="commandFlags"></param> 63 /// <returns></returns> 64 long StringLength(string key, CommandFlags commandFlags = CommandFlags.None); 65 66 /// <summary> 67 /// Redis String數據類型 返回拼接後總長度 68 /// </summary> 69 /// <param name="key"></param> 70 /// <param name="appendVal"></param> 71 /// <param name="commandFlags"></param> 72 /// <returns>總長度</returns> 73 long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None); 74 75 /// <summary> 76 /// 設置新值並且返回舊值 77 /// </summary> 78 /// <param name="key"></param> 79 /// <param name="newVal"></param> 80 /// <param name="commandFlags"></param> 81 /// <returns>OldVal</returns> 82 string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None); 83 84 /// <summary> 85 /// 為數字增長val 86 /// </summary> 87 /// <param name="key"></param> 88 /// <param name="val"></param> 89 /// <param name="commandFlags"></param> 90 /// <returns>增長後的值</returns> 91 double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None); 92 93 /// <summary> 94 /// Redis String數據類型 95 /// 類似於模糊查詢 key* 查出所有key開頭的鍵 96 /// </summary> 97 /// <typeparam name="T"></typeparam> 98 /// <param name="key"></param> 99 /// <param name="pageSize"></param> 100 /// <param name="commandFlags"></param> 101 /// <returns>返回List<T></returns> 102 List<T> StringGetList<T>(string key, int pageSize = 1000, CommandFlags commandFlags = CommandFlags.None) where T : class; 103 #endregion 104 105 106 #region Redis各數據類型公用 107 108 /// <summary> 109 /// Redis中是否存在指定Key 110 /// </summary> 111 /// <param name="key"></param> 112 /// <param name="commandFlags"></param> 113 /// <returns></returns> 114 bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None); 115 116 /// <summary> 117 /// 從Redis中移除鍵 118 /// </summary> 119 /// <param name="key"></param> 120 /// <param name="commandFlags"></param> 121 /// <returns></returns> 122 bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None); 123 124 /// <summary> 125 /// 從Redis中移除多個鍵 126 /// </summary> 127 /// <param name="keys"></param> 128 /// <param name="commandFlags"></param> 129 /// <returns></returns> 130 void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None); 131 132 /// <summary> 133 /// Dispose DB connection 釋放DB相關鏈接 134 /// </summary> 135 void DbConnectionStop(); 136 #endregion 137 } 138 }
在RedisClient.cs中實現如下:
using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using StackExchange.Redis; namespace RedisRepository { public class RedisClient : IRedisClient { #region 初始化 private readonly IDatabase _db; private readonly ConnectionMultiplexer _redis; /// <summary> /// 構造函數,在其中註冊Redis事件 /// </summary> public RedisClient() { const string configuration = "{0},abortConnect=false,defaultDatabase={1},ssl=false,ConnectTimeout={2},allowAdmin=true,connectRetry={3}"; _redis = ConnectionMultiplexer .Connect(string.Format(configuration, RedisClientConfigurations.Url, RedisClientConfigurations.DefaultDatabase, RedisClientConfigurations.ConnectTimeout, RedisClientConfigurations.ConnectRetry)); _redis.PreserveAsyncOrder = RedisClientConfigurations.PreserveAsyncOrder; //_redis.ConnectionFailed; _db = _redis.GetDatabase(); } #endregion #region Redis String數據類型操作 /// <summary> /// Redis String類型 新增一條記錄 /// </summary> /// <typeparam name="T">generic refrence type</typeparam> /// <param name="key">unique key of value</param> /// <param name="value">value of key of type T</param> /// <param name="expiresAt">time span of expiration</param> /// <returns>true or false</returns> public bool StringSet<T>(string key, T value, TimeSpan? expiresAt = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class { var stringContent = SerializeContent(value); return _db.StringSet(key, stringContent, expiresAt, when, commandFlags); } /// <summary> /// Redis String類型 新增一條記錄 /// </summary> /// <typeparam name="T">generic refrence type</typeparam> /// <param name="key">unique key of value</param> /// <param name="value">value of key of type object</param> /// <param name="expiresAt">time span of expiration</param> /// <returns>true or false</returns> public bool StringSet<T>(string key, object value, TimeSpan? expiresAt = default(TimeSpan?), When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class { var stringContent = SerializeContent(value); return _db.StringSet(key, stringContent, expiresAt, when, commandFlags); } /// <summary> /// Redis String數據類型 獲取指定key中字元串長度 /// </summary> /// <param name="key"></param> /// <returns></returns> public long StringLength(string key, CommandFlags commandFlags = CommandFlags.None) { return _db.StringLength(key, commandFlags); } /// <summary> /// Redis String數據類型 返回拼接後總長度 /// </summary> /// <param name="key"></param> /// <param name="appendVal"></param> /// <returns>總長度</returns> public long StringAppend(string key, string appendVal, CommandFlags commandFlags = CommandFlags.None) { return _db.StringAppend(key, appendVal, commandFlags); } /// <summary> /// 設置新值並且返回舊值 /// </summary> /// <param name="key"></param> /// <param name="newVal"></param> /// <param name="commandFlags"></param> /// <returns>OldVal</returns> public string StringGetAndSet(string key, string newVal, CommandFlags commandFlags = CommandFlags.None) { return _db.StringGetSet(key, newVal, commandFlags); } /// <summary> /// 更新時應使用此方法,代碼更可讀。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiresAt"></param> /// <param name="when"></param> /// <param name="commandFlags"></param> /// <returns></returns> public bool StringUpdate<T>(string key, T value, TimeSpan expiresAt, When when = When.Always, CommandFlags commandFlags = CommandFlags.None) where T : class { var stringContent = SerializeContent(value); return _db.StringSet(key, stringContent, expiresAt, when, commandFlags); } /// <summary> /// 為數字增長val /// </summary> /// <param name="key"></param> /// <param name="val">可以為負</param> /// <param name="commandFlags"></param> /// <returns>增長後的值</returns> public double StringIncrement(string key, double val, CommandFlags commandFlags = CommandFlags.None) { return _db.StringIncrement(key, val, commandFlags); } /// <summary> /// Redis String類型 Get /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns>T</returns> public T StringGet<T>(string key, CommandFlags commandFlags = CommandFlags.None) where T : class { try { RedisValue myString = _db.StringGet(key, commandFlags); if (myString.HasValue && !myString.IsNullOrEmpty) { return DeserializeContent<T>(myString); } else { return null; } } catch (Exception) { // Log Exception return null; } } /// <summary> /// Redis String類型 /// 類似於模糊查詢 key* 查出所有key開頭的鍵 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pageSize"></param> /// <param name="commandFlags"></param> /// <returns>List<T></returns> public List<T> StringGetList<T>(string key, int pageSize = 1000, CommandFlags commandFlags = CommandFlags.None) where T : class { try { var server = _redis.GetServer(host: RedisClientConfigurations.Url, port: RedisClientConfigurations.Port); var keys = server.Keys(_db.Database, key, pageSize, commandFlags); var keyValues = _db.StringGet(keys.ToArray(), commandFlags); var result = new List<T>(); foreach (var redisValue in keyValues) { if (redisValue.HasValue && !redisValue.IsNullOrEmpty) { var item = DeserializeContent<T>(redisValue); result.Add(item); } } return result; } catch (Exception) { // Log Exception return null; } } #endregion #region Redis Hash散列數據類型操作 #endregion #region Redis List列表數據類型操作 #endregion #region Redis Set集合數據類型操作 #endregion #region Redis Sort Set有序集合數據類型操作 #endregion #region Redis各數據類型公用 /// <summary> /// Redis中是否存在指定Key /// </summary> /// <param name="key"></param> /// <returns></returns> public bool KeyExists(string key, CommandFlags commandFlags = CommandFlags.None) { return _db.KeyExists(key, commandFlags); } /// <summary> /// Dispose DB connection 釋放DB相關鏈接 /// </summary> public void DbConnectionStop() { _redis.Dispose(); } /// <summary> /// 從Redis中移除鍵 /// </summary> /// <param name="key"></param> /// <returns></returns> public bool KeyRemove(string key, CommandFlags commandFlags = CommandFlags.None) { return _db.KeyDelete(key, commandFlags); } /// <summary> /// 從Redis中移除多個鍵 /// </summary> /// <param name="keys"></param> public void KeyRemove(RedisKey[] keys, CommandFlags commandFlags = CommandFlags.None) { _db.KeyDelete(keys, commandFlags); } #endregion #region 私有公用方法 // serialize and Deserialize content in separate functions as redis can save value as array of binary. // so, any time you need to change the way of handling value, do it here. private string SerializeContent(object value) { return JsonConvert.SerializeObject(value); } private T DeserializeContent<T>(RedisValue myString) { return JsonConvert.DeserializeObject<T>(myString); } #endregion } }
下麵簡單介紹一下本文中的方法細節之處。
首先在RedisClient類的構造方法中初始化Redis數據操作對象_db。每個方法更多的詳盡信息請註意方法註釋。如果關於Redis命令還不瞭解,請看前期Redis命令拾遺系列文章 http://www.cnblogs.com/tdws/tag/NoSql/