/// <summary> /// 单例获取 /// </summary> //public static ConnectionMultiplexer Instance //{ // get // { // if (_instance == null) // { // lock (Locker) // { // if (_instance == null || !_instance.IsConnected) // { // _instance = GetManager(); // } // } // } // return _instance; // } //} ///通过配置文件来加载链接 public static ConnectionMultiplexer GetConnectionMultiplexer(RedisServiceConfig config) { if (!ConnectionCache.ContainsKey(config)) { ConnectionCache[config] = GetManager(config); } return(ConnectionCache[config]); }
public void Init(RedisServiceConfig config) { if (config == null || string.IsNullOrEmpty(config.Address)) { throw new KeyNotFoundException("redis config is not set"); } _config = config; _conn = RedisConnectionHelp.GetConnectionMultiplexer(_config); _db = GetDatabase(); }
/// <summary> /// 缓存获取 /// </summary> /// <param name="connectionString"></param> /// <returns></returns> //public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString) //{ // if (!ConnectionCache.ContainsKey(connectionString)) // { // ConnectionCache[connectionString] = GetManager(connectionString); // } // return ConnectionCache[connectionString]; //} private static ConnectionMultiplexer GetManager(RedisServiceConfig config) { var options = ConfigurationOptions.Parse(config.Address); options.AbortOnConnectFail = false; if (!string.IsNullOrWhiteSpace(config.Pwd)) { options.Password = config.Pwd; } var connect = ConnectionMultiplexer.Connect(options); //注册如下事件 connect.ConnectionFailed += MuxerConnectionFailed; connect.ConnectionRestored += MuxerConnectionRestored; connect.ErrorMessage += MuxerErrorMessage; connect.ConfigurationChanged += MuxerConfigurationChanged; connect.HashSlotMoved += MuxerHashSlotMoved; connect.InternalError += MuxerInternalError; return(connect); }
/// <summary> /// 应该使用这种方式初始化,否则如果有两个Redis数据库就 没办法处理了 /// </summary> /// <param name="config"></param> public RedisService(RedisServiceConfig config) { Init(config); }