示例#1
0
        public void CreateClient_Should_Try_To_Connect_To_Host()
        {
            var ISettingsService = new Mock <ISettingsService>();

            ISettingsService
            .Setup(x => x.Get <CacheServiceSettings>())
            .Returns(new CacheServiceSettings
            {
                Redis = new CacheServiceSettings.RedisSettings
                {
                    Enabled          = true,
                    ConnectionString = "localhost"
                }
            });


            var service = new RedisClientFactory
            {
                ISettingsService = ISettingsService.Object
            };

            Assert.Throws <RedisException>(() =>
            {
                using (var client = service.GetClient())
                {
                }
            });
        }
示例#2
0
        private void RegisterDependencies(IServiceCollection services)
        {
            _logger.Information("Starting registering dependencies...");

            services.AddTransient <IEncryptService, EncryptService>();
            services.AddTransient <IKeyGenerator, RsaKeyGenerator>();
            services.AddTransient <IRandomStringGenerator, RandomBase64StringGenerator>();
            services.AddTransient <IPasswordEncryptor, PasswordEncryptor>();

            var appConfig = Configuration.GetSection("service").Get <ApplicationConfig>();

            services.AddSingleton(appConfig.Consul ?? new ConsulConfig());
            services.AddSingleton(appConfig.Fabio ?? new FabioConfig());

            var connectionMultiplexer = ConnectionMultiplexer.Connect(appConfig.ConnectionStrings.Redis.ConnectionString);
            var redisClientFactory    = new RedisClientFactory(connectionMultiplexer, appConfig.ConnectionStrings.Redis.DefaultExpiration);

            services.AddSingleton <IRedisClientFactory, RedisClientFactory>(provider => redisClientFactory);
            services.AddMediatR(typeof(Startup).Assembly);

            services.AddSingleton <IConsulClient, ConsulClient>(p => new ConsulClient(consulConfig =>
            {
                var address          = appConfig.Consul.Url;
                consulConfig.Address = new Uri(address);
            }));

            _logger.Information("Finished registering dependencies.");
        }
        private static IRedisClient <TModel> CreateRedisClient <TModel>()
            where TModel : IModel, new()
        {
            var factory = new RedisClientFactory <TModel>(AppConfig.Main.RedisConnectionString,
                                                          AppConfig.Main.MainDbId);

            return(factory.CreateRedisClient());
        }
示例#4
0
 public static void Setup(string @namespace = Constants.DefaultNamespace, Granularity[] granularities = null)
 {
     if (options == null && lazyConnector == null)
     {
         options       = new RateContext(@namespace, granularities ?? new Granularity[] { Granularity.Second, Granularity.Minute, Granularity.Hour, Granularity.Day });
         lazyConnector = new Lazy <RedisClient>(() => { return(RedisClientFactory.Connect("cache")); });
     }
 }
示例#5
0
        public void Dispose()
        {
            bool isMaster = IsMaster();

            _factory.Free(isMaster, _client);
            IsConnected = false;
            _client     = null;
            _factory    = null;
        }
示例#6
0
 /// <summary>
 /// 赋值Redis客户端工厂数据操作
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="reader"></param>
 /// <param name="prefix"></param>
 public static void SetRedisClientFactoryStoreSelectFields(RedisClientFactory factory, DbDataReader reader, string prefix)
 {
     factory.ID            = (Guid)reader[string.Format("{0}id", prefix)];
     factory.Name          = reader[string.Format("{0}name", prefix)].ToString();
     factory.Type          = reader[string.Format("{0}type", prefix)].ToString();
     factory.Configuration = reader[string.Format("{0}configuration", prefix)].ToString();
     factory.CreateTime    = (DateTime)reader[string.Format("{0}createtime", prefix)];
     factory.ModifyTime    = (DateTime)reader[string.Format("{0}modifytime", prefix)];
 }
        private static IRedisClient <TModel> CreateRedisClient <TModel>()
            where TModel : IModel, new()
        {
            var redisConnectionString = TestsContext.RedisConnectionString;
            var mainDbId = TestsContext.MainDbId;

            var factory = new RedisClientFactory <TModel>(redisConnectionString, mainDbId);

            return(factory.CreateRedisClient());
        }
        public void Dispose()
        {
            GC.SuppressFinalize(this);
            string END_POINT_KEY      = "REDIS_EVENT_SOURCE_PRODUCER_ENDPOINT";
            string PASSWORD_KEY       = "REDIS_EVENT_SOURCE_PRODUCER_PASS";
            string key                = $"{PARTITION}:{SHARD}";
            var    redisClientFactory = new RedisClientFactory(
                _fakeLogger,
                $"Test {DateTime.Now: yyyy-MM-dd HH_mm_ss}",
                RedisUsageIntent.Admin,
                null,
                END_POINT_KEY, PASSWORD_KEY);

            IDatabaseAsync db = redisClientFactory.GetDbAsync().Result;

            db.KeyDeleteAsync(key, CommandFlags.DemandMaster).Wait();
        }
示例#9
0
        public RedisClientFactory QueryByNameSync(string name)
        {
            RedisClientFactory factory = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _redisConnectionFactory.CreateReadForRedisClientFactory(), (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"SELECT {0} FROM [RedisClientFactory] WHERE name=@name;", StoreHelper.GetRedisClientFactoryStoreSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = name
                    };
                    command.Parameters.Add(parameter);
                    command.Prepare();
                    SqlDataReader reader = null;

                    using (reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            factory = new RedisClientFactory();
                            StoreHelper.SetRedisClientFactoryStoreSelectFields(factory, reader, string.Empty);
                        }
                        reader.Close();
                    }
                }
            });
            return(factory);
        }
示例#10
0
        public Rediser(string conString, SerializerType serializerType)
        {
            IConnectionMultiplexer opt = ConnectionFactory.ConcretConnection(conString);

            this._client = RedisClientFactory.ConcretRedisClient(opt, serializerType);
        }
示例#11
0
 internal void SetClient(RedisClientFactory factory, IClient client)
 {
     _factory    = factory;
     _client     = client as RedisClient;
     IsConnected = client.IsConnected;
 }
示例#12
0
 private IDatabase getRedisDb()
 {
     return(RedisClientFactory.getInstance());
 }
示例#13
0
 public ConnectionManage()
 {
     _kvclient = RedisClientFactory.CreateRepository(AppConfigurationConsts.RedisConnectionString());
 }
示例#14
0
        protected override IEventStorage GetStorage(string name)
        {
            var redis = RedisClientFactory.GetRedisClient(name);

            return(new RedisEventStorage(redis, this.Encoding));
        }