示例#1
0
        static void Main(string[] args)
        {
            var options = new LRUCacheOptions(1000);

            //options.DataPersist = new FileBasedCacheStore("test.bin");
            cache = new LRUCache.LRUCache(options);

            var list = new List <Thread>();

            var sw = new Stopwatch();

            sw.Start();

            for (int i = 0; i < 10; i++)
            {
                var t = new Thread(BenchMarkTest);
                t.Start();

                list.Add(t);
            }

            foreach (var t in list)
            {
                t.Join();
            }

            sw.Stop();

            Console.WriteLine("ended in {0}", sw.ElapsedMilliseconds);

            Console.ReadLine();
        }
        public void TestMethod2()
        {
            LRUCache.LRUCache cache = new LRUCache.LRUCache(0);

            /*
             * ["LFUCache","put","get"]
             * [[0],[0,0],[0]]
             */
            cache.Put(0, 0);
            cache.Get(0);
        }
        public void CacheContainsItemsAfterAdd()
        {
            LRUCache<object> test = new LRUCache<object>();

            for (int i = 0; i < Iterations; i++ )
            {
                object o = new object();
                test.Add(o);
                Assert.AreEqual(test.Count, i + 1);
                Assert.AreEqual(test.Contains(o), true);
            }
        }
 public void TestMethod1()
 {
     LRUCache.LRUCache cache = new LRUCache.LRUCache(2);
     cache.Put(1, 1);
     cache.Put(2, 2);
     Assert.AreEqual(cache.Get(1), 1);           // returns 1
     cache.Put(3, 3);                            // evicts key 2
     Assert.AreEqual(cache.Get(2), -1);          // returns -1 (not found)
     cache.Put(4, 4);                            // evicts key 1
     Assert.AreEqual(cache.Get(1), -1);          // returns -1 (not found)
     Assert.AreEqual(cache.Get(3), 3);           // returns 3
     Assert.AreEqual(cache.Get(4), 4);           // returns 4
 }
        public void AddingItemMakesItNotLeastRecentlyUsed()
        {
            LRUCache<object> test = new LRUCache<object>();

            for (int i = 0; i < Iterations; i++)
            {
                object o = new object();
                test.Add(o);
            }

            object[] contents = new object[test.Count()];
            test.CopyTo(contents, 0);

            for (int i = 0; i < Iterations; i++)
            {
                object item = contents[i];
                test.Add(item);
                Assert.AreNotEqual(test.Oldest, item);
            }
        }
        public void OldestItemIsRemovedWhenCacheIsAtCapacity()
        {
            const int capacity = 20;
            LRUCache<object> target = new LRUCache<object>(capacity);

            for (int i = 0; i < capacity; i++)
            {
                target.Add(new object());
            }

            target.DiscardingOldestItem += OnDiscardingOldestItem;

            for (int i = 0; i < 100; i ++)
            {
                object o = target.Oldest;
                Assert.IsTrue(target.Contains(o));
                target.Add(new object());
                Assert.AreEqual(o, ItemBeingDiscarded);
                Assert.IsFalse(target.Contains(o));
            }
        }
示例#7
0
        public void CacheDoesNotContainItemAfterRemoval()
        {
            LRUCache <object> test     = new LRUCache <object>();
            object            toRemove = null;

            for (int i = 0; i < Iterations; i++)
            {
                object o = new object();
                test.Add(o);
                if (i == 17)
                {
                    toRemove = o;
                }
            }

            Assert.IsTrue(test.Contains(toRemove));
            Assert.IsTrue(test.Count == Iterations);
            test.Remove(toRemove);
            Assert.IsFalse(test.Contains(toRemove));
            Assert.IsTrue(test.Count == 99);
        }
示例#8
0
        public void CacheWithNoCapacityDoesNotDiscardItems()
        {
            LRUCache <object> test = new LRUCache <object>(0);

            object firstObject = null;

            for (int i = 0; i < LRUCache <object> .DefaultCapacity; i++)
            {
                object item = new object();
                firstObject = firstObject ?? item;
                test.Add(item);
            }

            Assert.AreEqual(test.Oldest, firstObject);
            for (int i = 0; i < LRUCache <object> .DefaultCapacity; i++)
            {
                object item = new object();
                test.Add(item);
            }
            Assert.AreEqual(test.Oldest, firstObject);
        }
