zoukankan      html  css  js  c++  java
  • RedisConnectionHelp

    using StackExchange.Redis;
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace RedisCharpClient {
        class RedisConnectionHelp {
            //系统自定义Key前缀
            public static readonly string SysCustomKey = ConfigurationManager.AppSettings["redisKey"] ?? "";
            //"127.0.0.1:6379,allowadmin=true
            private static readonly string RedisConnectionString = ConfigurationManager.ConnectionStrings["RedisExchangeHosts"].ConnectionString;
            private static readonly object Locker = new object();
            private static ConnectionMultiplexer _instance;
            private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
    
            /// <summary>
            /// 单例获取
            /// </summary>
            public static ConnectionMultiplexer Instance {
                get {
                    if (_instance == null) {
                        lock (Locker) {
                            if (_instance == null || !_instance.IsConnected) {
                                _instance = GetManager();
                            }
                        }
                    }
                    return _instance;
                }
            }
    
            /// <summary>
            /// 缓存获取
            /// </summary>
            public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString) {
                if (!ConnectionCache.ContainsKey(connectionString)) {
                    ConnectionCache[connectionString] = GetManager(connectionString);
                }
                return ConnectionCache[connectionString];
            }
    
            /// <summary>
            /// 
            /// </summary>
            private static ConnectionMultiplexer GetManager(string connectionString = null) {
                connectionString = connectionString ?? RedisConnectionString;
                var connect = ConnectionMultiplexer.Connect(connectionString);
    
                //注册如下事件
                connect.ConnectionFailed += MuxerConnectionFailed;
                connect.ConnectionRestored += MuxerConnectionRestored;
                connect.ErrorMessage += MuxerErrorMessage;
                connect.ConfigurationChanged += MuxerConfigurationChanged;
                connect.HashSlotMoved += MuxerHashSlotMoved;
                connect.InternalError += MuxerInternalError;
    
                return connect;
            }
    
            #region 事件
            /// <summary>
            /// 配置更改时
            /// </summary>
            private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e) {
                Console.WriteLine("Configuration changed: " + e.EndPoint);
            }
    
            /// <summary>
            /// 发生错误时
            /// </summary>
            private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e) {
                Console.WriteLine("ErrorMessage: " + e.Message);
            }
    
            /// <summary>
            /// 重新建立连接之前的错误
            /// </summary>
            private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e) {
                Console.WriteLine("ConnectionRestored: " + e.EndPoint);
            }
    
            /// <summary>
            /// 连接失败,如果重新连接成功你将不会收到这个通知
            /// </summary>
            private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e) {
                Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
            }
    
            /// <summary>
            /// 更改集群
            /// </summary>
            private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e) {
                Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
            }
    
            /// <summary>
            /// redis类库错误
            /// </summary>
            private static void MuxerInternalError(object sender, InternalErrorEventArgs e) {
                Console.WriteLine("InternalError:Message" + e.Exception.Message);
            }
            #endregion 事件
        }
    }
    

      

  • 相关阅读:
    初识多线程
    java开发中我们经常用到的一些名词
    gitHub提交代码
    Java-基础-HashMap
    Java-基础-LinkedList
    Java-基础-ArrayList
    Java-基础-JDK动态代理
    Java-基础-反射
    RabbitMQ-延迟队列
    RabbitMQ-TTL-死信队列_DLX
  • 原文地址:https://www.cnblogs.com/anduinlothar/p/14632522.html
Copyright © 2011-2022 走看看