Esempio n. 1
0
        /// <summary>
        /// Adds the services.
        /// </summary>
        /// <param name="services">Services.</param>
        public void AddServices(IServiceCollection services)
        {
            services.AddOptions();

            services.TryAddSingleton <IEasyCachingSerializer, DefaultBinaryFormatterSerializer>();

            services.Configure(_name, _configure);

            services.TryAddSingleton <IEasyCachingProviderFactory, DefaultEasyCachingProviderFactory>();

            services.AddSingleton <EasyCachingCSRedisClient>(x =>
            {
                var optionsMon = x.GetRequiredService <IOptionsMonitor <RedisOptions> >();
                var options    = optionsMon.Get(_name);

                var conns     = options.DBConfig.ConnectionStrings;
                var rule      = options.DBConfig.NodeRule;
                var sentinels = options.DBConfig.Sentinels;
                var readOnly  = options.DBConfig.ReadOnly;

                if (sentinels != null && sentinels.Any())
                {
                    var redisClient = new EasyCachingCSRedisClient(_name, conns[0], sentinels.ToArray(), readOnly);
                    return(redisClient);
                }

                if (conns.Count == 1)
                {
                    var redisClient = new EasyCachingCSRedisClient(_name, conns[0]);
                    return(redisClient);
                }
                else
                {
                    var redisClient = new EasyCachingCSRedisClient(_name, rule, conns.ToArray());
                    return(redisClient);
                }
            });

            Func <IServiceProvider, DefaultCSRedisCachingProvider> createFactory = x =>
            {
                var clients     = x.GetServices <EasyCachingCSRedisClient>();
                var serializers = x.GetServices <IEasyCachingSerializer>();
                var optionsMon  = x.GetRequiredService <IOptionsMonitor <RedisOptions> >();
                var options     = optionsMon.Get(_name);
                var dlf         = x.GetService <CSRedisLockFactory>();
                var factory     = x.GetService <ILoggerFactory>();
                return(new DefaultCSRedisCachingProvider(_name, clients, serializers, options, dlf, factory));
            };

            services.AddSingleton <IEasyCachingProvider, DefaultCSRedisCachingProvider>(createFactory);
            services.AddSingleton <IRedisCachingProvider, DefaultCSRedisCachingProvider>(createFactory);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.DefaultCSRedisCachingProvider"/> class.
 /// </summary>
 /// <param name="name">Name.</param>
 /// <param name="clients">Clients.</param>
 /// <param name="serializer">Serializer.</param>
 /// <param name="options">Options.</param>
 /// <param name="loggerFactory">Logger factory.</param>
 public DefaultCSRedisCachingProvider(
     string name,
     IEnumerable <EasyCachingCSRedisClient> clients,
     IEasyCachingSerializer serializer,
     IOptionsMonitor <RedisOptions> options,
     ILoggerFactory loggerFactory = null)
 {
     this._name       = name;
     this._serializer = serializer;
     this._options    = options.CurrentValue;
     this._logger     = loggerFactory?.CreateLogger <DefaultCSRedisCachingProvider>();
     this._cache      = clients.FirstOrDefault(x => x.Name.Equals(_name));
     this._cacheStats = new CacheStats();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.DefaultCSRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="clients">Clients.</param>
        /// <param name="serializers">Serializers.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultCSRedisCachingProvider(
            string name,
            IEnumerable <EasyCachingCSRedisClient> clients,
            IEnumerable <IEasyCachingSerializer> serializers,
            RedisOptions options,
            ILoggerFactory loggerFactory = null)
        {
            this._name       = name;
            this._serializer = serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName));
            this._options    = options;
            this._logger     = loggerFactory?.CreateLogger <DefaultCSRedisCachingProvider>();
            this._cache      = clients.Single(x => x.Name.Equals(_name));
            this._cacheStats = new CacheStats();

            this.ProviderName          = this._name;
            this.ProviderType          = CachingProviderType.Redis;
            this.ProviderStats         = this._cacheStats;
            this.ProviderMaxRdSecond   = _options.MaxRdSecond;
            this.IsDistributedProvider = true;
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.DefaultCSRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="clients">Clients.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultCSRedisCachingProvider(
            string name,
            IEnumerable <EasyCachingCSRedisClient> clients,
            IEasyCachingSerializer serializer,
            IOptionsMonitor <RedisOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            this._name       = name;
            this._serializer = serializer;
            this._options    = options.CurrentValue;
            this._logger     = loggerFactory?.CreateLogger <DefaultCSRedisCachingProvider>();
            this._cache      = clients.FirstOrDefault(x => x.Name.Equals(_name));
            this._cacheStats = new CacheStats();

            this.ProviderName          = this._name;
            this.ProviderStats         = this._cacheStats;
            this.ProviderType          = _options.CachingProviderType;
            this.ProviderOrder         = _options.Order;
            this.ProviderMaxRdSecond   = _options.MaxRdSecond;
            this.IsDistributedProvider = true;
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.CSRedis.DefaultCSRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="clients">Clients.</param>
        /// <param name="serializers">Serializers.</param>
        /// <param name="options">Options.</param>
        /// <param name="factory">Distributed lock factory</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultCSRedisCachingProvider(
            string name,
            IEnumerable <EasyCachingCSRedisClient> clients,
            IEnumerable <IEasyCachingSerializer> serializers,
            RedisOptions options,
            CSRedisLockFactory factory   = null,
            ILoggerFactory loggerFactory = null)
            : base(factory, options)
        {
            this._name       = name;
            this._options    = options;
            this._logger     = loggerFactory?.CreateLogger <DefaultCSRedisCachingProvider>();
            this._cache      = clients.Single(x => x.Name.Equals(_name));
            this._cacheStats = new CacheStats();

            this._serializer = !string.IsNullOrWhiteSpace(options.SerializerName)
                ? serializers.Single(x => x.Name.Equals(options.SerializerName))
                : serializers.FirstOrDefault(x => x.Name.Equals(_name)) ?? serializers.Single(x => x.Name.Equals(EasyCachingConstValue.DefaultSerializerName));

            this.ProviderName          = this._name;
            this.ProviderType          = CachingProviderType.Redis;
            this.ProviderStats         = this._cacheStats;
            this.ProviderMaxRdSecond   = _options.MaxRdSecond;
            this.IsDistributedProvider = true;

            _info = new ProviderInfo
            {
                CacheStats            = _cacheStats,
                EnableLogging         = options.EnableLogging,
                IsDistributedProvider = IsDistributedProvider,
                LockMs         = options.LockMs,
                MaxRdSecond    = options.MaxRdSecond,
                ProviderName   = ProviderName,
                ProviderType   = ProviderType,
                SerializerName = options.SerializerName,
                SleepMs        = options.SleepMs,
                Serializer     = _serializer,
                CacheNulls     = options.CacheNulls,
            };
        }