使用 StackExchange.Redis 封裝屬於自己的 RedisHelper 目錄 核心類 ConnectionMultiplexer 字元串(String) 哈希(Hash) 列表(List) 有序集合(sorted set) Key 操作 發佈訂閱 其他 簡介 目前 .NET 使用訪問 ...
使用 StackExchange.Redis 封裝屬於自己的 RedisHelper
目錄
- 核心類 ConnectionMultiplexer
- 字元串(String)
- 哈希(Hash)
- 列表(List)
- 有序集合(sorted set)
- Key 操作
- 發佈訂閱
- 其他
簡介
目前 .NET 使用訪問 Redis 的的類庫主流應該是 StackExchange.Redis,自己參考網上的文章(也許是吃飽了撐著),也嘗試做出簡單的封裝。
/// <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> ///