示例#1
0
        public static IServiceCollection AddStackExchangeRedis(this IServiceCollection services, IConfiguration config)
        {
            var redisOption = new RedisCacheOption(config);

            services.AddStackExchangeRedis(redisOption);
            return(services);
        }
 public StackExchangeRedisManager(ConnectionMultiplexer connection, RedisCacheOption option)
 {
     _connection     = connection;
     _option         = option;
     CustomPrefixKey = option.DefaultCustomKey;
     _database       = _connection.GetDatabase();
 }
示例#3
0
        public static IServiceCollection AddCsRedis(this IServiceCollection services, IConfiguration config)
        {
            var option = new RedisCacheOption(config);

            services.AddCsRedis(option);
            return(services);
        }
示例#4
0
 public CsRedisManager(RedisCacheOption option,
                       ConnectionMultiplexer connection,
                       IMemoryCache memorycache, ILogger <CsRedisManager> logger)
 {
     _connection  = connection;
     _option      = option;
     _memorycache = memorycache;
     _logger      = logger;
 }
示例#5
0
 public StackExchangeRedisManager(ConnectionMultiplexer connection,
                                  ISerializer serializer,
                                  RedisCacheOption option,
                                  IMemoryCache memorycache,
                                  ILogger <StackExchangeRedisManager> logger)
 {
     Serializer      = serializer ?? throw new ArgumentNullException(nameof(serializer));
     _connection     = connection;
     _option         = option;
     CustomPrefixKey = option.DefaultCustomKey;
     if (!string.IsNullOrWhiteSpace(CustomPrefixKey))
     {
         _database = _connection.GetDatabase(_option.Database).WithKeyPrefix(CustomPrefixKey);
     }
     else
     {
         _database = _connection.GetDatabase(_option.Database);
     }
     _memorycache = memorycache;
     _logger      = logger;
 }
示例#6
0
        public static IServiceCollection AddStackExchangeRedis(this IServiceCollection services, RedisCacheOption redisOption)
        {
            services.AddSingleton(redisOption);
            var configurationOptions = new ConfigurationOptions
            {
                ConnectTimeout     = redisOption.ConnectionTimeout,
                Password           = redisOption.Password,
                Ssl                = redisOption.IsSsl,
                SslHost            = redisOption.SslHost,
                AbortOnConnectFail = false,
            };

            foreach (var endpoint in redisOption.Endpoints)
            {
                configurationOptions.EndPoints.Add(endpoint.Host, endpoint.Port);
            }
            services.Add(ServiceDescriptor.Singleton(ConnectionMultiplexer.Connect(configurationOptions)));
            services.AddSingleton <IRedisManager, StackExchangeRedisManager>();
            return(services);
        }
示例#7
0
        public static IServiceCollection AddCsRedis(this IServiceCollection services, RedisCacheOption option)
        {
            services.AddSingleton(option);
            List <string> csredisConns = new List <string>();
            string        password     = option.Password;
            int           defaultDb    = option.Database;
            string        ssl          = option.SslHost;
            string        keyPrefix    = option.DefaultCustomKey;
            int           writeBuffer  = 10240;
            int           poolsize     = 10;

            foreach (var e in option.Endpoints)
            {
                string server = e.Host;
                int    port   = e.Port;
                if (string.IsNullOrWhiteSpace(server) || port <= 0)
                {
                    continue;
                }
                csredisConns.Add($"{server}:{port},password={password},defaultDatabase={defaultDb},poolsize={poolsize},ssl={ssl},writeBuffer={writeBuffer},prefix={keyPrefix}");
            }

            CSRedis.CSRedisClient csredis;

            try
            {
                csredis = new CSRedis.CSRedisClient(null, csredisConns.ToArray());
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Check the configuration for redis;{ex}");
            }

            RedisHelper.Initialization(csredis);
            services.AddScoped <IRedisManager, CsRedisManager>();
            return(services);
        }
