Exemplo n.º 1
0
        public async Task GetOrCreateStringAsync_MemoryEmpty_RedisContains_CallTwice()
        {
            const string generatedKey = "key1";

            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            var options    = Options.Create(new MemoryDistributedCacheOptions());
            var redisCache = new MemoryDistributedCache(options);
            await redisCache.SetStringAsync(generatedKey, Test);

            var logger = Substitute.For <ILogger <CommonCachingService> >();

            var cacheService = new CommonCachingService(memoryCache, redisCache, logger);

            var result = await cacheService.GetOrCreateStringAsync(generatedKey, () => Task.FromResult(Test), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5));

            Assert.AreEqual(Test, result);

            result = await cacheService.GetOrCreateStringAsync(generatedKey, () => Task.FromResult(Test), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5));

            Assert.AreEqual(Test, result);

            logger.Received(1).Log(LogLevel.Debug, Arg.Any <EventId>(), Arg.Is <Microsoft.Extensions.Logging.Internal.FormattedLogValues>(flv => flv.ToString() == $"Getting cached value from Distributed cache for key {generatedKey}"), null, Arg.Any <Func <Object, Exception, string> >());
            logger.Received(1).Log(LogLevel.Debug, Arg.Any <EventId>(), Arg.Is <Microsoft.Extensions.Logging.Internal.FormattedLogValues>(flv => flv.ToString() == $"Read cached value from Distributed cache for key {generatedKey}"), null, Arg.Any <Func <Object, Exception, string> >());
            logger.Received(0).Log(LogLevel.Debug, Arg.Any <EventId>(), Arg.Is <Microsoft.Extensions.Logging.Internal.FormattedLogValues>(flv => flv.ToString() == $"Stored in Distributed cache for key {generatedKey}"), null, Arg.Any <Func <Object, Exception, string> >());
        }
Exemplo n.º 2
0
        public async Task GetOrCreateStringAsync_MemoryEmpty_RedisContains_CallTwice()
        {
            const string generatedKey = "key1";

            IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());

            var options    = Options.Create(new MemoryDistributedCacheOptions());
            var redisCache = new MemoryDistributedCache(options);
            await redisCache.SetStringAsync(generatedKey, Test);

            var logger = Substitute.For <LoggerMock <CommonCachingService> >();

            var cacheService = new CommonCachingService(memoryCache, redisCache, logger);

            var result = await cacheService.GetOrCreateStringAsync(generatedKey, () => Task.FromResult(Test), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5));

            Assert.AreEqual(Test, result);

            result = await cacheService.GetOrCreateStringAsync(generatedKey, () => Task.FromResult(Test), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5));

            Assert.AreEqual(Test, result);

            logger.Received(1).Log(LogLevel.Debug, $"Getting cached value from Distributed cache for key {generatedKey}");
            logger.Received(1).Log(LogLevel.Debug, $"Read cached value from Distributed cache for key {generatedKey}");
            logger.Received(0).Log(LogLevel.Debug, $"Stored in Distributed cache for key {generatedKey}");
        }