目前 .NET 使用訪問 Redis 的的類庫主流應該是 StackExchange.redis,本身參考網上的文章(也許是吃飽了撐着),也嘗試作出簡單的封裝。git
/// <summary> /// Redis 助手 /// </summary> public class RedisHelper { /// <summary> /// 鏈接字符串 /// </summary> private static readonly string ConnectionString; /// <summary> /// redis 鏈接對象 /// </summary> private static IConnectionMultiplexer _connMultiplexer; /// <summary> /// 默認的 Key 值(用來看成 RedisKey 的前綴) /// </summary> private static readonly string DefaultKey; /// <summary> /// 鎖 /// </summary> private static readonly object Locker = new object(); /// <summary> /// 數據庫 /// </summary> private readonly IDatabase _db; /// <summary> /// 獲取 Redis 鏈接對象 /// </summary> /// <returns></returns> public IConnectionMultiplexer GetConnectionRedisMultiplexer() { if ((_connMultiplexer == null) || !_connMultiplexer.IsConnected) { lock (Locker) { if ((_connMultiplexer == null) || !_connMultiplexer.IsConnected) _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); } } return _connMultiplexer; } #region 其它 public ITransaction GetTransaction() { return _db.CreateTransaction(); } #endregion 其它 #region 構造函數 static RedisHelper() { ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString; _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); DefaultKey = ConfigurationManager.AppSettings["Redis.DefaultKey"]; AddRegisterEvent(); } public RedisHelper(int db = -1) { _db = _connMultiplexer.GetDatabase(db); } #endregion 構造函數 #region String 操做 /// <summary> /// 設置 key 並保存字符串(若是 key 已存在,則覆蓋值) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringSet(redisKey, redisValue, expiry); } /// <summary> /// 保存多個 Key-value /// </summary> /// <param name="keyValuePairs"></param> /// <returns></returns> public bool StringSet(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value)); return _db.StringSet(keyValuePairs.ToArray()); } /// <summary> /// 獲取字符串 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public string StringGet(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringGet(redisKey); } /// <summary> /// 存儲一個對象(該對象會被序列化保存) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public bool StringSet<T>(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return _db.StringSet(redisKey, json, expiry); } /// <summary> /// 獲取一個對象(會進行反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public T StringGet<T>(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.StringGet(redisKey)); } #region async /// <summary> /// 保存一個字符串值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> StringSetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringSetAsync(redisKey, redisValue, expiry); } /// <summary> /// 保存一組字符串值 /// </summary> /// <param name="keyValuePairs"></param> /// <returns></returns> public async Task<bool> StringSetAsync(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value)); return await _db.StringSetAsync(keyValuePairs.ToArray()); } /// <summary> /// 獲取單個值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<string> StringGetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringGetAsync(redisKey); } /// <summary> /// 存儲一個對象(該對象會被序列化保存) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> StringSetAsync<T>(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return await _db.StringSetAsync(redisKey, json, expiry); } /// <summary> /// 獲取一個對象(會進行反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<T> StringGetAsync<T>(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.StringGetAsync(redisKey)); } #endregion async #endregion String 操做 #region Hash 操做 /// <summary> /// 判斷該字段是否存在 hash 中 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public bool HashExists(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashExists(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public bool HashDelete(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public long HashDelete(string redisKey, IEnumerable<RedisValue> hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField.ToArray()); } /// <summary> /// 在 hash 設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public bool HashSet(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashSet(redisKey, hashField, value); } /// <summary> /// 在 hash 中設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashFields"></param> public void HashSet(string redisKey, IEnumerable<HashEntry> hashFields) { redisKey = AddKeyPrefix(redisKey); _db.HashSet(redisKey, hashFields.ToArray()); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public RedisValue HashGet(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } /// <summary> /// 從 hash 返回全部的字段值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> HashKeys(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashKeys(redisKey); } /// <summary> /// 返回 hash 中的全部值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public RedisValue[] HashValues(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashValues(redisKey); } /// <summary> /// 在 hash 設定值(序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public bool HashSet<T>(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return _db.HashSet(redisKey, hashField, json); } /// <summary> /// 在 hash 中獲取值(反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public T HashGet<T>(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.HashGet(redisKey, hashField)); } #region async /// <summary> /// 判斷該字段是否存在 hash 中 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<bool> HashExistsAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashExistsAsync(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<bool> HashDeleteAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<long> HashDeleteAsync(string redisKey, IEnumerable<RedisValue> hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField.ToArray()); } /// <summary> /// 在 hash 設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<bool> HashSetAsync(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashSetAsync(redisKey, hashField, value); } /// <summary> /// 在 hash 中設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashFields"></param> public async Task HashSetAsync(string redisKey, IEnumerable<HashEntry> hashFields) { redisKey = AddKeyPrefix(redisKey); await _db.HashSetAsync(redisKey, hashFields.ToArray()); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<RedisValue> HashGetAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashGetAsync(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } /// <summary> /// 從 hash 返回全部的字段值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashKeysAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashKeysAsync(redisKey); } /// <summary> /// 返回 hash 中的全部值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashValuesAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashValuesAsync(redisKey); } /// <summary> /// 在 hash 設定值(序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<bool> HashSetAsync<T>(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return await _db.HashSetAsync(redisKey, hashField, json); } /// <summary> /// 在 hash 中獲取值(反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<T> HashGetAsync<T>(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.HashGetAsync(redisKey, hashField)); } #endregion async #endregion Hash 操做 #region List 操做 /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public string ListLeftPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPop(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public string ListRightPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPop(redisKey); } /// <summary> /// 移除列表指定鍵上與該值相同的元素 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRemove(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRemove(redisKey, redisValue); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRightPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, redisValue); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListLeftPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, redisValue); } /// <summary> /// 返回列表上該鍵的長度,若是不存在,返回 0 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public long ListLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLength(redisKey); } /// <summary> /// 返回在該列表上鍵所對應的元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> ListRange(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRange(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public T ListLeftPop<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.ListLeftPop(redisKey)); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public T ListRightPop<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.ListRightPop(redisKey)); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRightPush<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, Serialize(redisValue)); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListLeftPush<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, Serialize(redisValue)); } #region List-async /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<string> ListLeftPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPopAsync(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<string> ListRightPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPopAsync(redisKey); } /// <summary> /// 移除列表指定鍵上與該值相同的元素 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRemoveAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRemoveAsync(redisKey, redisValue); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRightPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, redisValue); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListLeftPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, redisValue); } /// <summary> /// 返回列表上該鍵的長度,若是不存在,返回 0 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<long> ListLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLengthAsync(redisKey); } /// <summary> /// 返回在該列表上鍵所對應的元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> ListRangeAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRangeAsync(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<T> ListLeftPopAsync<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.ListLeftPopAsync(redisKey)); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<T> ListRightPopAsync<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.ListRightPopAsync(redisKey)); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRightPushAsync<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, Serialize(redisValue)); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListLeftPushAsync<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, Serialize(redisValue)); } #endregion List-async #endregion List 操做 #region SortedSet 操做 /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public bool SortedSetAdd(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetAdd(redisKey, member, score); } /// <summary> /// 在有序集合中返回指定範圍的元素,默認狀況下從低到高。 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> SortedSetRangeByRank(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRangeByRank(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public long SortedSetLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetLength(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <param name="memebr"></param> /// <returns></returns> public bool SortedSetLength(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRemove(redisKey, memebr); } /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public bool SortedSetAdd<T>(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return _db.SortedSetAdd(redisKey, json, score); } #region SortedSet-Async /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public async Task<bool> SortedSetAddAsync(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetAddAsync(redisKey, member, score); } /// <summary> /// 在有序集合中返回指定範圍的元素,默認狀況下從低到高。 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> SortedSetRangeByRankAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRangeByRankAsync(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<long> SortedSetLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetLengthAsync(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <param name="memebr"></param> /// <returns></returns> public async Task<bool> SortedSetRemoveAsync(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRemoveAsync(redisKey, memebr); } /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public async Task<bool> SortedSetAddAsync<T>(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return await _db.SortedSetAddAsync(redisKey, json, score); } #endregion SortedSet-Async #endregion SortedSet 操做 #region key 操做 /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public bool KeyDelete(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyDelete(redisKey); } /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKeys"></param> /// <returns></returns> public long KeyDelete(IEnumerable<string> redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return _db.KeyDelete(keys.ToArray()); } /// <summary> /// 校驗 Key 是否存在 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public bool KeyExists(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExists(redisKey); } /// <summary> /// 重命名 Key /// </summary> /// <param name="redisKey"></param> /// <param name="redisNewKey"></param> /// <returns></returns> public bool KeyRename(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyRename(redisKey, redisNewKey); } /// <summary> /// 設置 Key 的時間 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public bool KeyExpire(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExpire(redisKey, expiry); } #region key-async /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<bool> KeyDeleteAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyDeleteAsync(redisKey); } /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKeys"></param> /// <returns></returns> public async Task<long> KeyDeleteAsync(IEnumerable<string> redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return await _db.KeyDeleteAsync(keys.ToArray()); } /// <summary> /// 校驗 Key 是否存在 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<bool> KeyExistsAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExistsAsync(redisKey); } /// <summary> /// 重命名 Key /// </summary> /// <param name="redisKey"></param> /// <param name="redisNewKey"></param> /// <returns></returns> public async Task<bool> KeyRenameAsync(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyRenameAsync(redisKey, redisNewKey); } /// <summary> /// 設置 Key 的時間 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> KeyExpireAsync(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExpireAsync(redisKey, expiry); } #endregion key-async #endregion key 操做 #region 發佈訂閱 /// <summary> /// 訂閱 /// </summary> /// <param name="channel"></param> /// <param name="handle"></param> public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle) { var sub = _connMultiplexer.GetSubscriber(); sub.Subscribe(channel, handle); } /// <summary> /// 發佈 /// </summary> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public long Publish(RedisChannel channel, RedisValue message) { var sub = _connMultiplexer.GetSubscriber(); return sub.Publish(channel, message); } /// <summary> /// 發佈(使用序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public long Publish<T>(RedisChannel channel, T message) { var sub = _connMultiplexer.GetSubscriber(); return sub.Publish(channel, Serialize(message)); } #region 發佈訂閱-async /// <summary> /// 訂閱 /// </summary> /// <param name="channel"></param> /// <param name="handle"></param> public async Task SubscribeAsync(RedisChannel channel, Action<RedisChannel, RedisValue> handle) { var sub = _connMultiplexer.GetSubscriber(); await sub.SubscribeAsync(channel, handle); } /// <summary> /// 發佈 /// </summary> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public async Task<long> PublishAsync(RedisChannel channel, RedisValue message) { var sub = _connMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, message); } /// <summary> /// 發佈(使用序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public async Task<long> PublishAsync<T>(RedisChannel channel, T message) { var sub = _connMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, Serialize(message)); } #endregion 發佈訂閱-async #endregion 發佈訂閱 #region private method /// <summary> /// 添加 Key 的前綴 /// </summary> /// <param name="key"></param> /// <returns></returns> private static string AddKeyPrefix(string key) { return $"{DefaultKey}:{key}"; } #region 註冊事件 /// <summary> /// 添加註冊事件 /// </summary> private static void AddRegisterEvent() { _connMultiplexer.ConnectionRestored += ConnMultiplexer_ConnectionRestored; _connMultiplexer.ConnectionFailed += ConnMultiplexer_ConnectionFailed; _connMultiplexer.ErrorMessage += ConnMultiplexer_ErrorMessage; _connMultiplexer.ConfigurationChanged += ConnMultiplexer_ConfigurationChanged; _connMultiplexer.HashSlotMoved += ConnMultiplexer_HashSlotMoved; _connMultiplexer.InternalError += ConnMultiplexer_InternalError; _connMultiplexer.ConfigurationChangedBroadcast += ConnMultiplexer_ConfigurationChangedBroadcast; } /// <summary> /// 從新配置廣播時(一般意味着主從同步更改) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}"); } /// <summary> /// 發生內部錯誤時(主要用於調試) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}"); } /// <summary> /// 更改集羣時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_HashSlotMoved(object sender, HashSlotMovedEventArgs e) { Console.WriteLine( $"{nameof(ConnMultiplexer_HashSlotMoved)}: {nameof(e.OldEndPoint)}-{e.OldEndPoint} To {nameof(e.NewEndPoint)}-{e.NewEndPoint}, "); } /// <summary> /// 配置更改時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}"); } /// <summary> /// 發生錯誤時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}"); } /// <summary> /// 物理鏈接失敗時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}"); } /// <summary> /// 創建物理鏈接時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}"); } #endregion 註冊事件 /// <summary> /// 序列化 /// </summary> /// <param name="obj"></param> /// <returns></returns> private static byte[] Serialize(object obj) { if (obj == null) return null; var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, obj); var data = memoryStream.ToArray(); return data; } } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> private static T Deserialize<T>(byte[] data) { if (data == null) return default(T); var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream(data)) { var result = (T)binaryFormatter.Deserialize(memoryStream); return result; } } #endregion private method }
在 StackExchange.Redis 中最核心(中樞)的是 ConnectionMultiplexer 類,在全部調用之間它的實例對象應該被設計爲在整個應用程序域中爲共享和重用的,並不該該爲每個操做都建立一個 ConnectionMultiplexer 對象實例,也就是說咱們可使用常見的單例模式進行建立。github
雖然 ConnectionMultiplexer 實現了 IDisposable 接口,但這並不意味着須要使用 using 進行釋放,由於建立一個 ConnectionMultiplexer 對象是十分昂貴的 , 因此最好的是咱們一直重用一個 ConnectionMultiplexer 對象。redis
/// <summary> /// 鏈接字符串 /// </summary> private static readonly string ConnectionString; /// <summary> /// redis 鏈接對象 /// </summary> private static IConnectionMultiplexer _connMultiplexer; /// <summary> /// 默認的 Key 值(用來看成 RedisKey 的前綴) /// </summary> private static readonly string DefaultKey; /// <summary> /// 鎖 /// </summary> private static readonly object Locker = new object(); /// <summary> /// 數據庫 /// </summary> private readonly IDatabase _db; /// <summary> /// 獲取 Redis 鏈接對象 /// </summary> /// <returns></returns> public IConnectionMultiplexer GetConnectionRedisMultiplexer() { if ((_connMultiplexer == null) || !_connMultiplexer.IsConnected) { lock (Locker) { if ((_connMultiplexer == null) || !_connMultiplexer.IsConnected) _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); } } return _connMultiplexer; }
我選擇讓這些靜態變量的字段在靜態構造函數中進行統一的初始化,如鏈接字符串,ConnectionMultiplexer 對象和註冊事件等。數據庫
#region 構造函數 static RedisHelper() { ConnectionString = ConfigurationManager.ConnectionStrings["RedisConnectionString"].ConnectionString; _connMultiplexer = ConnectionMultiplexer.Connect(ConnectionString); DefaultKey = ConfigurationManager.AppSettings["Redis.DefaultKey"]; AddRegisterEvent(); } public RedisHelper(int db = -1) { _db = _connMultiplexer.GetDatabase(db); } #endregion 構造函數
#region 註冊事件 /// <summary> /// 添加註冊事件 /// </summary> private static void AddRegisterEvent() { ConnMultiplexer.ConnectionRestored += ConnMultiplexer_ConnectionRestored; ConnMultiplexer.ConnectionFailed += ConnMultiplexer_ConnectionFailed; ConnMultiplexer.ErrorMessage += ConnMultiplexer_ErrorMessage; ConnMultiplexer.ConfigurationChanged += ConnMultiplexer_ConfigurationChanged; ConnMultiplexer.HashSlotMoved += ConnMultiplexer_HashSlotMoved; ConnMultiplexer.InternalError += ConnMultiplexer_InternalError; ConnMultiplexer.ConfigurationChangedBroadcast += ConnMultiplexer_ConfigurationChangedBroadcast; } /// <summary> /// 從新配置廣播時(一般意味着主從同步更改) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}"); } /// <summary> /// 發生內部錯誤時(主要用於調試) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}"); } /// <summary> /// 更改集羣時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_HashSlotMoved(object sender, HashSlotMovedEventArgs e) { Console.WriteLine( $"{nameof(ConnMultiplexer_HashSlotMoved)}: {nameof(e.OldEndPoint)}-{e.OldEndPoint} To {nameof(e.NewEndPoint)}-{e.NewEndPoint}, "); } /// <summary> /// 配置更改時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}"); } /// <summary> /// 發生錯誤時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}"); } /// <summary> /// 物理鏈接失敗時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}"); } /// <summary> /// 創建物理鏈接時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}"); } #endregion 註冊事件
這裏的 Redis.DefaultKey 和 RedisConnectionString 對應配置文件中 App.Config。json
DefaultKey(默認 Key)的做用是給 redisKey 值添加一個前綴,我這裏使用了命名空間做爲了前綴,在 Redis 可視化工具中能夠更直觀的進行顯示(以「:」進行字符串的拼接)。異步
存儲字符串值。全部的同步方法都帶有對應的異步方法,因此也進行了簡單的封裝。比較經常使用的應該是字符串操做,直接將 value 序列化存放到 redis 中。async
#region String 操做 /// <summary> /// 設置 key 並保存字符串(若是 key 已存在,則覆蓋值) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringSet(redisKey, redisValue, expiry); } /// <summary> /// 保存多個 Key-value /// </summary> /// <param name="keyValuePairs"></param> /// <returns></returns> public bool StringSet(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value)); return _db.StringSet(keyValuePairs.ToArray()); } /// <summary> /// 獲取字符串 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public string StringGet(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return _db.StringGet(redisKey); } /// <summary> /// 存儲一個對象(該對象會被序列化保存) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public bool StringSet<T>(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return _db.StringSet(redisKey, json, expiry); } /// <summary> /// 獲取一個對象(會進行反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public T StringGet<T>(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.StringGet(redisKey)); } #region async /// <summary> /// 保存一個字符串值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> StringSetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringSetAsync(redisKey, redisValue, expiry); } /// <summary> /// 保存一組字符串值 /// </summary> /// <param name="keyValuePairs"></param> /// <returns></returns> public async Task<bool> StringSetAsync(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs) { keyValuePairs = keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value)); return await _db.StringSetAsync(keyValuePairs.ToArray()); } /// <summary> /// 獲取單個值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<string> StringGetAsync(string redisKey, string redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return await _db.StringGetAsync(redisKey); } /// <summary> /// 存儲一個對象(該對象會被序列化保存) /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> StringSetAsync<T>(string redisKey, T redisValue, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(redisValue); return await _db.StringSetAsync(redisKey, json, expiry); } /// <summary> /// 獲取一個對象(會進行反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<T> StringGetAsync<T>(string redisKey, TimeSpan? expiry = null) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.StringGetAsync(redisKey)); } #endregion async #endregion String 操做
哈希是一個 string 類型的 field 和 value 的映射表,哈希特別適合用於存儲對象。ide
#region Hash 操做 /// <summary> /// 判斷該字段是否存在 hash 中 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public bool HashExists(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashExists(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public bool HashDelete(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public long HashDelete(string redisKey, IEnumerable<RedisValue> hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashDelete(redisKey, hashField.ToArray()); } /// <summary> /// 在 hash 設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public bool HashSet(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashSet(redisKey, hashField, value); } /// <summary> /// 在 hash 中設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashFields"></param> public void HashSet(string redisKey, IEnumerable<HashEntry> hashFields) { redisKey = AddKeyPrefix(redisKey); _db.HashSet(redisKey, hashFields.ToArray()); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public RedisValue HashGet(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return _db.HashGet(redisKey, hashField); } /// <summary> /// 從 hash 返回全部的字段值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> HashKeys(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashKeys(redisKey); } /// <summary> /// 返回 hash 中的全部值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public RedisValue[] HashValues(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.HashValues(redisKey); } /// <summary> /// 在 hash 設定值(序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public bool HashSet<T>(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return _db.HashSet(redisKey, hashField, json); } /// <summary> /// 在 hash 中獲取值(反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public T HashGet<T>(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.HashGet(redisKey, hashField)); } #region async /// <summary> /// 判斷該字段是否存在 hash 中 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<bool> HashExistsAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashExistsAsync(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<bool> HashDeleteAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField); } /// <summary> /// 從 hash 中移除指定字段 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<long> HashDeleteAsync(string redisKey, IEnumerable<RedisValue> hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashDeleteAsync(redisKey, hashField.ToArray()); } /// <summary> /// 在 hash 設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<bool> HashSetAsync(string redisKey, string hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashSetAsync(redisKey, hashField, value); } /// <summary> /// 在 hash 中設定值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashFields"></param> public async Task HashSetAsync(string redisKey, IEnumerable<HashEntry> hashFields) { redisKey = AddKeyPrefix(redisKey); await _db.HashSetAsync(redisKey, hashFields.ToArray()); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<RedisValue> HashGetAsync(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } /// <summary> /// 在 hash 中獲取值 /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashGetAsync(string redisKey, RedisValue[] hashField, string value) { redisKey = AddKeyPrefix(redisKey); return await _db.HashGetAsync(redisKey, hashField); } /// <summary> /// 從 hash 返回全部的字段值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashKeysAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashKeysAsync(redisKey); } /// <summary> /// 返回 hash 中的全部值 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> HashValuesAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.HashValuesAsync(redisKey); } /// <summary> /// 在 hash 設定值(序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <param name="value"></param> /// <returns></returns> public async Task<bool> HashSetAsync<T>(string redisKey, string hashField, T value) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(value); return await _db.HashSetAsync(redisKey, hashField, json); } /// <summary> /// 在 hash 中獲取值(反序列化) /// </summary> /// <param name="redisKey"></param> /// <param name="hashField"></param> /// <returns></returns> public async Task<T> HashGetAsync<T>(string redisKey, string hashField) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.HashGetAsync(redisKey, hashField)); } #endregion async #endregion Hash 操做
列表是簡單的字符串列表,按照插入順序排序。你能夠添加一個元素導列表的頭部(左邊)或者尾部(右邊)。函數
#region List 操做 /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public string ListLeftPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPop(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public string ListRightPop(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPop(redisKey); } /// <summary> /// 移除列表指定鍵上與該值相同的元素 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRemove(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRemove(redisKey, redisValue); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRightPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, redisValue); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListLeftPush(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, redisValue); } /// <summary> /// 返回列表上該鍵的長度,若是不存在,返回 0 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public long ListLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListLength(redisKey); } /// <summary> /// 返回在該列表上鍵所對應的元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> ListRange(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.ListRange(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public T ListLeftPop<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.ListLeftPop(redisKey)); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public T ListRightPop<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(_db.ListRightPop(redisKey)); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListRightPush<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListRightPush(redisKey, Serialize(redisValue)); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public long ListLeftPush<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return _db.ListLeftPush(redisKey, Serialize(redisValue)); } #region List-async /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<string> ListLeftPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPopAsync(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<string> ListRightPopAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPopAsync(redisKey); } /// <summary> /// 移除列表指定鍵上與該值相同的元素 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRemoveAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRemoveAsync(redisKey, redisValue); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRightPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, redisValue); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListLeftPushAsync(string redisKey, string redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, redisValue); } /// <summary> /// 返回列表上該鍵的長度,若是不存在,返回 0 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<long> ListLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLengthAsync(redisKey); } /// <summary> /// 返回在該列表上鍵所對應的元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> ListRangeAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRangeAsync(redisKey); } /// <summary> /// 移除並返回存儲在該鍵列表的第一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<T> ListLeftPopAsync<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.ListLeftPopAsync(redisKey)); } /// <summary> /// 移除並返回存儲在該鍵列表的最後一個元素 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<T> ListRightPopAsync<T>(string redisKey) { redisKey = AddKeyPrefix(redisKey); return Deserialize<T>(await _db.ListRightPopAsync(redisKey)); } /// <summary> /// 在列表尾部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListRightPushAsync<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListRightPushAsync(redisKey, Serialize(redisValue)); } /// <summary> /// 在列表頭部插入值。若是鍵不存在,先建立再插入值 /// </summary> /// <param name="redisKey"></param> /// <param name="redisValue"></param> /// <returns></returns> public async Task<long> ListLeftPushAsync<T>(string redisKey, T redisValue) { redisKey = AddKeyPrefix(redisKey); return await _db.ListLeftPushAsync(redisKey, Serialize(redisValue)); } #endregion List-async #endregion List 操做
有序集合成員是惟一的,這就意味着集合中不能出現重複的數據。工具
#region SortedSet 操做 /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public bool SortedSetAdd(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetAdd(redisKey, member, score); } /// <summary> /// 在有序集合中返回指定範圍的元素,默認狀況下從低到高。 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public IEnumerable<RedisValue> SortedSetRangeByRank(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRangeByRank(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public long SortedSetLength(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetLength(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <param name="memebr"></param> /// <returns></returns> public bool SortedSetLength(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return _db.SortedSetRemove(redisKey, memebr); } /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public bool SortedSetAdd<T>(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return _db.SortedSetAdd(redisKey, json, score); } #region SortedSet-Async /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public async Task<bool> SortedSetAddAsync(string redisKey, string member, double score) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetAddAsync(redisKey, member, score); } /// <summary> /// 在有序集合中返回指定範圍的元素,默認狀況下從低到高。 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<IEnumerable<RedisValue>> SortedSetRangeByRankAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRangeByRankAsync(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<long> SortedSetLengthAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetLengthAsync(redisKey); } /// <summary> /// 返回有序集合的元素個數 /// </summary> /// <param name="redisKey"></param> /// <param name="memebr"></param> /// <returns></returns> public async Task<bool> SortedSetRemoveAsync(string redisKey, string memebr) { redisKey = AddKeyPrefix(redisKey); return await _db.SortedSetRemoveAsync(redisKey, memebr); } /// <summary> /// SortedSet 新增 /// </summary> /// <param name="redisKey"></param> /// <param name="member"></param> /// <param name="score"></param> /// <returns></returns> public async Task<bool> SortedSetAddAsync<T>(string redisKey, T member, double score) { redisKey = AddKeyPrefix(redisKey); var json = Serialize(member); return await _db.SortedSetAddAsync(redisKey, json, score); } #endregion SortedSet-Async #endregion SortedSet 操做
#region key 操做 /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public bool KeyDelete(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyDelete(redisKey); } /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKeys"></param> /// <returns></returns> public long KeyDelete(IEnumerable<string> redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return _db.KeyDelete(keys.ToArray()); } /// <summary> /// 校驗 Key 是否存在 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public bool KeyExists(string redisKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExists(redisKey); } /// <summary> /// 重命名 Key /// </summary> /// <param name="redisKey"></param> /// <param name="redisNewKey"></param> /// <returns></returns> public bool KeyRename(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return _db.KeyRename(redisKey, redisNewKey); } /// <summary> /// 設置 Key 的時間 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public bool KeyExpire(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return _db.KeyExpire(redisKey, expiry); } #region key-async /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<bool> KeyDeleteAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyDeleteAsync(redisKey); } /// <summary> /// 移除指定 Key /// </summary> /// <param name="redisKeys"></param> /// <returns></returns> public async Task<long> KeyDeleteAsync(IEnumerable<string> redisKeys) { var keys = redisKeys.Select(x => (RedisKey)AddKeyPrefix(x)); return await _db.KeyDeleteAsync(keys.ToArray()); } /// <summary> /// 校驗 Key 是否存在 /// </summary> /// <param name="redisKey"></param> /// <returns></returns> public async Task<bool> KeyExistsAsync(string redisKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExistsAsync(redisKey); } /// <summary> /// 重命名 Key /// </summary> /// <param name="redisKey"></param> /// <param name="redisNewKey"></param> /// <returns></returns> public async Task<bool> KeyRenameAsync(string redisKey, string redisNewKey) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyRenameAsync(redisKey, redisNewKey); } /// <summary> /// 設置 Key 的時間 /// </summary> /// <param name="redisKey"></param> /// <param name="expiry"></param> /// <returns></returns> public async Task<bool> KeyExpireAsync(string redisKey, TimeSpan? expiry) { redisKey = AddKeyPrefix(redisKey); return await _db.KeyExpireAsync(redisKey, expiry); } #endregion key-async #endregion key 操做
通常會使用第三方的 MQ,如 RabbitMQ。
#region 發佈訂閱 /// <summary> /// 訂閱 /// </summary> /// <param name="channel"></param> /// <param name="handle"></param> public void Subscribe(RedisChannel channel, Action<RedisChannel, RedisValue> handle) { var sub = ConnMultiplexer.GetSubscriber(); sub.Subscribe(channel, handle); } /// <summary> /// 發佈 /// </summary> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public long Publish(RedisChannel channel, RedisValue message) { var sub = ConnMultiplexer.GetSubscriber(); return sub.Publish(channel, message); } /// <summary> /// 發佈(使用序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public long Publish<T>(RedisChannel channel, T message) { var sub = ConnMultiplexer.GetSubscriber(); return sub.Publish(channel, Serialize(message)); } #region 發佈訂閱-async /// <summary> /// 訂閱 /// </summary> /// <param name="channel"></param> /// <param name="handle"></param> public async Task SubscribeAsync(RedisChannel channel, Action<RedisChannel, RedisValue> handle) { var sub = ConnMultiplexer.GetSubscriber(); await sub.SubscribeAsync(channel, handle); } /// <summary> /// 發佈 /// </summary> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public async Task<long> PublishAsync(RedisChannel channel, RedisValue message) { var sub = ConnMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, message); } /// <summary> /// 發佈(使用序列化) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="channel"></param> /// <param name="message"></param> /// <returns></returns> public async Task<long> PublishAsync<T>(RedisChannel channel, T message) { var sub = ConnMultiplexer.GetSubscriber(); return await sub.PublishAsync(channel, Serialize(message)); } #endregion 發佈訂閱-async #endregion 發佈訂閱
這裏使用了二進制序列化器進行序列化操做。
/// <summary> /// 添加 Key 的前綴 /// </summary> /// <param name="key"></param> /// <returns></returns> private static string AddKeyPrefix(string key) { return $"{DefaultKey}:{key}"; } /// <summary> /// 序列化 /// </summary> /// <param name="obj"></param> /// <returns></returns> private static byte[] Serialize(object obj) { if (obj == null) return null; var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, obj); var data = memoryStream.ToArray(); return data; } } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> private static T Deserialize<T>(byte[] data) { if (data == null) return default(T); var binaryFormatter = new BinaryFormatter(); using (var memoryStream = new MemoryStream(data)) { var result = (T)binaryFormatter.Deserialize(memoryStream); return result; } }
測試代碼:
【備註】內部還有不少的接口和方法沒有進行封裝,還須要本身進行封裝。