Beispiel #1
0
 private static void TestSync()
 {
     try
     {
         //MemoryCacheSync cache = CreateMemoryCacheSync();
         MemoryCacheSync cache = MemoryCacheSync.Default;
         Console.WriteLine("InstanceId:{0},AppId:{1}", cache.InstanceId, cache.AppId);
         while (true)
         {
             string text = Console.ReadLine();
             if (text == null)
             {
                 continue;
             }
             string[] array = text.Split(' ');
             string   cmd   = array[0];
             string   key   = array.Length >= 2 ? array[1] : "";
             string   value = array.Length >= 3 ? array[2] : "";
             if (cmd.Equals("set"))
             {
                 cache.Set(key, value, new TimeSpan(1, 0, 0));
             }
             if (cmd.Equals("get"))
             {
                 Console.WriteLine(cache.Get(key));
             }
             if (cmd.Equals("remove"))
             {
                 cache.Remove(key);
             }
             if (cmd != "status")
             {
                 continue;
             }
             IDatabase db = cache.ConnectionMultiplexer.GetDatabase();
             IEnumerable <RedisValue> list = db.SetScan(cache.AppId);
             var redisValues = list as RedisValue[] ?? list.ToArray();
             Console.WriteLine(redisValues.Count());
             foreach (RedisValue item in redisValues)
             {
                 Console.WriteLine(item);
                 string totalQueryCountKey    = string.Format("{0}-{1}-{2}", cache.AppId, item, "TotalQueryCount");
                 string totalLocalHitCountKey = string.Format("{0}-{1}-{2}", cache.AppId, item, "TotalLocalHitCount");
                 string totalRedisHitCountKey = string.Format("{0}-{1}-{2}", cache.AppId, item, "TotalRedisHitCount");
                 Console.WriteLine("{0}:{1}", totalQueryCountKey, db.StringGet(totalQueryCountKey));
                 Console.WriteLine("{0}:{1}", totalLocalHitCountKey, db.StringGet(totalLocalHitCountKey));
                 Console.WriteLine("{0}:{1}", totalRedisHitCountKey, db.StringGet(totalRedisHitCountKey));
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
Beispiel #2
0
        private static void TestGetOrAdd()
        {
            try
            {
                Console.WriteLine("TestGetOrAdd:");
                //MemoryCacheSync cache = CreateMemoryCacheSync();
                MemoryCacheSync cache = MemoryCacheSync.Default;
                foreach (string key in AllKeys)
                {
                    cache.Remove(key);
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                List <Task> taskList = new List <Task>();
                for (int i = 0; i < ThreadNum; i++)
                {
                    string key = SearchKeys[i];
                    taskList.Add(Task.Factory.StartNew(() =>
                    {
                        object value = cache.GetOrAdd(key, () =>
                        {
                            Thread.Sleep(2000);
                            Console.WriteLine(key);
                            return(key);
                        }, new TimeSpan(0, 0, 5));
                        Console.WriteLine(value);
                    }));
                }
                Task.WaitAll(taskList.ToArray());
                watch.Stop();

                Console.WriteLine("time:" + watch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }