Пример #1
0
        public async Task SetAndGetCacheItemAsync()
        {
            var memoryCache =
                new MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions()));

            await memoryCache.SetAsync("test_key_str", "test");

            await memoryCache.SetAsync("test_key_int", 123);

            Assert.AreEqual(await memoryCache.GetAsync("test_key_str"), "test");
            Assert.AreEqual(await memoryCache.GetAsync <int>("test_key_int"), 123);
        }
Пример #2
0
        public async Task UseExpiryTime_AbsoluteAsync()
        {
            var memoryCache =
                new MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions()));

            await memoryCache.SetAsync("test_key_async", 456, DateTime.Now.AddSeconds(5));

            Assert.AreEqual(memoryCache.Get("test_key_async"), 456);
            Assert.AreNotEqual(memoryCache.Get("test_key_async"), 123);
            System.Threading.Thread.Sleep(3 * 1000);
            Assert.AreEqual(memoryCache.Get("test_key_async"), 456);
            System.Threading.Thread.Sleep(3 * 1000);
            Assert.IsNull(memoryCache.Get("test_key_async"));

            Assert.AreEqual(memoryCache.GetAll().Count, 0);
        }