示例#9
0
        static void Main(string[] args)
        {
            LRUCache <string, string> cache = new LRUCache <string, string>(10);

            for (int i = 0; i < 10; ++i)
            {
                cache.add(i.ToString(), i.ToString());
            }

            for (int i = 0; i < 10; ++i)
            {
                Console.WriteLine(cache.get(i.ToString()) + ",");
            }

            cache.add("100", "100");
            for (int i = 0; i < 10; ++i)
            {
                Console.WriteLine(cache.get(i.ToString()) + ",");
            }
            Console.ReadKey();
        }
示例#10
0
文件: 146LRUCache.cs 项目: auz34/MyGo
        static void Main(string[] args)
        {
            var cache = new LRUCache(3);

            cache.Set(1, 1);
            cache.Set(2, 2);
            cache.Set(3, 3);
            cache.Set(4, 4);

            var get1 = cache.Get(1);
            var get2 = cache.Get(2);

            Console.WriteLine("1 should not exist anymore proof = {0}", get1);
            Console.WriteLine("2 should still exist anymore proof = {0}", get2);
            cache.Set(5, 5);
            var get3 = cache.Get(3);

            Console.WriteLine("3 should not exist anymore proof = {0}", get3);


            Console.ReadLine();
        }
示例#11
0
        static void Main(string[] args)
        {
            LRUCache cache = new LRUCache(2);

            Console.WriteLine("null");

            cache.Put(1, 1);
            Console.WriteLine("null");

            cache.Put(2, 2);
            Console.WriteLine("null");

            // 1
            Console.WriteLine(cache.Get(1));

            cache.Put(3, 3);
            Console.WriteLine("null");

            // -1
            Console.WriteLine(cache.Get(2));

            cache.Put(4, 4);
            Console.WriteLine("null");

            // -1
            Console.WriteLine(cache.Get(1));

            // 3
            Console.WriteLine(cache.Get(3));

            // 4
            Console.WriteLine(cache.Get(4));


            Console.WriteLine("");

            cache = new LRUCache(2);
            Console.WriteLine("null");

            // -1
            Console.WriteLine(cache.Get(2));

            cache.Put(2, 6);
            Console.WriteLine("null");

            // -1
            Console.WriteLine(cache.Get(1));

            cache.Put(1, 5);
            Console.WriteLine("null");

            cache.Put(1, 2);
            Console.WriteLine("null");

            // 2
            Console.WriteLine(cache.Get(1));

            // 6
            Console.WriteLine(cache.Get(2));



            Console.WriteLine("");

            cache = new LRUCache(2);
            Console.WriteLine("null");

            cache.Put(2, 1);
            Console.WriteLine("null");

            cache.Put(1, 1);
            Console.WriteLine("null");

            cache.Put(2, 3);
            Console.WriteLine("null");

            cache.Put(4, 1);
            Console.WriteLine("null");

            // -1
            Console.WriteLine(cache.Get(1));

            // 3
            Console.WriteLine(cache.Get(2));
        }
示例#12
0
 internal LRUCacheEnumerator(LRUCache <T> cache)
 {
     Cache       = cache;
     CurrentNode = cache.List.First;
 }
        public void RemoveItemWhileOtherThreadsAreAddingItems()
        {
            const int ThreadCount = 20;

            Exceptions.Clear();

            // set the capacity high enough so that the item to remove won't
            // get discarded.
            Target = new LRUCache<object>(ThreadCount * AddCount + 1);
            object itemToRemove = new object();
            Target.Add(itemToRemove);

            ManualResetEvent[] events = new ManualResetEvent[ThreadCount];

            for (int i = 0; i < ThreadCount; i++)
            {
                events[i] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(Add, events[i]);
            }

            // wait until at least some items have been added to the cache.
            while(true)
            {
                if (Target.Count > 0)
                {
                    break;
                }
            }

            Debug.WriteLine(String.Format("RemoveItemWhileOtherThreadsAreAddingItems: {0} items in cache.", Target.Count));
            Assert.IsTrue(Target.Remove(itemToRemove));

            WaitHandle.WaitAll(events);

            string msg = "";
            if (Exceptions.Count != 0)
            {
                msg = String.Format("Unexpected exception: {0}", Exceptions[0].Message);
            }
            Assert.AreEqual(Exceptions.Count, 0, msg);
        }
