예제 #1
0
        static StartupSingletons()
        {
            var config = ConfigurationRoot = new ConfigurationBuilder().AddJsonFile("config.json")
                                             .AddJsonFile("config.development.json", optional: true)
                                             .Build();

            var json = new System.Text.Json.JsonSerializerOptions();

            json.ConfigurePacemDefaults();
            var cacheOptions = new MemoryDistributedCacheOptions();
            var cache        = new MemoryDistributedCache(new OptionsWrapper <MemoryDistributedCacheOptions>(cacheOptions));

            BlobStorageUpdater = new BlobStorageUpdater(cache, config, new OptionsWrapper <System.Text.Json.JsonSerializerOptions>(json));
        }
        public async Task SetAsyncUsesExpiryPolicy()
        {
            var fixedTime = new DateTimeOffset(2020, 1, 27, 21, 20, 0, TimeSpan.Zero);
            var mockTime  = Substitute.For <ISystemClock>();

            mockTime.UtcNow.Returns(fixedTime);
            var options = new MemoryDistributedCacheOptions
            {
                Clock = mockTime
            };
            var cache             = new MemoryDistributedCache(Options.Create(options));
            var expirationOptions = new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = fixedTime.Subtract(TimeSpan.FromSeconds(1))
            };
            await cache.SetAsync("should_be_gone", new Example { Name = "to expire" }, expirationOptions);

            var missing = await cache.GetAsync <Example>("should_be_gone");

            Assert.Null(missing);
        }
 /// <summary>
 ///     Replace distributed cache to in-memory cache for testing.
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public SystemUnderTest ReplaceDistributedInMemoryCache(MemoryDistributedCacheOptions options = default)
 {
     ReplaceService <IDistributedCache>(new MemoryDistributedCache(
                                            new OptionsWrapper <MemoryDistributedCacheOptions>(options ?? new MemoryDistributedCacheOptions())));
     return(this);
 }
예제 #4
0
 private void ConfigureDistrubutedCacheOptions(MemoryDistributedCacheOptions options)
 {
 }
예제 #5
0
        /// <summary>
        /// Configure the Cache
        ///
        /// Some Helpful Links:
        ///		https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-3.1#caching-basics
        ///		https://stackexchange.github.io/StackExchange.Redis/Configuration
        /// </summary>
        /// <param name="services"></param>
        /// <param name="appConfig"></param>
        private static void ConfigureCache(IServiceCollection services, AppConfig appConfig)
        {
            switch (appConfig.Cache.CacheType)
            {
            case ConfigEnums.CacheType.Memory:
            {
                // Here you can set your memory cache options, for now just default
                var memoryCacheOptions = new MemoryCacheOptions();

                // Add Memory Cache to our Services
                services.AddMemoryCache(o => {
                        o = memoryCacheOptions;
                    });

                break;
            }

            case ConfigEnums.CacheType.DistributedMemory:
            {
                // Here you can set your distributed memory cache options, for now just default
                var distributedMemoryCacheOptions = new MemoryDistributedCacheOptions();

                // Add Distributed Memory Cache to our Services
                services.AddDistributedMemoryCache(o => {
                        o = distributedMemoryCacheOptions;
                    });

                break;
            }

            case ConfigEnums.CacheType.RedisCache:
            {
                // Here you can set your redis cache options, for now just default
                var redisCacheOptions = new RedisCacheOptions();

                redisCacheOptions.ConfigurationOptions.ClientName  = appConfig.Cache.RedisClientId;
                redisCacheOptions.ConfigurationOptions.Password    = appConfig.Cache.RedisSecret;
                redisCacheOptions.ConfigurationOptions.ServiceName = appConfig.Cache.RedisServiceName;
                redisCacheOptions.ConfigurationOptions.EndPoints.Add(appConfig.Cache.RedisEndpoint);

                // Add Redis Cache to our Services
                services.AddDistributedRedisCache(o => {
                        o = redisCacheOptions;
                    });

                break;
            }

            default:
            {
                // Here you can set your memory cache options, for now just default
                var memoryCacheOptions = new MemoryCacheOptions();

                // Add Memory Cache to our Services
                services.AddMemoryCache(o => {
                        o = memoryCacheOptions;
                    });

                break;
            }
            }
        }
예제 #6
0
 private void SetupDistributedCache(MemoryDistributedCacheOptions options)
 {
     options.SizeLimit               = _applicationSettings.MemoryCacheSizeLimit;
     options.CompactionPercentage    = _applicationSettings.MemoryCacheCompactionPercentage;
     options.ExpirationScanFrequency = _applicationSettings.MemoryCacheExpirationScanFrequency;
 }