Beispiel #1
0
        public static void Handle()
        {
Handle:
            Console.WriteLine($"Plase in put Cache Number:");
            int.TryParse(Console.ReadLine(), out int number);

            Stopwatch sw = Stopwatch.StartNew();

            Parallel.For(0, number, i =>
            {
                cache.Set(i.ToString(), i.ToString() + ":Value");
            });
            sw.Stop();
            Console.WriteLine($"运行时间:{sw.ElapsedMilliseconds}/ms,TPS:{number * 1000 / sw.ElapsedMilliseconds}");
            goto Handle;
to:
            Console.WriteLine($"Total:{cache.Count}");
            var key = Console.ReadLine();

            if (key == "all")
            {
                foreach (var item in cache._linkedList)
                {
                    //cache.TryGet(item, out string itemValue);
                    Console.WriteLine(item);
                }
                goto to;
            }
            if (key == "r")
            {
                Console.WriteLine($"Plase in put Remove key:");
                var removeKey = Console.ReadLine();
                cache.Remove(removeKey);
                goto to;
            }
            if (key == "c")
            {
                cache.Dispose();
                goto to;
            }
            if (cache.TryGet(key, out String x))
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine("Not Found!");
                cache.Set(key, key + ":Value");
            }
            goto to;
        }
Beispiel #2
0
        public static void Handle()
        {
            for (int i = 0; i < 10; i++)
            {
                cache.Set(i.ToString(), i.ToString() + ":Value");
            }
to:
            Console.WriteLine($"Total:{cache.Count}");
            var key = Console.ReadLine();

            if (key == "all")
            {
                foreach (var item in cache._linkedList)
                {
                    //cache.TryGet(item, out string itemValue);
                    Console.WriteLine(item);
                }
                goto to;
            }
            if (key == "r")
            {
                Console.WriteLine($"Plase in put Remove key:");
                var removeKey = Console.ReadLine();
                cache.Remove(removeKey);
                goto to;
            }
            if (key == "c")
            {
                cache.Dispose();
                goto to;
            }
            if (cache.TryGet(key, out String x))
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine("Not Found!");
                cache.Set(key, key + ":Value");
            }
            goto to;
        }
Beispiel #3
0
        static void LRUCacheTest()
        {
            try
            {
                bool   runForever = true;
                byte[] data       = InitByteArray(_DataLength, 0x00);
                byte[] keyData;

                LRUCache <string, byte[]> cache = new LRUCache <string, byte[]>(_Capacity, _EvictCount);
                cache.Events.Added        += Added;
                cache.Events.Cleared      += Cleared;
                cache.Events.Disposed     += Disposed;
                cache.Events.Evicted      += Evicted;
                cache.Events.Prepopulated += Prepopulated;
                cache.Events.Removed      += Removed;
                cache.Events.Replaced     += Replaced;

                Thread.Sleep(250);

                while (runForever)
                {
                    Console.Write("Command [LRU] > ");
                    string userInput = Console.ReadLine();
                    if (String.IsNullOrEmpty(userInput))
                    {
                        continue;
                    }

                    switch (userInput.ToLower())
                    {
                    case "?":
                        MenuLRU();
                        break;

                    case "get":
                        Console.Write("Key > ");
                        string getKey = Console.ReadLine();
                        if (String.IsNullOrEmpty(getKey))
                        {
                            break;
                        }
                        if (cache.TryGet(getKey, out keyData))
                        {
                            Console.WriteLine("Cache hit");
                        }
                        else
                        {
                            Console.WriteLine("Cache miss");
                        }
                        break;

                    case "all":
                        Dictionary <string, byte[]> dump = cache.All();
                        if (dump != null && dump.Count > 0)
                        {
                            foreach (KeyValuePair <string, byte[]> entry in dump)
                            {
                                Console.WriteLine(entry.Key);
                            }
                            Console.WriteLine("Count: " + dump.Count + " entries");
                        }
                        else
                        {
                            Console.WriteLine("Empty");
                        }
                        break;

                    case "add":
                        Console.Write("Key  : ");
                        string key = Console.ReadLine();
                        if (String.IsNullOrEmpty(key))
                        {
                            break;
                        }
                        Console.Write("Data : ");
                        string newData = Console.ReadLine();
                        if (String.IsNullOrEmpty(newData))
                        {
                            break;
                        }
                        byte[] newDataBytes = Encoding.UTF8.GetBytes(newData);
                        cache.AddReplace(key, newDataBytes);
                        break;

                    case "load":
                        DateTime startTime = DateTime.Now;

                        for (int i = 0; i < _LoadCount; i++)
                        {
                            string loadKey = Guid.NewGuid().ToString();
                            Console.Write("Adding entry " + i + " of " + _LoadCount + "                      \r");
                            cache.AddReplace(loadKey, data);
                        }

                        Console.WriteLine(
                            "Loaded " + _LoadCount +
                            " records in " + TotalTimeFrom(startTime) + ": " +
                            DecimalToString(TotalMsFrom(startTime) / _LoadCount) + "ms per entry");
                        break;

                    case "last_used":
                        Console.WriteLine("Last used key: " + cache.LastUsed());
                        break;

                    case "first_used":
                        Console.WriteLine("First used key: " + cache.FirstUsed());
                        break;

                    case "oldest":
                        Console.WriteLine("Oldest key: " + cache.Oldest());
                        break;

                    case "newest":
                        Console.WriteLine("Newest key: " + cache.Newest());
                        break;

                    case "count":
                        Console.WriteLine("Cache count: " + cache.Count());
                        break;

                    case "clear":
                        cache.Clear();
                        Console.WriteLine("Cache cleared");
                        break;

                    case "dispose":
                        cache.Dispose();
                        break;

                    case "q":
                    case "quit":
                        runForever = false;
                        break;

                    default:
                        continue;
                    }
                }

                Console.WriteLine("Goodbye!");
                return;
            }
            catch (Exception e)
            {
                PrintException(e);
            }
            finally
            {
                Console.WriteLine("");
                Console.Write("Press ENTER to exit.");
                Console.ReadLine();
            }
        }