/// <summary>
        /// Initializes a new instance of the <see cref="T:EasyCaching.HybridCache.HybridCachingProvider"/> class.
        /// </summary>
        /// <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(
            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");


            // 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);
        }
        public void NotNull_Should_Not_Throw_ArgumentNullException_When_Argument_Is_Not_Null(object obj)
        {
            var ex = Record.Exception(() => ArgumentCheck.NotNull(obj, "name"));

            Assert.Null(ex);
        }
 public void NotNull_Should_Throw_ArgumentNullException_When_Argument_Is_Null()
 {
     Assert.Throws <ArgumentNullException>(() => ArgumentCheck.NotNull(null, "name"));
 }
Beispiel #4
0
        /// <summary>
        /// Uses the easy caching response caching.
        /// </summary>
        /// <returns>The easy caching response caching.</returns>
        /// <param name="app">App.</param>
        public static IApplicationBuilder UseEasyCachingResponseCaching(this IApplicationBuilder app)
        {
            ArgumentCheck.NotNull(app, nameof(app));

            return(app.UseMiddleware <EasyCachingResponseCachingMiddleware>());
        }