Пример #1
0
        /// <summary>
        /// Adds the default redis cache.
        /// </summary>
        /// <returns>The default redis cache.</returns>
        /// <param name="services">Services.</param>
        /// <param name="dbAction">Options Action.</param>
        public static IServiceCollection AddDefaultRedisCache(
            this IServiceCollection services,
            Action <RedisDBOptions> dbAction)
        {
            var options = new RedisOptions();

            return(services.AddDefaultRedisCache(dbAction, x =>
            {
                x.CachingProviderType = options.CachingProviderType;
                x.MaxRdSecond = options.MaxRdSecond;
                x.Order = options.Order;
            }));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="dbProvider">DB Provider.</param>
        /// <param name="serializer">Serializer.</param>
        public DefaultRedisCachingProvider(
            IRedisDatabaseProvider dbProvider,
            IEasyCachingSerializer serializer,
            RedisOptions options)
        {
            ArgumentCheck.NotNull(dbProvider, nameof(dbProvider));
            ArgumentCheck.NotNull(serializer, nameof(serializer));

            this._dbProvider = dbProvider;
            this._serializer = serializer;
            this._options    = options;
            this._cache      = _dbProvider.GetDatabase();
            this._servers    = _dbProvider.GetServerList();
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="dbProvider">DB Provider.</param>
        /// <param name="serializer">Serializer.</param>
        public DefaultRedisCachingProvider(
            IRedisDatabaseProvider dbProvider,
            IEasyCachingSerializer serializer,
            RedisOptions options,
            ILoggerFactory loggerFactory = null)
        {
            ArgumentCheck.NotNull(dbProvider, nameof(dbProvider));
            ArgumentCheck.NotNull(serializer, nameof(serializer));

            this._dbProvider = dbProvider;
            this._serializer = serializer;
            this._options    = options;
            this._logger     = loggerFactory?.CreateLogger <DefaultRedisCachingProvider>();
            this._cache      = _dbProvider.GetDatabase();
            this._servers    = _dbProvider.GetServerList();
        }
Пример #4
0
        /// <summary>
        /// Uses the redis.
        /// </summary>
        /// <returns>The redis.</returns>
        /// <param name="options">Options.</param>
        /// <param name="configuration">Configuration.</param>
        /// <param name="name">Name.</param>
        /// <param name="sectionName">SectionName.</param>
        public static EasyCachingOptions UseRedis(this EasyCachingOptions options, IConfiguration configuration, string name = EasyCachingConstValue.DefaultRedisName, string sectionName = EasyCachingConstValue.RedisSection)
        {
            var dbConfig     = configuration.GetSection(sectionName);
            var redisOptions = new RedisOptions();

            dbConfig.Bind(redisOptions);

            void configure(RedisOptions x)
            {
                x.EnableLogging = redisOptions.EnableLogging;
                x.MaxRdSecond   = redisOptions.MaxRdSecond;
                x.DBConfig      = redisOptions.DBConfig;
            }

            options.RegisterExtension(new RedisOptionsExtension(name, configure));
            return(options);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="dbProviders">Db providers.</param>
        /// <param name="serializers">Serializers.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultRedisCachingProvider(
            string name,
            IEnumerable <IRedisDatabaseProvider> dbProviders,
            IEnumerable <IEasyCachingSerializer> serializers,
            RedisOptions options,
            ILoggerFactory loggerFactory = null)
        {
            ArgumentCheck.NotNullAndCountGTZero(dbProviders, nameof(dbProviders));
            ArgumentCheck.NotNullAndCountGTZero(serializers, nameof(serializers));

            this._name       = name;
            this._dbProvider = dbProviders.Single(x => x.DBProviderName.Equals(name));
            this._options    = options;

            if (options.EnableLogging)
            {
                this.Logger = loggerFactory.CreateLogger <DefaultRedisCachingProvider>();
            }

            this._cache      = _dbProvider.GetDatabase();
            this._servers    = _dbProvider.GetServerList();
            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;

            _info = new ProviderInfo
            {
                CacheStats     = _cacheStats,
                EnableLogging  = options.EnableLogging,
                LockMs         = options.LockMs,
                MaxRdSecond    = options.MaxRdSecond,
                ProviderName   = ProviderName,
                ProviderType   = ProviderType,
                SerializerName = options.SerializerName,
                SleepMs        = options.SleepMs,
                Serializer     = _serializer,
                CacheNulls     = options.CacheNulls
            };
        }
Пример #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="dbProvider">Db provider.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultRedisCachingProvider(
            IRedisDatabaseProvider dbProvider,
            IEasyCachingSerializer serializer,
            IOptionsMonitor <RedisOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            ArgumentCheck.NotNull(dbProvider, nameof(dbProvider));
            ArgumentCheck.NotNull(serializer, nameof(serializer));

            this._dbProvider = dbProvider;
            this._serializer = serializer;
            this._options    = options.CurrentValue;
            this._logger     = loggerFactory?.CreateLogger <DefaultRedisCachingProvider>();
            this._cache      = _dbProvider.GetDatabase();
            this._servers    = _dbProvider.GetServerList();
            this._cacheStats = new CacheStats();
            this._name       = EasyCachingConstValue.DefaultRedisName;
        }
Пример #7
0
        /// <summary>
        /// Uses the redis.
        /// </summary>
        /// <returns>The redis.</returns>
        /// <param name="options">Options.</param>
        /// <param name="configuration">Configuration.</param>
        /// <param name="name">Name.</param>
        public static EasyCachingOptions UseRedis(this EasyCachingOptions options, IConfiguration configuration, string name = "")
        {
            var dbConfig     = configuration.GetSection(EasyCachingConstValue.InMemorySection);
            var redisOptions = new RedisOptions();

            dbConfig.Bind(redisOptions);

            void configure(RedisOptions x)
            {
                x.CachingProviderType = redisOptions.CachingProviderType;
                x.EnableLogging       = redisOptions.EnableLogging;
                x.MaxRdSecond         = redisOptions.MaxRdSecond;
                x.Order    = redisOptions.Order;
                x.DBConfig = redisOptions.DBConfig;
            }

            options.RegisterExtension(new RedisOptionsExtension(name, configure));
            return(options);
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.Redis.DefaultRedisCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="dbProviders">Db providers.</param>
        /// <param name="serializer">Serializer.</param>
        /// <param name="options">Options.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public DefaultRedisCachingProvider(
            string name,
            IEnumerable <IRedisDatabaseProvider> dbProviders,
            IEasyCachingSerializer serializer,
            IOptionsMonitor <RedisOptions> options,
            ILoggerFactory loggerFactory = null)
        {
            ArgumentCheck.NotNullAndCountGTZero(dbProviders, nameof(dbProviders));
            ArgumentCheck.NotNull(serializer, nameof(serializer));

            this._dbProvider = dbProviders.FirstOrDefault(x => x.DBProviderName.Equals(name));
            this._serializer = serializer;
            this._options    = options.CurrentValue;
            this._logger     = loggerFactory?.CreateLogger <DefaultRedisCachingProvider>();
            this._cache      = _dbProvider.GetDatabase();
            this._servers    = _dbProvider.GetServerList();
            this._cacheStats = new CacheStats();
            this._name       = name;
        }
        /// <summary>
        /// Adds the default redis cache.
        /// </summary>
        /// <returns>The default redis cache.</returns>
        /// <param name="services">Services.</param>
        /// <param name="dbAction">Db action.</param>
        /// <param name="providerAction">Provider action.</param>
        public static IServiceCollection AddDefaultRedisCache(
            this IServiceCollection services,
            Action <RedisDBOptions> dbAction,
            Action <RedisOptions> providerAction)
        {
            ArgumentCheck.NotNull(services, nameof(services));
            ArgumentCheck.NotNull(dbAction, nameof(dbAction));
            ArgumentCheck.NotNull(providerAction, nameof(providerAction));

            services.AddOptions();
            services.Configure(dbAction);

            var providerOption = new RedisOptions();

            providerAction(providerOption);
            services.AddSingleton(providerOption);

            services.TryAddSingleton <IEasyCachingSerializer, DefaultBinaryFormatterSerializer>();
            services.TryAddSingleton <IRedisDatabaseProvider, RedisDatabaseProvider>();
            services.TryAddSingleton <IEasyCachingProvider, DefaultRedisCachingProvider>();

            return(services);
        }
 public RedisDatabaseProvider(string name, RedisOptions options)
 {
     _options = options.DBConfig;
     _connectionMultiplexer = new Lazy <ConnectionMultiplexer>(CreateConnectionMultiplexer);
     _name = name;
 }