示例#1
0
        public void Test_Save(string name, int age, int gender)
        {
            var p1 = new Person()
            {
                Name = name, Age = age, Gender = gender
            };
            var cache = new LiteCache <Person> (99999, 99999, "mycache");
            var item  = cache.AddValue(p1);

            Assert.True(!string.IsNullOrEmpty(item.Item1) && item.Item2);
        }
示例#2
0
        public void Test_BatchSave()
        {
            var cache    = new LiteCache <Person> (99999, 99999, "mycache1");
            var dataList = new List <Person> ();

            for (int i = 0; i < 100; i++)
            {
                var p = new Person()
                {
                    Name = "testName", Age = i, Gender = i
                };
                dataList.Insert(0, p);
            }
            var item = cache.AddValue(dataList);

            Assert.True(item.Item1 == 100 && item.Item2);
        }
示例#3
0
        public void Test_changeData()
        {
            var cache = new LiteCache <Person> (99999, 99999, "mycache3");

            Assert.True(cache.Count == 0);
            var data1 = cache.AddValue(new Person()
            {
                Name = "zj1", Age = 30, Gender = 0
            });
            var data2 = cache.AddValue(new Person()
            {
                Name = "zj2", Age = 30, Gender = 0
            });
            var data3 = cache.AddValue(new Person()
            {
                Name = "zj3", Age = 30, Gender = 0
            });

            Assert.True("zj1" == cache.GlobalDictionary[data1.Item1].Name);
            Assert.True("zj2" == cache.GlobalDictionary[data2.Item1].Name);
            Assert.True("zj3" == cache.GlobalDictionary[data3.Item1].Name);

            var cache_copy = new LiteCache <Person> (99999, 99999, "mycache3");

            Assert.True(cache_copy.Count == 3);

            Assert.True(cache_copy.DeleteValue(data1.Item1));

            Assert.True(cache_copy.Count == 2);

            Assert.True(cache_copy.UpdateValue(data2.Item1, new Person()
            {
                Name = "zj new", Age = 30, Gender = 0
            }));

            Assert.True(cache_copy.GetValue(data2.Item1).Item1.Name == "zj new");

            Assert.True(cache_copy.Flush());
        }
示例#4
0
        public async Task Test_dataClear(int liveTime, int checkTime, int delayTime)
        {
            var cache = new LiteCache <Person> (liveTime, checkTime, "mycache4");
            var task1 = await cache.AsynAddValue(new Person()
            {
                Name = "zj", Age = 30, Gender = 0
            });

            Assert.True(task1.Item2 && !string.IsNullOrEmpty(task1.Item1));
            await Task.Delay(delayTime * 1000);

            var task2 = await cache.AsynGetValue(task1.Item1);

            if (delayTime > liveTime)
            {
                Assert.True(!task2.Item2 && task2.Item1 == null);
            }
            else
            {
                Assert.True(task2.Item2 && task2.Item1.Name == "zj");
            }
            cache.Flush();
        }
示例#5
0
        static void Main(string[] args)
        {
            var cache = new LiteCache <Person> (999999, 999999, "mycache");
            // try {
            //     foreach (var item in cache.GlobalDictionary) {
            //         Console.WriteLine ($"location cache key={item.Key},name={item.Value.Name}");
            //     }
            // } catch (System.Exception ex) {
            //     Console.WriteLine ("load location data error,info=" + ex.Message);
            // }

            // while (true) {

            //     Console.WriteLine ("Please enter the keyboard number and press enter to confirm");
            //     Console.WriteLine ("1:creat,2:query,3:Count,4:Flush");
            //     var input_number = Console.ReadLine ();
            //     switch (input_number) {
            //         case "1":
            //             Console.WriteLine ("you creat data,For example:zj,30,0");
            //             var input_creat = Console.ReadLine ();
            //             var ary = input_creat.Split (',');
            //             var newPerson = new Person () { Name = ary[0], Age = int.Parse (ary[1]), Gender = int.Parse (ary[2]) };
            //             var set_result = cache.AddValue (newPerson);
            //             if (set_result.Item2) {
            //                 Console.WriteLine ("creat data complete,guid=" + set_result.Item1);
            //             } else {
            //                 Console.WriteLine ("creat data error");
            //             }
            //             break;
            //         case "2":
            //             Console.WriteLine ("you query data,input guid key");
            //             var input_query = Console.ReadLine ();
            //             var result = cache.GetValue (input_query);
            //             if (result.Item2) {
            //                 Console.WriteLine ($"data query complete,name:{result.Item1.Name},age:{result.Item1.Age}");
            //             } else {
            //                 Console.WriteLine ("not data for query");
            //             }
            //             break;
            //         case "3":
            //             Console.WriteLine ("data count=" + cache.Count);
            //             break;
            //         case "4":
            //             if (cache.Flush ()) {
            //                 Console.WriteLine ("flush data complete");
            //             }
            //             break;
            //     }
            // }

            Task t1 = Task.Run(() => {
                var list = new List <Person>();
                for (int i = 0; i < 999999; i++)
                {
                    list.Add(new Person()
                    {
                        Name = "zj1", Age = i, Gender = 0
                    });
                }
                cache.AddValue(list);
            });

            Task t2 = Task.Run(() => {
                // for (int i = 0; i < 99999; i++) {
                //     cache.AddValue (new Person () { Name = "zj2", Age = i, Gender = 0 });
                // }
            });

            var sw = Stopwatch.StartNew();

            Task[] tasks = new Task[] { t1, t2 };
            Stopwatch.StartNew();
            Task.WaitAll(tasks);
            Console.WriteLine("count=" + cache.Count);
            Console.WriteLine(sw.Elapsed);
        }