示例#14
0
		public static void Main (string[] args)
		{

			LRUCache cache = new LRUCache (4);
			cache.Set (1, 11);
			cache.Set (2, 22);
			cache.Set (3, 33);
			cache.Set (4, 44);
			cache.Print ();
			Console.WriteLine (" ");
			Console.WriteLine(cache.Get (0));
			Console.WriteLine (cache.Get (1));
			cache.PrintMostRecentUsed (); 
			Console.WriteLine (cache.Get (4));
			cache.PrintMostRecentUsed ();
			cache.Set (5, 55);


			cache.Print ();

		}
示例#15
0
        public static void Main(string[] args)
        {
            LRUCache <Uri, WebResource> webcache = new LRUCache <Uri, WebResource>(_cbMaxCacheSize, new CacheEntryHander());

            /*
             * CODE BLOCK 1 :
             *
             * Exercise cache through the addition of 'count' web resources.
             */
            for (int count = 0; count < 20; count++)
            {
                Uri uri = new Uri(_uriDummyRoot, string.Format("webresource{0}", count));

                using (LockableValueRef <WebResource> refWebResource = webcache.Get(uri))
                {
                    if (null != refWebResource)
                    {
                        /*
                         * Already cached. Note that in this example this will never happen.
                         */
                    }
                    else
                    {
                        /*
                         * REQ'D IMPL :
                         *
                         * Note that in this sample code we perform no actual download of the web resource
                         * if it doesn't yet exist in the cache.
                         */
                        webcache.Put(uri, new WebResource(string.Format("dummy_cache_file{0}", count), random.Next((int)_cbMaxFileSize)));
                    }
                }

                Console.WriteLine("** Cache has {0} entries with size {1} (max. size {2}) **", webcache.Count, webcache.CurrentSize, webcache.MaxSize);
            }

            /*
             * CODE BLOCK 2 :
             *
             * Serialize out the web cache.
             * Deserialize back the web cache (in this case to demonstrate a deep clone).
             */
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream(8192))
            {
#pragma warning disable 0219
                LRUCache <Uri, WebResource> webcacheCloned = null;
#pragma warning restore 0219
                try
                {
                    formatter.Serialize(stream, webcache);
                    stream.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    webcacheCloned = formatter.Deserialize(stream) as LRUCache <Uri, WebResource>;
                }
                catch (Exception)
                {
                    /*
                     * REQ'D IMPL : Handle serialization exception.
                     */
                }
            }
        }
        public void CopyToWorksWhileItemsAreBeingAdded()
        {
            Target = new LRUCache<object>();
            Exceptions.Clear();

            const int ThreadCount = 20;
            ManualResetEvent[] events = new ManualResetEvent[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                events[i] = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(Add, events[i]);
            }

            // wait until at least some items have been added to the cache.
            while(true)
            {
                if (Target.Count > 0)
                {
                    break;
                }
            }

            // this has to be sized to the target's capacity, not count, because
            // the count may change before CopyTo executes.
            object[] copy = new object[Target.Capacity];
            try
            {
                Target.CopyTo(copy, 0);
                int count = 0;
                foreach (object o in copy)
                {
                    if (o != null)
                    {
                        count++;
                    }
                }
                Debug.WriteLine(String.Format("CopyToWorksWhileItemsAreBeingAdded: {0} items in copy after CopyTo.", count));
            }
            catch (Exception e)
            {
                Assert.Fail(String.Format("Exception during CopyTo: {0}", e.Message));
            }

            WaitHandle.WaitAll(events);

            string msg = "";
            if (Exceptions.Count != 0)
            {
                msg = String.Format("Unexpected exception: {0}", Exceptions[0].Message);
            }
            Assert.AreEqual(Exceptions.Count, 0, msg);
        }
        public void OKToRemoveItemNotInCache()
        {
            LRUCache<object> test = new LRUCache<object>();

            for (int i = 0; i < Iterations; i++)
            {
                object item = new object();
                test.Add(item);
            }
            object not_in_cache = new object();
            Assert.IsFalse(test.Remove(not_in_cache));
        }
        public void CacheWithNoCapacityDoesNotDiscardItems()
        {
            LRUCache<object> test = new LRUCache<object>(0);

            object firstObject = null;
            for (int i = 0; i < LRUCache<object>.DefaultCapacity; i++)
            {
                object item = new object();
                firstObject = firstObject ?? item;
                test.Add(item);
            }

            Assert.AreEqual(test.Oldest, firstObject);
            for (int i = 0; i < LRUCache<object>.DefaultCapacity; i++)
            {
                object item = new object();
                test.Add(item);
            }
            Assert.AreEqual(test.Oldest, firstObject);
        }
        public void ItemsAreRemovedInReverseOfOrderAdded()
        {
            LRUCache<object> test = new LRUCache<object>();
            List<object> itemsAdded = new List<object>();

            for (int i = 0; i < Iterations; i++)
            {
                object o = new object();
                test.Add(o);
                itemsAdded.Add(o);
            }

            foreach (object o in itemsAdded)
            {
                Assert.AreEqual(test.Oldest, o);
                Assert.IsTrue(test.Remove(o));
            }

            Assert.AreEqual(test.Count, 0);
        }
 public void CacheIsNotReadOnly()
 {
     LRUCache<object> cache = new LRUCache<object>();
     Assert.IsFalse(cache.IsReadOnly);
 }
        public void CacheDoesNotContainItemAfterRemoval()
        {
            LRUCache<object> test = new LRUCache<object>();
            object toRemove = null;

            for (int i = 0; i < Iterations; i++)
            {
                object o = new object();
                test.Add(o);
                if (i == 17)
                {
                    toRemove = o;
                }
            }

            Assert.IsTrue(test.Contains(toRemove));
            Assert.IsTrue(test.Count == Iterations);
            test.Remove(toRemove);
            Assert.IsFalse(test.Contains(toRemove));
            Assert.IsTrue(test.Count == 99);
        }
        public void CopyToReturnsCorrectArray()
        {
            const int itemCount = 100;
            object[] items = new object[itemCount];
            object[] contents = new object[itemCount];
            LRUCache<object> target = new LRUCache<object>();

            for (int i = 0; i < itemCount; i++)
            {
                items[i] = new object();
                target.Add(items[i]);
            }

            target.CopyTo(contents, 0);
            for (int i = 0; i < itemCount; i++)
            {
                Assert.AreEqual(items[i], contents[i]);
            }
        }
示例#23
0
 public int Execute(LRUCache cache)
 {
     cache.Put(this.Key, this.Value);
     return(0);
 }
示例#24
0
        public void CacheIsNotReadOnly()
        {
            LRUCache <object> cache = new LRUCache <object>();

            Assert.IsFalse(cache.IsReadOnly);
        }
        public void OldestItemIsLastItemAdded()
        {
            LRUCache<object> test = new LRUCache<object>();
            object firstAdded = null;

            for (int i = 0; i < Iterations; i++)
            {
                object o = new object();
                if (i == 0)
                {
                    firstAdded = o;
                }
                test.Add(o);
            }

            Assert.AreEqual(test.Oldest, firstAdded);
        }
        private void OnDiscardingOldestItem(object sender, EventArgs e)
        {
            LRUCache <object> cache = (LRUCache <object>)sender;

            ItemBeingDiscarded = cache.Oldest;
        }
        public void CacheIsEnumerable()
        {
            LRUCache<int> cache = new LRUCache<int>();
            int i;

            for (i = 0; i < 100; i++)
            {
                cache.Add(i);
            }

            i = 0;
            foreach (int item in cache)
            {
                Assert.AreEqual(item, i++);
            }
        }