示例#8
0
        public static IServiceCollection AddRedisManager(this IServiceCollection services, RedisCacheOption redisCacheOption)
        {
            var redisCacheComponent = redisCacheOption?.RedisComponent ?? RedisCacheComponentEnum.NullRedis;

            switch (redisCacheComponent)
            {
            case RedisCacheComponentEnum.CSRedisCore:
                services.AddCsRedis(redisCacheOption);
                break;

            case RedisCacheComponentEnum.StackExchangeRedis:
                services.AddStackExchangeRedis(redisCacheOption);
                break;

            case RedisCacheComponentEnum.NullRedis:
                services.AddSingleton <IRedisManager, NullCache>();
                break;

            default:
                services.AddSingleton(redisCacheOption);
                services.AddSingleton <IRedisManager, NullCache>();
                break;
            }
            return(services);
        }
示例#9
0
        public static IServiceCollection AddCsRedis(this IServiceCollection services, RedisCacheOption option)
        {
            services.AddSingleton <ISerializer, NewtonsoftSerializer>();
            services.AddSingleton(option);
            List <string> csredisConns = new List <string>();
            string        password     = option.Password;
            int           defaultDb    = option.Database;
            string        ssl          = option.SslHost;
            string        keyPrefix    = option.DefaultCustomKey;
            int           writeBuffer  = 10240;
            int           poolsize     = option.PoolSize == 0 ? 10 : option.PoolSize;
            int           timeout      = option.ConnectionTimeout;

            foreach (var e in option.Endpoints)
            {
                string server = e.Host;
                int    port   = e.Port;
                if (string.IsNullOrWhiteSpace(server) || port <= 0)
                {
                    continue;
                }
                csredisConns.Add($"{server}:{port},password={password},defaultDatabase={defaultDb},poolsize={poolsize},ssl={ssl},writeBuffer={writeBuffer},prefix={keyPrefix},preheat={option.Preheat},idleTimeout={timeout},testcluster={option.Cluster}");
            }

            CSRedis.CSRedisClient csredis;

            try
            {
                csredis = new CSRedis.CSRedisClient(null, csredisConns.ToArray());
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"请检查是否为非密码模式,Password必须为空字符串;请检查Database是否为0,只能在非集群模式下才可配置Database大于0;{ex}");
            }

            RedisHelper.Initialization(csredis);
            services.AddSingleton <IRedisManager, CsRedisManager>();

            /*注入stackexchange驱动,防止CSRedis驱动下获取IDatabase异常 */
            #region   注入stackexchange驱动
            var configurationOptions = new ConfigurationOptions
            {
                KeepAlive          = 180,
                ConnectTimeout     = option.ConnectionTimeout,
                Password           = option.Password,
                Ssl                = option.IsSsl,
                SslHost            = option.SslHost,
                AbortOnConnectFail = false,
                AsyncTimeout       = option.ConnectionTimeout,
                DefaultDatabase    = option.Database,
            };

            foreach (var endpoint in option.Endpoints)
            {
                configurationOptions.EndPoints.Add(endpoint.Host, endpoint.Port);
            }

            var connect = ConnectionMultiplexer.Connect(configurationOptions);
            //注册如下事件
            connect.ConnectionFailed     += MuxerConnectionFailed;
            connect.ConnectionRestored   += MuxerConnectionRestored;
            connect.ErrorMessage         += MuxerErrorMessage;
            connect.ConfigurationChanged += MuxerConfigurationChanged;
            connect.HashSlotMoved        += MuxerHashSlotMoved;
            connect.InternalError        += MuxerInternalError;
            services.AddSingleton(connect);
            //services.Add(ServiceDescriptor.Singleton(connect));
            #endregion

            return(services);
        }
示例#10
0
 public CsRedisManager(RedisCacheOption option)
 {
     _option = option;
 }
示例#11
0
 public CsRedisManager(RedisCacheOption option, ConnectionMultiplexer connection)
 {
     _connection = connection;
     _option     = option;
 }