Exemplo n.º 1
0
        private ICache GetCache(CacheConfig config)
        {
            ICache cache = null;
            var    normalisedCacheToUse = !string.IsNullOrWhiteSpace(config.CacheToUse) ? config.CacheToUse.ToLowerInvariant() : string.Empty;

            switch (normalisedCacheToUse)
            {
            case CacheTypes.MemoryCache:
                cache = new MemoryCacheAdapter(_logger);
                break;

            case CacheTypes.WebCache:
                cache = new WebCacheAdapter(_logger);
                break;

            case CacheTypes.AppFabricCache:
                cache = new AppFabricCacheAdapter(_logger, config);
                break;

            case CacheTypes.memcached:
                cache = new memcachedAdapter(_logger, config);
                break;

            case CacheTypes.redis:
                cache = new RedisCacheAdapter(_logger, config);
                break;

            default:
                cache = new MemoryCacheAdapter(_logger);
                break;
            }
            return(cache);
        }
        public async void WriteEntry_WritesEntryUsingCorrectAbsoluteExpiration_ReadBeforeExpirationTime()
        {
            const string cacheKey           = "test-cache-key";
            const string value              = "test-value";
            DateTime     absoluteExpiration = new DateTime(year: 2018, month: 3, day: 17, hour: 8, minute: 0, second: 0);

            using (IMemoryCache memoryCache = GetMemoryCache())
            {
                MemoryCacheAdapter memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: TimeSpan.FromDays(10));

                using (CurrentTime.UseMockUtcNow(absoluteExpiration.AddHours(-1)))
                {
                    await memoryCacheAdapter.WriteEntry(cacheKey, new CacheEntry <string>(value, absoluteExpiration));
                }

                using (CurrentTime.UseMockUtcNow(absoluteExpiration.AddSeconds(-1)))
                {
                    CacheEntry <string> cacheEntry = memoryCache.Get <CacheEntry <string> >(cacheKey);

                    Assert.NotNull(cacheEntry);
                    Assert.Equal(actual: cacheEntry.Value, expected: value);
                    Assert.Equal(actual: cacheEntry.AbsoluteExpiration, expected: absoluteExpiration);
                }
            }
        }
        public async void WriteEntry_WritesEntryUsingCorrectSlidingExpiration_ReadAfterExpirationTime()
        {
            const string cacheKey           = "test-cache-key";
            const string value              = "test-value";
            DateTime     absoluteExpiration = new DateTime(year: 2018, month: 3, day: 17, hour: 8, minute: 0, second: 0);

            using (IMemoryCache memoryCache = GetMemoryCache())
            {
                TimeSpan           slidingExpiration  = TimeSpan.FromMinutes(10);
                MemoryCacheAdapter memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: slidingExpiration);
                DateTime           writeTime          = absoluteExpiration.AddHours(-1);

                using (CurrentTime.UseMockUtcNow(writeTime))
                {
                    await memoryCacheAdapter.WriteEntry(cacheKey, new CacheEntry <string>(value, absoluteExpiration));
                }

                using (CurrentTime.UseMockUtcNow(writeTime.Add(slidingExpiration).AddSeconds(1)))
                {
                    CacheEntry <string> cacheEntry = memoryCache.Get <CacheEntry <string> >(cacheKey);

                    Assert.Null(cacheEntry);
                }
            }
        }
Exemplo n.º 4
0
        public void Initialize()
        {
            var options = Substitute.For <IOptions <ms.MemoryCacheOptions> >();

            options.Value.Returns(new ms.MemoryCacheOptions());

            var memoryCache = new MemoryCacheAdapter(new ms.MemoryCache(options));

            _vmFactory = new VMFactory(memoryCache, new VMTypesAccessor());
        }
        public async void ReadEntry_EntryDoesNotExist_ReturnsNull()
        {
            const string cacheKey = "test-cache-key";

            using (IMemoryCache memoryCache = GetMemoryCache())
            {
                MemoryCacheAdapter memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: TimeSpan.FromMinutes(1));

                Assert.Null(await memoryCacheAdapter.ReadEntry <string>(cacheKey));
            }
        }
        public async void ReadEntry_EntryExists_ReturnsEntry()
        {
            const string cacheKey           = "test-cache-key";
            const string value              = "test-value";
            DateTime     absoluteExpiration = new DateTime(year: 2018, month: 3, day: 17, hour: 8, minute: 0, second: 0);

            using (IMemoryCache memoryCache = GetMemoryCache())
                using (CurrentTime.UseMockUtcNow(absoluteExpiration.AddHours(-1)))
                {
                    memoryCache.Set(cacheKey, new CacheEntry <string>(value, absoluteExpiration));

                    MemoryCacheAdapter  memoryCacheAdapter = new MemoryCacheAdapter(memoryCache, slidingExpiration: TimeSpan.FromMinutes(1));
                    CacheEntry <string>?cacheEntry         = await memoryCacheAdapter.ReadEntry <string>(cacheKey);

                    Assert.NotNull(cacheEntry);
                    Assert.Equal(actual: cacheEntry !.Value, expected: value);
                    Assert.Equal(actual: cacheEntry !.AbsoluteExpiration, expected: absoluteExpiration);
                }
        }