【干貨】.NET/C#中使用Redis
簡(jiǎn)介
在C#中使用Redis,一般有兩種方式:
1、ServiceStack.Redis,據(jù)說是Redis官方推薦使用的驅(qū)動(dòng)類庫(kù),但是是收費(fèi)的。
2、StackExchange.Redis,可能性能要比ServiceStack.Redis差點(diǎn),但是是免費(fèi)的。
本次使用StackExchange.Redis來實(shí)現(xiàn)Redis操作。
二
添加StackExchange.Redis引用
想要在C#中使用Redis,首先得要有個(gè)Redis支持的C#版的驅(qū)動(dòng)。
通過網(wǎng)絡(luò)下載或nuget安裝,得到Redis相關(guān)的dll,添加到項(xiàng)目中引用。這里介紹下通過NuGet方式添加
第一步:在項(xiàng)目中右鍵,選擇管理NuGet管理包

第二步:搜索StackExchange.Redis添加,我這里已經(jīng)添加過了所以沒有添加按鈕

通過這兩步,會(huì)在項(xiàng)目中自動(dòng)添加StackExchange.Redis引用
有需要的可以自行添加Newtonsoft.Json引用
StackExchange.Redis.dll:Redis驅(qū)動(dòng)程序

三
連接數(shù)據(jù)庫(kù)
在連接數(shù)據(jù)庫(kù)之前,請(qǐng)確保Redis數(shù)據(jù)庫(kù)服務(wù)已經(jīng)啟動(dòng)。
StackExchange.Redis核心代碼都在ConnectionMultiplexer類中,這個(gè)類的實(shí)例不需要在每個(gè)操作中不停的創(chuàng)建,一般都會(huì)封裝為單例模式共享使用該對(duì)象實(shí)例。
1.連接字符串(redis數(shù)據(jù)庫(kù)默認(rèn)端口為:6379)
private static readonly string ConnectionWriteString = "127.0.0.1:6379";
2.創(chuàng)建連接
private static readonly IConnectionMultiplexer ConnMultiplexer = ConnectionMultiplexer.Connect(ConnectionWriteString);
3.數(shù)據(jù)庫(kù)
private readonly IDatabase _db=ConnMultiplexer.GetDatabase(0);//參數(shù)為創(chuàng)建的數(shù)據(jù)庫(kù)的位置
四
操作String類型方法封裝
? ? ? ??///
? ? ? ? /// 設(shè)置 key 并保存字符串(如果 key 已存在,則覆蓋值)
? ? ? ? ///
? ? ? ? /// 名稱
? ? ? ? /// 值
? ? ? ? /// 時(shí)間
? ? ? ? ///
? ? ? ? public bool StringSet(string redisKey, string redisValue, TimeSpan? expiry = null)
? ? ? ? {
? ? ? ? ? ? return _db.StringSet(redisKey, redisValue, expiry);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 獲取字符串
? ? ? ? ///
? ? ? ? /// 名稱
? ? ? ? /// 時(shí)間
? ? ? ? ///
? ? ? ? public string StringGet(string redisKey, TimeSpan? expiry = null)
? ? ? ? {
? ? ? ? ? ? return _db.StringGet(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 存儲(chǔ)一個(gè)對(duì)象(該對(duì)象會(huì)被序列化保存)
? ? ? ? ///
? ? ? ? /// 名稱
? ? ? ? /// 值
? ? ? ? /// 時(shí)間
? ? ? ? ///
? ? ? ? public bool StringSet
? ? ? ? {
? ? ? ? ? ? var json = Serialize(redisValue);
? ? ? ? ? ? return _db.StringSet(redisKey, json, expiry);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 獲取一個(gè)對(duì)象(會(huì)進(jìn)行反序列化)
? ? ? ? ///
? ? ? ? /// 名稱
? ? ? ? /// 時(shí)間
? ? ? ? ///
? ? ? ? public T StringGet
? ? ? ? {
? ? ? ? ? ? return Deserialize
? ? ? ? }
五
操作Hash類型方法封裝
? ? ??? ///
? ? ? ? /// 判斷該字段是否存在 hash 中
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool HashExists(string redisKey, string hashField)
? ? ? ? {
? ? ? ? ? ? return _db.HashExists(redisKey, hashField);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 從 hash 中移除指定字段
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool HashDelete(string redisKey, string hashField)
? ? ? ? {
? ? ? ? ? ? return _db.HashDelete(redisKey, hashField);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 從 hash 中移除指定字段(多個(gè)刪除)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long HashDelete(string redisKey, IEnumerable
? ? ? ? {
? ? ? ? ? ? return _db.HashDelete(redisKey, hashField.ToArray());
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 設(shè)定值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool HashSet(string redisKey, string hashField, string value)
? ? ? ? {
? ? ? ? ? ? return _db.HashSet(redisKey, hashField, value);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 中設(shè)定值(多個(gè))
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public void HashSet(string redisKey, IEnumerable
? ? ? ? {
? ? ? ? ? ? _db.HashSet(redisKey, hashFields.ToArray());
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 中獲取值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public RedisValue HashGet(string redisKey, string hashField)
? ? ? ? {
? ? ? ? ? ? return _db.HashGet(redisKey, hashField);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 中獲取值(多個(gè))
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public RedisValue[] HashGet(string redisKey, RedisValue[] hashField, string value)
? ? ? ? {
? ? ? ? ? ? return _db.HashGet(redisKey, hashField);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 從 hash 返回所有的字段值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public IEnumerable
? ? ? ? {
? ? ? ? ? ? return _db.HashKeys(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回 hash 中的所有值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public RedisValue[] HashValues(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.HashValues(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 設(shè)定值(序列化)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool HashSet
? ? ? ? {
? ? ? ? ? ? var json = Serialize(value);
? ? ? ? ? ? return _db.HashSet(redisKey, hashField, json);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在 hash 中獲取值(反序列化)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public T HashGet
? ? ? ? {
? ? ? ? ? ? return Deserialize
? ? ? ? }
六
操作List類型方法封裝
? ? ??? ///
? ? ? ? /// 移除并返回存儲(chǔ)在該鍵列表的第一個(gè)元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public string ListLeftPop(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.ListLeftPop(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除并返回存儲(chǔ)在該鍵列表的最后一個(gè)元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public string ListRightPop(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.ListRightPop(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除列表指定鍵上與該值相同的元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListRemove(string redisKey, string redisValue)
? ? ? ? {
? ? ? ? ? ? return _db.ListRemove(redisKey, redisValue);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在列表尾部插入值。如果鍵不存在,先創(chuàng)建再插入值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListRightPush(string redisKey, string redisValue)
? ? ? ? {
? ? ? ? ? ? return _db.ListRightPush(redisKey, redisValue);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在列表頭部插入值。如果鍵不存在,先創(chuàng)建再插入值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListLeftPush(string redisKey, string redisValue)
? ? ? ? {
? ? ? ? ? ? return _db.ListLeftPush(redisKey, redisValue);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回列表上該鍵的長(zhǎng)度,如果不存在,返回 0
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListLength(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.ListLength(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回在該列表上鍵所對(duì)應(yīng)的元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public IEnumerable
? ? ? ? {
? ? ? ? ? ? return _db.ListRange(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回在該列表上鍵所對(duì)應(yīng)的元素
? ? ? ? ///
? ? ? ? /// 開始行
? ? ? ? /// 結(jié)束行
? ? ? ? ///
? ? ? ? public IEnumerable
? ? ? ? {
? ? ? ? ? ? return _db.ListRange(redisKey, startRow, endRow);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除并返回存儲(chǔ)在該鍵列表的第一個(gè)元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public T ListLeftPop
? ? ? ? {
? ? ? ? ? ? return Deserialize
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除并返回存儲(chǔ)在該鍵列表的最后一個(gè)元素
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public T ListRightPop
? ? ? ? {
? ? ? ? ? ? return Deserialize
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在列表尾部插入值。如果鍵不存在,先創(chuàng)建再插入值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListRightPush
? ? ? ? {
? ? ? ? ? ? return _db.ListRightPush(redisKey, Serialize(redisValue));
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在列表頭部插入值。如果鍵不存在,先創(chuàng)建再插入值
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long ListLeftPush
? ? ? ? {
? ? ? ? ? ? return _db.ListLeftPush(redisKey, Serialize(redisValue));
? ? ? ? }
七
操作SortedSet類型方法封裝
? ? ??? ///
? ? ? ? /// SortedSet 新增
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool SortedSetAdd(string redisKey, string member, double score)
? ? ? ? {
? ? ? ? ? ? return _db.SortedSetAdd(redisKey, member, score);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在有序集合中返回指定范圍的元素,默認(rèn)情況下從低到高。
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public IEnumerable
? ? ? ? {
? ? ? ? ? ? return _db.SortedSetRangeByRank(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回有序集合的元素個(gè)數(shù)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long SortedSetLength(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.SortedSetLength(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回有序集合的元素個(gè)數(shù)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool SortedSetLength(string redisKey, string memebr)
? ? ? ? {
? ? ? ? ? ? return _db.SortedSetRemove(redisKey, memebr);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// SortedSet 新增
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool SortedSetAdd
? ? ? ? {
? ? ? ? ? ? var json = Serialize(member);
? ? ? ? ? ? return _db.SortedSetAdd(redisKey, json, score);
? ? ? ? }
? ? ? ? #region SortedSet-Async
? ? ? ? ///
? ? ? ? /// SortedSet 新增
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.SortedSetAddAsync(redisKey, member, score);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 在有序集合中返回指定范圍的元素,默認(rèn)情況下從低到高。
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.SortedSetRangeByRankAsync(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回有序集合的元素個(gè)數(shù)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.SortedSetLengthAsync(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 返回有序集合的元素個(gè)數(shù)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.SortedSetRemoveAsync(redisKey, memebr);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// SortedSet 新增
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? var json = Serialize(member);
? ? ? ? ? ? return await _db.SortedSetAddAsync(redisKey, json, score);
? ? ? ? }
八
操作key類型方法封裝
? ??? ? ///
? ? ? ? /// 移除指定 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool KeyDelete(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.KeyDelete(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除指定 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long KeyDelete(IEnumerable
? ? ? ? {
? ? ? ? ? ? var keys = redisKeys.Select(x => (RedisKey)x);
? ? ? ? ? ? return _db.KeyDelete(keys.ToArray());
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 校驗(yàn) Key 是否存在
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool KeyExists(string redisKey)
? ? ? ? {
? ? ? ? ? ? return _db.KeyExists(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 重命名 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool KeyRename(string redisKey, string redisNewKey)
? ? ? ? {
? ? ? ? ? ? return _db.KeyRename(redisKey, redisNewKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 設(shè)置 Key 的時(shí)間
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public bool KeyExpire(string redisKey, TimeSpan? expiry)
? ? ? ? {
? ? ? ? ? ? return _db.KeyExpire(redisKey, expiry);
? ? ? ? }
? ? ? ? #region key-async
? ? ? ? ///
? ? ? ? /// 移除指定 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.KeyDeleteAsync(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 移除指定 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? var keys = redisKeys.Select(x => (RedisKey)x);
? ? ? ? ? ? return await _db.KeyDeleteAsync(keys.ToArray());
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 校驗(yàn) Key 是否存在
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.KeyExistsAsync(redisKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 重命名 Key
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.KeyRenameAsync(redisKey, redisNewKey);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 設(shè)置 Key 的時(shí)間
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? return await _db.KeyExpireAsync(redisKey, expiry);
? ? ? ? }
九
發(fā)布訂閱方法封裝
? ??? ? ///
? ? ? ? /// 訂閱
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public void Subscribe(RedisChannel channel, Action
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? sub.Subscribe(channel, handle);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)布
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long Publish(RedisChannel channel, RedisValue message)
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? return sub.Publish(channel, message);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)布(使用序列化)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public long Publish
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? return sub.Publish(channel, Serialize(message));
? ? ? ? }
? ? ? ? #region 發(fā)布訂閱-async
? ? ? ? ///
? ? ? ? /// 訂閱
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task SubscribeAsync(RedisChannel channel, Action
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? await sub.SubscribeAsync(channel, handle);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)布
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? return await sub.PublishAsync(channel, message);
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)布(使用序列化)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? public async Task
? ? ? ? {
? ? ? ? ? ? var sub = ConnMultiplexer.GetSubscriber();
? ? ? ? ? ? return await sub.PublishAsync(channel, Serialize(message));
? ? ? ? }
十
事件方法封裝
? ??? ? ///
? ? ? ? /// 添加注冊(cè)事件
? ? ? ? ///
? ? ? ? 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;
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 重新配置廣播時(shí)(通常意味著主從同步更改)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)生內(nèi)部錯(cuò)誤時(shí)(主要用于調(diào)試)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 更改集群時(shí)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? 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}, ");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 配置更改時(shí)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 發(fā)生錯(cuò)誤時(shí)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 物理連接失敗時(shí)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}");
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 建立物理連接時(shí)
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}");
? ? ? ? }
十一
序列化方法封裝
? ??? ? ///
? ? ? ? /// 序列化
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? ///
? ? ? ? /// 反序列化
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? ///
? ? ? ? private static T Deserialize
? ? ? ? {
? ? ? ? ? ? if (data == null)
? ? ? ? ? ? ? ? return default(T);
? ? ? ? ? ? var binaryFormatter = new BinaryFormatter();
? ? ? ? ? ? using (var memoryStream = new MemoryStream(data))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var result = (T)binaryFormatter.Deserialize(memoryStream);
? ? ? ? ? ? ? ? return result;
? ? ? ? ? ? }
? ? ? ? }
十二
調(diào)用Redis方法
RedisBaseTools redis = new RedisBaseTools();
redis.StringSet("keyname", "1234567", new TimeSpan(0, 10, 0));
redis.StringGet("keyname");
其他類型方法調(diào)用方式相似

END
