Beispiel #1
0
        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"));
        }
Beispiel #2
0
        public void TimedLRUCache_RenewTTD()
        {
            TimedLRUCache <int, int> cache;

            cache          = new TimedLRUCache <int, int>();
            cache.RenewTTD = true;

            cache.Add(0, 0, TimeSpan.FromSeconds(1.0));
            cache.Add(1, 1, TimeSpan.FromSeconds(1.0));
            cache.Add(2, 2, TimeSpan.FromSeconds(1.0));
            cache.Add(3, 3, TimeSpan.FromSeconds(1.0));

            Thread.Sleep(TimeSpan.FromSeconds(1) + SysTime.Resolution);

            cache.Touch(0);
            cache.Flush();

            Assert.IsTrue(cache.ContainsKey(0));
            Assert.IsFalse(cache.ContainsKey(1));
            Assert.IsFalse(cache.ContainsKey(2));
            Assert.IsFalse(cache.ContainsKey(3));
        }
Beispiel #3
0
        public void TimedLRUCache_FlushLRU()
        {
            TimedLRUCache <int, int> cache;

            cache = new TimedLRUCache <int, int>();
            Assert.IsFalse(cache.RenewTTD);

            cache.Add(0, 0, TimeSpan.FromSeconds(1.0));
            cache.Add(1, 1, TimeSpan.FromSeconds(1.0));
            cache.Add(2, 2, TimeSpan.FromSeconds(4.0));
            cache.Add(3, 3, TimeSpan.FromSeconds(1.0));
            cache.Touch(0);
            cache.Touch(1);
            cache.Touch(3);

            // Item 2 should now head the LRU list

            Thread.Sleep(TimeSpan.FromSeconds(1) + SysTime.Resolution);
            cache.FlushLRU();

            // All the items should still be present since
            // item 2 hasn't expired yet and will block any
            // other items from be purged

            Assert.IsTrue(cache.ContainsKey(0));
            Assert.IsTrue(cache.ContainsKey(1));
            Assert.IsTrue(cache.ContainsKey(2));
            Assert.IsTrue(cache.ContainsKey(3));

            Thread.Sleep(TimeSpan.FromSeconds(3) + SysTime.Resolution);

            // All of the items should have expired by now

            cache.FlushLRU();

            Assert.IsFalse(cache.ContainsKey(0));
            Assert.IsFalse(cache.ContainsKey(1));
            Assert.IsFalse(cache.ContainsKey(2));
            Assert.IsFalse(cache.ContainsKey(3));
        }
Beispiel #4
0
        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));
        }