Beispiel #1
0
        private LinkedListNode <CacheWrapper> ArrangeCacheWrapper()
        {
            var mockMemoryCache = new Mock <IDistributedCache>();

            mockMemoryCache.Setup(repo => repo.GetAsync(It.IsAny <string>(), default(CancellationToken))).ReturnsAsync((GetTestBook()[0]).SerializeObj());

            var mockRedisCache = new Mock <IDistributedCache>();

            mockRedisCache.Setup(repo => repo.GetAsync(It.IsAny <string>(), default(CancellationToken))).ReturnsAsync((GetTestBook()[1]).SerializeObj());

            var caches = new List <IDistributedCache>()
            {
                mockMemoryCache.Object, mockRedisCache.Object
            };

            var settings = new List <ICacheSetting>()
            {
                new CacheSetting {
                    Level = 0, AbsoluteExpirationDay = 1, AbsoluteExpirationRelativeToNowInMinutes = 15
                },
                new CacheSetting {
                    Level = 1, AbsoluteExpirationDay = 7, AbsoluteExpirationRelativeToNowInMinutes = 60
                }
            };

            return(CacheManagerFactory.CreateCacheHierachy(caches, settings));
        }
Beispiel #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddDistributedMemoryCache();
            services.AddDistributedRedisCache(option =>
            {
                option.Configuration = "localhost:6379";
                option.InstanceName  = "master";
            });

            services.AddTransient <IBookService, BookService>();
            services.AddTransient <HttpClient>();
            services.AddSingleton <IApiEndPointsSetting>(Configuration.GetSection("ApiEndPoints").Get <ApiEndPointsSetting>());
            services.AddTransient <IRepository <int, Book>, BookRepository>();
            services.AddTransient <IRepository <int, Comment>, CommentRepository>();

            var cacheSettings = Configuration.GetSection("CacheOptions").Get <CacheSetting[]>();

            services.AddSingleton <ICacheManager>(serviceProvider =>
                                                  new SampleCacheManager(CacheManagerFactory.CreateCacheHierachy(serviceProvider.GetServices <IDistributedCache>().ToList(), cacheSettings.ToList <ICacheSetting>()))
                                                  );
        }