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); }
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); }
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); }
public async Task Exists() { var memoryCache = new MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions())); memoryCache.Set("test_key", 123, 3); Assert.IsTrue(memoryCache.Exists("test_key")); Assert.IsTrue(await memoryCache.ExistsAsync("test_key")); memoryCache.Remove("test_key"); Assert.IsFalse(memoryCache.Exists("test_key")); }