Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="optionsAccs">Options accs.</param>
        /// <param name="factory">Providers factory</param>
        /// <param name="bus">Bus.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public HybridCachingProvider(
            string name
            , HybridCachingOptions optionsAccs
            , IEasyCachingProviderFactory factory
            , IEasyCachingBus bus          = null
            , ILoggerFactory loggerFactory = null
            )
        {
            ArgumentCheck.NotNull(factory, nameof(factory));

            this._name    = name;
            this._options = optionsAccs;

            ArgumentCheck.NotNullOrWhiteSpace(_options.TopicName, nameof(_options.TopicName));

            if (optionsAccs.EnableLogging)
            {
                this._logger = loggerFactory.CreateLogger <HybridCachingProvider>();
            }

            // Here use the order to distinguish traditional provider
            this._localCache = factory.GetCachingProvider(_options.LocalCacheProviderName);

            // Here use the order to distinguish traditional provider
            this._distributedCache = factory.GetCachingProvider(_options.DistributedCacheProviderName);

            this._bus = bus ?? NullEasyCachingBus.Instance;
            this._bus.Subscribe(_options.TopicName, OnMessage);

            this._cacheId = Guid.NewGuid().ToString("N");
        }
Exemplo n.º 2
0
        public HybridCachingTest()
        {
            _namespace = "hybrid";

            var options = new HybridCachingOptions
            {
                EnableLogging                 = false,
                TopicName                     = "test_topic",
                LocalCacheProviderName        = "m1",
                DistributedCacheProviderName  = "myredis",
                BusRetryCount                 = 1,
                DefaultExpirationForTtlFailed = 60
            };

            IServiceCollection services = new ServiceCollection();

            services.AddEasyCaching(option =>
            {
                option.UseInMemory("m1");
                option.UseInMemory("m2");

                option.UseRedis(config =>
                {
                    config.DBConfig.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
                    config.DBConfig.Database = 5;
                }, "myredis");

                option.UseHybrid(config =>
                {
                    config.EnableLogging                = false;
                    config.TopicName                    = "test_topic";
                    config.LocalCacheProviderName       = "m1";
                    config.DistributedCacheProviderName = "myredis";
                });

                option.WithRedisBus(config =>
                {
                    config.Endpoints.Add(new Core.Configurations.ServerEndPoint("127.0.0.1", 6379));
                    config.Database = 6;
                });
            });

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            factory = serviceProvider.GetService <IEasyCachingProviderFactory>();

            var bus = serviceProvider.GetService <IEasyCachingBus>();

            hybridCaching_1 = serviceProvider.GetService <IHybridCachingProvider>();

            fakeBus         = A.Fake <IEasyCachingBus>();
            fakeFactory     = A.Fake <IEasyCachingProviderFactory>();
            fakeDisProvider = A.Fake <FakeDistributedCachingProvider>();
            var myOptions = Options.Create(options);

            FakeCreatProvider();
            fakeHybrid = new HybridCachingProvider("h1", myOptions.Value, fakeFactory, fakeBus);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
        /// </summary>
        /// <param name="optionsAccs">Options accs.</param>
        /// <param name="IEasyCachingProviderFactory">Providers factory</param>
        /// <param name="bus">Bus.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public HybridCachingProvider(
            IOptions <HybridCachingOptions> optionsAccs
            , IEasyCachingProviderFactory factory
            , IEasyCachingBus bus          = null
            , ILoggerFactory loggerFactory = null
            )
        {
            ArgumentCheck.NotNull(factory, nameof(factory));

            this._options = optionsAccs.Value;

            ArgumentCheck.NotNullOrWhiteSpace(_options.TopicName, nameof(_options.TopicName));

            this._logger = loggerFactory?.CreateLogger <HybridCachingProvider>();

            //Here use the order to distinguish traditional provider
            var local = factory.GetCachingProvider(_options.LocalCacheProviderName);

            if (local.IsDistributedCache)
            {
                throw new NotFoundCachingProviderException("Can not found any local caching providers.");
            }
            else
            {
                this._localCache = local;
            }

            //Here use the order to distinguish traditional provider
            var distributed = factory.GetCachingProvider(_options.DistributedCacheProviderName);

            if (!distributed.IsDistributedCache)
            {
                throw new NotFoundCachingProviderException("Can not found any distributed caching providers.");
            }
            else
            {
                this._distributedCache = distributed;
            }

            this._bus = bus ?? NullEasyCachingBus.Instance;
            this._bus.Subscribe(_options.TopicName, OnMessage);

            this._cacheId = Guid.NewGuid().ToString("N");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
        /// </summary>
        /// <param name="optionsAccs">Options accs.</param>
        /// <param name="providers">Providers.</param>
        /// <param name="bus">Bus.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public HybridCachingProvider(
            IOptions <HybridCachingOptions> optionsAccs
            , IEnumerable <IEasyCachingProvider> providers
            , IEasyCachingBus bus          = null
            , ILoggerFactory loggerFactory = null
            )
        {
            ArgumentCheck.NotNullAndCountGTZero(providers, nameof(providers));

            this._options = optionsAccs.Value;

            ArgumentCheck.NotNullOrWhiteSpace(_options.TopicName, nameof(_options.TopicName));

            this._logger = loggerFactory?.CreateLogger <HybridCachingProvider>();

            //Here use the order to distinguish traditional provider
            var local = providers.OrderBy(x => x.Order).FirstOrDefault(x => !x.IsDistributedCache);

            if (local == null)
            {
                throw new NotFoundCachingProviderException("Can not found any local caching providers.");
            }
            else
            {
                this._localCache = local;
            }

            //Here use the order to distinguish traditional provider
            var distributed = providers.OrderBy(x => x.Order).FirstOrDefault(x => x.IsDistributedCache);

            if (distributed == null)
            {
                throw new NotFoundCachingProviderException("Can not found any distributed caching providers.");
            }
            else
            {
                this._distributedCache = distributed;
            }

            this._bus = bus ?? NullEasyCachingBus.Instance;
            this._bus.Subscribe(_options.TopicName, OnMessage);

            this._cacheId = Guid.NewGuid().ToString("N");
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="optionsAccs">Options accs.</param>
        /// <param name="factory">Providers factory</param>
        /// <param name="bus">Bus.</param>
        /// <param name="loggerFactory">Logger factory.</param>
        public HybridCachingProvider(
            string name
            , HybridCachingOptions optionsAccs
            , IEasyCachingProviderFactory factory
            , IEasyCachingBus bus          = null
            , ILoggerFactory loggerFactory = null
            )
        {
            ArgumentCheck.NotNull(factory, nameof(factory));

            this._name    = name;
            this._options = optionsAccs;

            ArgumentCheck.NotNullOrWhiteSpace(_options.TopicName, nameof(_options.TopicName));

            this._logger = loggerFactory?.CreateLogger <HybridCachingProvider>();

            // Here use the order to distinguish traditional provider
            var local = factory.GetCachingProvider(_options.LocalCacheProviderName);

            if (local.IsDistributedCache)
            {
                throw new NotFoundCachingProviderException("Can not found any local caching providers.");
            }
            else
            {
                this._localCache = local;
            }

            // Here use the order to distinguish traditional provider
            var distributed = factory.GetCachingProvider(_options.DistributedCacheProviderName);

            if (!distributed.IsDistributedCache)
            {
                throw new NotFoundCachingProviderException("Can not found any distributed caching providers.");
            }
            else
            {
                this._distributedCache = distributed;
            }

            this._bus = bus ?? NullEasyCachingBus.Instance;
            this._bus.Subscribe(_options.TopicName, OnMessage);

            this._cacheId = Guid.NewGuid().ToString("N");


            // policy

            retryAsyncPolicy = Policy.Handle <Exception>()
                               .WaitAndRetryAsync(this._options.BusRetryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt - 1)));

            retryPolicy = Policy.Handle <Exception>()
                          .WaitAndRetry(this._options.BusRetryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt - 1)));

            fallbackPolicy = Policy.Handle <Exception>().Fallback(() => { });

            fallbackAsyncPolicy = Policy.Handle <Exception>().FallbackAsync(ct =>
            {
                return(Task.CompletedTask);
            });

            _busSyncWrap  = Policy.Wrap(fallbackPolicy, retryPolicy);
            _busAsyncWrap = Policy.WrapAsync(fallbackAsyncPolicy, retryAsyncPolicy);
        }