public void UpdateValueTest() { CacheItem cacheItem = new CacheItem("key", 3); Assert.AreEqual(3, (int)cacheItem.Value, "value"); cacheItem.Value = "hello"; Assert.AreEqual("hello", (string)cacheItem.Value, "value"); }
public void CreateTest() { CacheItem cacheItem = new CacheItem("key", 3); Assert.AreEqual("key", cacheItem.Key, "Key"); Assert.AreEqual(3, (int)cacheItem.Value, "value"); Assert.IsTrue(cacheItem.LastAccessed <= DateTime.Now, "Now"); Assert.IsTrue(cacheItem.LastAccessed > DateTime.Today, "Today"); }
public void UpdateLastAccessedTest() { CacheItem cacheItem = new CacheItem("key", 3); DateTime cacheItemCreated = cacheItem.LastAccessed; Thread.Sleep(100); cacheItem.UpdateLastAccessed(); Assert.IsTrue(cacheItem.LastAccessed > cacheItemCreated); }
/// <summary> /// Write a value to the cache /// </summary> /// <param name="key">the cache key</param> /// <param name="data">the value cached</param> public void Write(string key, object data) { if (this.cacheData.ContainsKey(key)) { this.cacheData[key].Value = data; } else { CacheItem newCacheItem = new CacheItem(key, data); this.cacheData.Add(key, newCacheItem); } }
/// <summary> /// Return true if the item has expired, is past the timeout /// </summary> /// <param name="item"></param> /// <returns></returns> private bool ItemHasTimedOut(CacheItem item) { return item.LastAccessed < DateTime.Now - this.Timeout; }