Exemple #1
0
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="dllName"></param>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public static RedisSetting GetSetting(string dllName = StaticString.DefaultRedisConfigDll, string sectionName = StaticString.DefautlRedisConfigSection)
        {
            if (!AppConfig.IsExists(dllName))
            {
                throw new NotFoundException("未找到对应的配置文件");
            }
            dllName = Assembly.GetCallingAssembly().GetName().Name;
            if (!AppConfig.DllConfigs[dllName].IsExists(sectionName))
            {
                throw new NotFoundException("未找到对应的配置文件下的配置节点");
            }
            var Setting = new RedisSetting();

            try
            {
                Setting.ClientName        = StringUtil.FormatKey(dllName, sectionName);
                Setting.LocalCacheEnable  = CoralConvert.Convert <bool>(AppConfig.DllConfigs[dllName][sectionName]["LocalCacheEnable"]);
                Setting.LocalCacheTimeout = CoralConvert.Convert <int>(AppConfig.DllConfigs[dllName][sectionName]["LocalCacheTimeout"]);
                Setting.ServerList        = AppConfig.DllConfigs[dllName][sectionName]["ServerList"];
                Setting.Version           = AppConfig.DllConfigs[dllName][sectionName]["Version"];
                Setting.DefaultDb         = Convert.ToInt32(AppConfig.DllConfigs[dllName][sectionName]["DefaultDb"] ?? "0");
                Setting.Password          = AppConfig.DllConfigs[dllName][sectionName]["Password"];
                return(Setting);
            }
            catch (Exception ex)
            {
                throw CoralException.ThrowException(item => item.ConfigError, innerException: ex, param: Setting);
            }
        }
Exemple #2
0
        public RedisForm(RedisSetting setting, Action callback)
        {
            _callback = callback;
            InitializeComponent();

            settingPropertyGrid.SelectedObject = setting;
        }
        public static RedisSetting Load()
        {
            var          tempPath = Path.Combine(ConfigPath, _fileName);
            RedisSetting setting  = new RedisSetting();

            try
            {
                if (File.Exists(tempPath))
                {
                    byte[] data = File.ReadAllBytes(tempPath);
                    setting = ProtoBufUtils.Deserialize <RedisSetting>(data);
                }
                else
                {
                    Save(setting);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("RedisSetting load error:{0}", ex);
                if (File.Exists(tempPath))
                {
                    File.Delete(tempPath);
                }
            }
            return(setting);
        }
Exemple #4
0
        public static IDatabase GetRedisDatabase(RedisSetting setting)
        {
            //这里需要提取,单一性
            var config = RedisSettings.GetOrAdd(setting.ClientName, x => setting);
            ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(AnalysisRedisSection(config));

            return(connectionMultiplexer.GetDatabase());
        }
Exemple #5
0
 public static ICache GetRedisCache(RedisSetting setting)
 {
     try
     {
         return(new RedisCache(setting));
     }
     catch (NullReferenceException)
     {
         throw CoralException.ThrowException(item => item.ConfigError);
     }
 }
        public static void Save(RedisSetting setting)
        {
            if (!Directory.Exists(ConfigPath))
            {
                Directory.CreateDirectory(ConfigPath);
            }
            var tempPath = Path.Combine(ConfigPath, _fileName);
            var buffer   = ProtoBufUtils.Serialize(setting);

            using (var sw = File.Open(tempPath, FileMode.OpenOrCreate))
            {
                sw.Write(buffer, 0, buffer.Length);
                sw.Flush();
            }
        }
Exemple #7
0
    public RedisClientService(RedisSetting setting, IServiceProvider serviceProvider,
                              ILogger <RedisClientService> logger)
    {
        _topics = GetTopicDictionary();

        _serviceProvider = serviceProvider;
        _logger          = logger;
        _lazyConnection  = new Lazy <ConnectionMultiplexer>(() =>
                                                            ConnectionMultiplexer.Connect(setting.ConnectionString));
        _subscriber = RedisCache.Multiplexer.GetSubscriber();
        foreach (var topic in _topics)
        {
            _subscriber.Subscribe(topic.Key, HandleApplicationMessageReceivedAsync);
        }
    }
Exemple #8
0
        public static IServiceCollection AddRedis(this IServiceCollection services, RedisSetting setting)
        {
            Requires.NotNull(setting, nameof(setting));

            var cSRedisClient = new CSRedis.CSRedisClient($"{setting.Configuration},prefix={setting.InstanceName}");

            RedisHelper.Initialization(cSRedisClient,
                                       value => JsonConvert.SerializeObject(value),
                                       deserialize: (data, type) => JsonConvert.DeserializeObject(data, type));

            services.AddSingleton <IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
            services.AddSingleton(setting);

            return(services);
        }
Exemple #9
0
        public async Task TestReloadSettings()
        {
            // test in \Debug\bin\
            this._settingsManager.ConfigFolder = AppDomain.CurrentDomain.BaseDirectory;
            var rs = new RedisSetting
            {
                ServerName = "localhost",
                RedisInstances = new List<RedisServer>
                {
                    new RedisServer {Hostname = "127.0.0.1", Port = 6379}
                }
            };
            this._settingsManager.SaveConfigFiles(new Setting[] {rs});

            await Task.Delay(100);

            var found = this._settingsManager.GetSetting<RedisSetting>();

            Setting[] testSettings =
            {
                new RedisSetting
                {
                    ServerName = "localhost_test_update",
                    RedisInstances = new List<RedisServer>
                    {
                        new RedisServer {Hostname = "127.0.0.1", Port = 6379}
                    }
                }
            };

            var settingChanged = false;
            found.SettingFileChanged += (s, e) => { settingChanged = true; };

            this._settingsManager.SaveConfigFiles(testSettings);

            await Task.Delay(100);

            Assert.True(settingChanged);
            Assert.Equal("localhost_test_update",found.ServerName );
        }
Exemple #10
0
        public static ConfigurationOptions AnalysisRedisSection(RedisSetting redisSetting)
        {
            string[] writeServerList = redisSetting.ServerList.Split(',');
            var      sdkOptions      = new ConfigurationOptions
            {
                ClientName         = redisSetting.ClientName,
                KeepAlive          = 5 * 60,
                DefaultVersion     = new Version(redisSetting.Version),
                AbortOnConnectFail = false,
                DefaultDatabase    = redisSetting.DefaultDb,
                ConnectRetry       = 10,
                Password           = redisSetting.Password,
                SyncTimeout        = redisSetting.SyncTimeout * 1000
            };

            foreach (var s in writeServerList)
            {
                sdkOptions.EndPoints.Add(s);
            }
            ;
            return(sdkOptions);
        }
        public static IServiceCollection AddEventBusRedis(this IServiceCollection services, RedisSetting setting)

        {
            services.AddSingleton(setting);
            services.AddSingleton <IEventBusDispatcher, RedisEventBusDispatcher>();
            services.AddSingleton <RedisClientService>();
            services.AddSingleton <IHostedService>(serviceProvider => serviceProvider.GetService <RedisClientService>());

            services.AddSingleton(serviceProvider =>
            {
                var mqttClientService         = serviceProvider.GetService <RedisClientService>();
                var mqttClientServiceProvider = new RedisClientServiceProvider(mqttClientService);
                return(mqttClientServiceProvider);
            });
            return(services);
        }
Exemple #12
0
 public RedisCache(RedisSetting setting)
 {
     _database = RedisManager.GetRedisDatabase(setting);
 }
 public static ConfigurationSetting SetRedisSetting(this ConfigurationSetting configurationSetting, RedisSetting redisSetting)
 {
     _redisSetting = redisSetting;
     return(configurationSetting);
 }