/// <summary> /// Clears all cached authentication information. /// </summary> /// <exception cref="AuthenticationException">Thrown if the authenticator is not open.</exception> public void ClearCache() { using (TimedLock.Lock(this)) { if (!isOpen) { throw new AuthenticationException(NotOpenMsg); } if (cache != null) { cache.Clear(); } } }
public void TimedLRUCache_Basic() { TimedLRUCache <string, string> cache; string value; cache = new TimedLRUCache <string, string>(StringComparer.OrdinalIgnoreCase); Assert.AreEqual(0, cache.Count); cache.Add("foo", "bar"); Assert.AreEqual(1, cache.Count); Assert.AreEqual("bar", cache["foo"]); try { value = cache["xxx"]; Assert.Fail("Expected a KeyNotFoundException"); } catch (Exception e) { Assert.AreEqual(typeof(KeyNotFoundException).Name, e.GetType().Name); } cache["foo"] = "foobar"; Assert.AreEqual("foobar", cache["foo"]); cache["bar"] = "boobar"; Assert.AreEqual("boobar", cache["bar"]); Assert.AreEqual("foobar", cache["foo"]); Assert.IsTrue(cache.TryGetValue("foo", out value)); Assert.AreEqual("foobar", value); Assert.IsTrue(cache.TryGetValue("bar", out value)); Assert.AreEqual("boobar", value); Assert.IsFalse(cache.TryGetValue("xxx", out value)); Assert.IsTrue(cache.ContainsKey("foo")); Assert.IsTrue(cache.ContainsKey("bar")); Assert.IsFalse(cache.ContainsKey("xxx")); cache.Remove("foo"); Assert.IsFalse(cache.ContainsKey("foo")); cache.Remove("bar"); Assert.IsFalse(cache.ContainsKey("bar")); cache.Remove("xxx"); Assert.IsFalse(cache.ContainsKey("xxx")); cache["foo"] = "foobar"; cache["bar"] = "boobar"; Assert.AreEqual(2, cache.Count); Assert.IsTrue(cache.ContainsKey("foo")); Assert.IsTrue(cache.ContainsKey("bar")); cache.Clear(); Assert.AreEqual(0, cache.Count); Assert.IsFalse(cache.ContainsKey("foo")); Assert.IsFalse(cache.ContainsKey("bar")); }
public void TimedLRUCache_MaxItems() { TimedLRUCache <int, int> cache; int value; cache = new TimedLRUCache <int, int>(); Assert.AreEqual(0, cache.Count); Assert.AreEqual(int.MaxValue, cache.MaxItems); cache.MaxItems = 3; //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache.Add(i, i); } Assert.AreEqual(3, cache.Count); cache.Add(3, 3); Assert.AreEqual(3, cache.Count); Assert.IsFalse(cache.ContainsKey(0)); Assert.IsTrue(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache[i] = i; } Assert.AreEqual(3, cache.Count); cache[3] = 3; Assert.AreEqual(3, cache.Count); Assert.IsFalse(cache.ContainsKey(0)); Assert.IsTrue(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache[i] = i; } Assert.AreEqual(3, cache.Count); cache.MaxItems = 2; Assert.AreEqual(2, cache.Count); Assert.IsFalse(cache.ContainsKey(0)); Assert.IsTrue(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); cache.MaxItems = 3; //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache[i] = i; } Assert.IsTrue(cache.ContainsKey(0)); Assert.AreEqual(3, cache.Count); cache[3] = 3; Assert.AreEqual(3, cache.Count); Assert.IsTrue(cache.ContainsKey(0)); Assert.IsFalse(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache[i] = i; } cache.Touch(0); Assert.AreEqual(3, cache.Count); cache[3] = 3; Assert.AreEqual(3, cache.Count); Assert.IsTrue(cache.ContainsKey(0)); Assert.IsFalse(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); //--------------------------------------------- cache.Clear(); for (int i = 0; i < 3; i++) { cache[i] = i; } cache.TryGetValue(0, out value); Assert.AreEqual(3, cache.Count); cache[3] = 3; Assert.AreEqual(3, cache.Count); Assert.IsTrue(cache.ContainsKey(0)); Assert.IsFalse(cache.ContainsKey(1)); Assert.IsTrue(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); //--------------------------------------------- cache.Clear(); for (int i = 0; i < 6; i++) { cache[i] = i; } cache.TryGetValue(0, out value); Assert.AreEqual(3, cache.Count); cache[3] = 3; Assert.AreEqual(3, cache.Count); Assert.IsFalse(cache.ContainsKey(0)); Assert.IsFalse(cache.ContainsKey(1)); Assert.IsFalse(cache.ContainsKey(2)); Assert.IsTrue(cache.ContainsKey(3)); Assert.IsTrue(cache.ContainsKey(4)); Assert.IsTrue(cache.ContainsKey(5)); }
public void TimedLRUCache_AutoDispose() { TimedLRUCache <int, TestItem> cache; DisposableItem dItem0 = new DisposableItem(0); DisposableItem dItem1 = new DisposableItem(1); DisposableItem dItem2 = new DisposableItem(2); TestItem item0 = new TestItem(0); TestItem item1 = new TestItem(1); TestItem item2 = new TestItem(2); cache = new TimedLRUCache <int, TestItem>(); cache.AutoDispose = true; cache.DefaultTTL = TimeSpan.FromMilliseconds(200); // Verify that disposable items are disposed when they // are implicitly removed from the cache when the maximum // number of items allowed has been exceeded. cache.MaxItems = 2; cache.Add(0, dItem0); cache.Add(1, dItem1); cache.Add(2, dItem2); Assert.AreEqual(2, cache.Count); Assert.IsTrue(dItem0.IsDisposed); Assert.IsFalse(dItem1.IsDisposed); Assert.IsFalse(dItem2.IsDisposed); // Verify that disposable items are disposed when the // cache is cleared. cache.Clear(); Assert.IsTrue(dItem1.IsDisposed); Assert.IsTrue(dItem2.IsDisposed); // Verify that disposable items are disposed when they // are explicitly removed. dItem0.IsDisposed = false; cache.Add(0, dItem0); cache.Remove(0); Assert.IsTrue(dItem0.IsDisposed); // Verify that disposable items are disposed when they // are replaced in the cache. cache.Clear(); dItem0.IsDisposed = false; dItem1.IsDisposed = false; cache.Add(0, dItem0); cache[0] = dItem1; Assert.IsTrue(dItem0.IsDisposed); // Verify that replacing the same disposable item instance // doesn't dispose the object. cache.Clear(); dItem0.IsDisposed = false; cache.Add(0, dItem0); cache[0] = dItem0; Assert.IsFalse(dItem0.IsDisposed); // Verify disposal after flushing. cache.Clear(); dItem0.IsDisposed = false; dItem1.IsDisposed = false; dItem2.IsDisposed = false; cache.Add(0, dItem0); cache.Add(1, dItem1); cache.Add(2, dItem2); Thread.Sleep(250); cache.Flush(); Assert.IsTrue(dItem0.IsDisposed); Assert.IsTrue(dItem1.IsDisposed); Assert.IsTrue(dItem2.IsDisposed); cache.Clear(); dItem0.IsDisposed = false; dItem1.IsDisposed = false; dItem2.IsDisposed = false; cache.Add(0, dItem0); cache.Add(1, dItem1); cache.Add(2, dItem2); Thread.Sleep(250); cache.FlushLRU(); Assert.IsTrue(dItem0.IsDisposed); Assert.IsTrue(dItem1.IsDisposed); Assert.IsTrue(dItem2.IsDisposed); // Verify that non-disposable items don't cause trouble // when AutoDispose=true cache.Clear(); cache.Add(0, item0); cache.Add(1, item1); cache.Add(2, item2); cache.Remove(1); cache[1] = new TestItem(3); cache[2] = cache[2]; cache.Clear(); cache.Add(0, item0); cache.Add(1, item1); cache.Add(2, item2); Thread.Sleep(250); cache.Flush(); cache.Add(0, item0); cache.Add(1, item1); cache.Add(2, item2); Thread.Sleep(250); cache.FlushLRU(); }