Beispiel #1
0
 internal ZeusCachingProfileOptions(bool isEnabled,
                                    CachingAdapterMode cachingAdapterMode,
                                    Func <IServiceProvider, HttpContext, string> cacheKeyHandler,
                                    Func <IServiceProvider, HttpContext, bool> cachingPredicate,
                                    Func <IServiceProvider, HttpContext, object, object> wrappingResultHandler)
 {
     CachingAdapterMode    = cachingAdapterMode;
     IsEnabled             = isEnabled;
     CacheKeyHandler       = cacheKeyHandler;
     CachingPredicate      = cachingPredicate;
     WrappingResultHandler = wrappingResultHandler;
 }
        public void Create_ForGivenMode_ReturnsExpectedAdapter(CachingAdapterMode mode, Type expectedInstanceType)
        {
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(x => x.GetService(typeof(IDistributedCache))).Returns(Mock.Of <IDistributedCache>());
            serviceProvider.Setup(x => x.GetService(typeof(IMemoryCache))).Returns(Mock.Of <IMemoryCache>());
            serviceProvider.Setup(x => x.GetService(typeof(ICachingAdapter))).Returns(new FakeCachingAdapter());

            CachingAdapterFactory factory = new CachingAdapterFactory(serviceProvider.Object);


            var adapter = factory.Create(mode);


            Assert.NotNull(adapter);
            Assert.IsType(expectedInstanceType, adapter);
        }
Beispiel #3
0
        public ICachingAdapter Create(CachingAdapterMode mode)
        {
            switch (mode)
            {
            case CachingAdapterMode.AutomaticDiscovery:
                return(new AutoDiscoveryCachingAdapter(_serviceProvider));

            case CachingAdapterMode.DistributedCache:
                var distributedCache = _serviceProvider.GetRequiredService <IDistributedCache>();
                return(new DistributedCachingAdapter(distributedCache));

            case CachingAdapterMode.MemoryCache:
                var memoryCache = _serviceProvider.GetRequiredService <IMemoryCache>();
                return(new MemoryCachingAdapter(memoryCache));

            case CachingAdapterMode.Custom:
                return(_serviceProvider.GetRequiredService <ICachingAdapter>());

            default:
                throw new NotSupportedException($"Caching adapter mode {mode} is not supprted.");
            }
        }
Beispiel #4
0
 /// <summary>
 /// Returns the user-registered caching adapter for caching.
 /// </summary>
 /// <returns></returns>
 public ZeusCachingProfileOptions UseCustomCachingAdapter()
 {
     CachingAdapterMode = CachingAdapterMode.Custom;
     return(this);
 }
Beispiel #5
0
 /// <summary>
 /// Sets the distributed caching adapter mode to <see cref="CachingAdapterMode.MemoryCache"/> and uses the
 /// underlying <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> service instance for caching.
 /// </summary>
 /// <returns></returns>
 public ZeusCachingProfileOptions UseInMemoryCachingAdapter()
 {
     CachingAdapterMode = CachingAdapterMode.MemoryCache;
     return(this);
 }
Beispiel #6
0
 /// <summary>
 /// Sets the distributed caching adapter mode to <see cref="CachingAdapterMode.DistributedCache"/> and uses the
 /// underlying <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/> service instance for caching.
 /// </summary>
 /// <returns></returns>
 public ZeusCachingProfileOptions UseDistributedCachingAdapter()
 {
     CachingAdapterMode = CachingAdapterMode.DistributedCache;
     return(this);
 }
Beispiel #7
0
 /// <summary>
 /// Sets the distributed caching adapter mode to <see cref="CachingAdapterMode.AutomaticDiscovery"/>. In this mode,
 /// the profile options looks for any implementations for <see cref="Microsoft.Extensions.Caching.Distributed.IDistributedCache"/>
 /// or <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> service to resolve.
 /// </summary>
 /// <returns></returns>
 public ZeusCachingProfileOptions UseAutoDiscoveryCachingAdapter()
 {
     CachingAdapterMode = CachingAdapterMode.AutomaticDiscovery;
     return(this);
 }