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

            memoryCache.Set("test_key", 123, 3);
            memoryCache.Remove("test_key");

            Assert.IsNull(memoryCache.Get("test_key"));
            Assert.AreEqual(memoryCache.GetAll().Count, 0);
        }
Пример #2
0
        public void UseExpiryTimeAsync()
        {
            var memoryCache =
                new MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions()));

            memoryCache.Set("test_key", 123, 3);
            Assert.AreEqual(memoryCache.Get("test_key"), 123);
            System.Threading.Thread.Sleep(4 * 1000);
            Assert.IsNull(memoryCache.Get("test_key"));

            Assert.AreEqual(memoryCache.GetAll().Count, 0);
        }
Пример #3
0
        public void SetAndGetCacheItem()
        {
            var memoryCache =
                new MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions()));

            memoryCache.Set("test_key_str", "test");
            memoryCache.Set("test_key_int", 123);

            Assert.AreEqual(memoryCache.Get("test_key_str"), "test");
            Assert.AreEqual(memoryCache.Get <int>("test_key_int"), 123);

            Assert.AreEqual(memoryCache.GetAll().Count, 2);
            Assert.AreEqual(memoryCache.GetAllKeys().Count, 2);
        }
Пример #4
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);
        }