[C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper

时间:2021-11-06 08:31:47

#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></param> /// <param></param> private static void ConnMultiplexer_ConfigurationChangedBroadcast(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChangedBroadcast)}: {e.EndPoint}"); } /// <summary> /// 发生内部错误时(主要用于调试) /// </summary> /// <param></param> /// <param></param> private static void ConnMultiplexer_InternalError(object sender, InternalErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_InternalError)}: {e.Exception}"); } /// <summary> /// 更改集群时 /// </summary> /// <param></param> /// <param></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></param> /// <param></param> private static void ConnMultiplexer_ConfigurationChanged(object sender, EndPointEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConfigurationChanged)}: {e.EndPoint}"); } /// <summary> /// 发生错误时 /// </summary> /// <param></param> /// <param></param> private static void ConnMultiplexer_ErrorMessage(object sender, RedisErrorEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ErrorMessage)}: {e.Message}"); } /// <summary> /// 物理连接失败时 /// </summary> /// <param></param> /// <param></param> private static void ConnMultiplexer_ConnectionFailed(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionFailed)}: {e.Exception}"); } /// <summary> /// 建立物理连接时 /// </summary> /// <param></param> /// <param></param> private static void ConnMultiplexer_ConnectionRestored(object sender, ConnectionFailedEventArgs e) { Console.WriteLine($"{nameof(ConnMultiplexer_ConnectionRestored)}: {e.Exception}"); } #endregion 注册事件