Exemplo n.º 1
0
        public void Parse_Build_ConnectionString(string connectionString, string buildConnectionString)
        {
            var builder =
                ConnectionStringBuilder.Parse(connectionString);

            Assert.That(builder.Build(), Is.EqualTo(buildConnectionString));
        }
Exemplo n.º 2
0
        void BuildRedisClient()
        {
            if (Options.ConnectionStrings != null && Options.ConnectionStrings.Any())
            {
                //读写分离
                if (Options.RW_Splitting)
                {
                    Client = new RedisClient(
                        Options.ConnectionStrings.First(),
                        Options.ConnectionStrings.Skip(1)
                        .Select(o => ConnectionStringBuilder.Parse(o))
                        .ToArray());
                }
                //集群
                else
                {
                    Client = new RedisClient(Options.ConnectionStrings
                                             .Select(o => ConnectionStringBuilder.Parse(o))
                                             .ToArray());
                }
            }
            else
            {
                //哨兵高可用
                if (Options.Sentinels != null && Options.Sentinels.Any())
                {
                    Client = new RedisClient(
                        Options.ConnectionString,
                        Options.Sentinels.ToArray(),
                        Options.RW_Splitting);
                }
                //普通
                else
                {
                    Client = new RedisClient(Options.ConnectionString);
                }
            }

            if (Options.ClientSideCachingOptions != null)
            {
                Client.UseClientSideCaching(Options.ClientSideCachingOptions);
            }

            if (Options.Subscribe != null && Options.Subscribe.Any())
            {
                Client.Subscribe(Options.Subscribe.ToArray(), Options.ReceiveData);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static FreeRedisHelper()
        {
            var connectionStrings = ConfigHelper.GetAppSettings <string[]>("RedisConnectionStrings");

            if (connectionStrings.Length == 1)
            {
                //普通模式
                Default = new FreeRedisHelper(connectionStrings[0]);
            }
            else
            {
                //集群模式
                Default = new FreeRedisHelper(connectionStrings.Select(v => ConnectionStringBuilder.Parse(v)).ToArray());
            }

            //配置序列化和反序列化
            Default.Serialize   = obj => JsonConvert.SerializeObject(obj);
            Default.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
        }
Exemplo n.º 4
0
        protected override void Load(ContainerBuilder builder)
        {
            RedisOptions options = AppConfig.GetSection <RedisOptions>("Redis");

            builder.Register(
                c =>
            {
                RedisClient redis = null;
                switch (options.Type)
                {
                case RedisType.MasterSlave:
                    var slave = new List <ConnectionStringBuilder>();
                    slave.AddRange(options.Slave.Select(p => ConnectionStringBuilder.Parse(p)).ToList());
                    redis = new RedisClient(options.Master, slave.ToArray());
                    break;

                case RedisType.Sentinel:
                    redis = new RedisClient(
                        options.Master,
                        options.Sentinel.ToArray(),
                        true
                        );
                    break;

                case RedisType.Cluster:
                    var clusters = new List <ConnectionStringBuilder>();
                    clusters.Add(options.Master);
                    clusters.AddRange(options.Slave.Select(p => ConnectionStringBuilder.Parse(p)).ToList());
                    redis = new RedisClient(clusters.ToArray());
                    break;

                default:
                    System.Console.WriteLine("Redis:配置错误!!!!");
                    break;
                }
                // var Redis = new CSRedisCache(new CSRedisClient("mymaster,password=584ab235-0e5e-11e9-a088-0c9d92bf536f,poolsize=50,connectTimeout=200,ssl=false", new string[] { "192.168.2.10:30600" }, true));
                return(redis);
            })
            .As <RedisClient>()
            .InstancePerLifetimeScope().PropertiesAutowired();
            builder.RegisterType <Cache>().As <ICache>().InstancePerLifetimeScope().PropertiesAutowired();
            builder.RegisterType <LockUtil>().As <ILockUtil>().SingleInstance().PropertiesAutowired();
        }
Exemplo n.º 5
0
        public RedisProvider(IConfiguration configuration)
        {
            var redisConn    = configuration["Connection:Redis"];
            var redisCluster = configuration.GetSection("Connection:RedisCluster").Get <List <string> >();

            if (redisCluster != null && redisCluster.Count > 0)
            {
                _redisClient = new RedisClient(
                    redisCluster.Select(o => ConnectionStringBuilder.Parse(o)).ToArray()
                    );
            }
            else if (string.IsNullOrEmpty(redisConn))
            {
                _redisClient = new RedisClient(redisConn);
            }
            else
            {
                throw new Exception("未配置Redis连接字符串,请在appsettings.json文件以下位置配置连接字符串(Connection:Redis或者集群主库集合Connection:RedisCluster)");
            }
        }