public void SlidingExpirationTest() { NFileCache target = new NFileCache("SlidingExpirationTest"); CacheItemPolicy policy = new CacheItemPolicy(); // Add an item and have it expire 500 ms from now policy.SlidingExpiration = new TimeSpan(0, 0, 0, 0, 500); target.Set("test", "test", policy); // Sleep for 200 Thread.Sleep(200); // Then try to access the item object result = target.Get("test"); Assert.AreEqual("test", result); // Sleep for another 350 Thread.Sleep(350); // Then try to access the item result = target.Get("test"); Assert.AreEqual("test", result); // Then sleep for more than 500 ms. Should be gone Thread.Sleep(600); result = target.Get("test"); Assert.IsNull(result); }
public void AutoShrinkTest() { NFileCache target = new NFileCache("AutoShrinkTest"); target.DefaultPolicy = new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(1D) }; target.MaxCacheSize = 20000; target.CacheResized += (object sender, FileCacheEventArgs args) => { Assert.IsNotNull(target["foo10"]); Assert.IsNotNull(target["foo40"]); }; for (int i = 0; i < 100; i++) { target["foo" + i] = "bar"; // Test to make sure it leaves items that have been recently accessed. if (i % 5 == 0 && i != 0) { var foo10 = target.Get("foo10"); var foo40 = target.Get("foo40"); } } }
public void AbsoluteExpirationTest() { NFileCache target = new NFileCache("AbsoluteExpirationTest"); CacheItemPolicy policy = new CacheItemPolicy(); // Add an item and have it expire yesterday policy.AbsoluteExpiration = DateTime.Now.AddDays(-1); target.Set("test", "test", policy); // Then try to access the item object result = target.Get("test"); Assert.IsNull(result); }
public void DefaultRegionTest() { NFileCache cacheWithDefaultRegion = new NFileCache("DefaultRegionTest"); cacheWithDefaultRegion.DefaultRegion = "foo"; NFileCache defaultCache = new NFileCache("DefaultRegionTest"); cacheWithDefaultRegion["foo"] = "bar"; string pull = defaultCache.Get("foo", "foo") as string; Assert.AreEqual("bar", pull); }
public void TestCount() { NFileCache target = new NFileCache("TestCount"); target["test"] = "test"; target["test"] = "bar"; Assert.AreEqual(0, target.GetCount("bar")); object result = target.Get("test"); Assert.AreEqual("bar", result); Assert.AreEqual(1, target.GetCount()); }
public void RemoveTest() { NFileCache target = new NFileCache("RemoveTest"); target.Set("test", "test", DateTime.Now.AddDays(3)); object result = target.Get("test"); Assert.AreEqual("test", result); // Check file system to be sure item was created string itemPath = target.GetItemPath("test"); Assert.IsTrue(File.Exists(itemPath)); // Now delete target.Remove("test"); result = target["test"]; Assert.IsNull(result); // Check file system to be sure item was removed Assert.IsFalse(File.Exists(itemPath)); }