public static void Show()
        {
            //没有队列
            //List<string> intList = new List<string>();
            //for (int i = 0; i < 1000; i++)
            //{
            //    intList.Add($"任务{i}");
            //}
            //#region
            //{
            //    Stopwatch stopwatch = new Stopwatch();
            //    stopwatch.Start();
            //    ParallelOptions parallelOptions = new ParallelOptions();
            //    parallelOptions.MaxDegreeOfParallelism = 10;
            //    Parallel.ForEach(intList, parallelOptions, s =>
            //    {
            //        Thread.Sleep(100);
            //    });
            //    stopwatch.Stop();
            //    Console.WriteLine($"一下子来了1000个任务,处理速度必变,时间得需要{stopwatch.ElapsedMilliseconds}ms");
            //    Console.WriteLine("在这期间,任务又来了好几万。任务累计。根本处理不过来");
            //    //处理能力有限,任务一下子特别多,会出现任务的累计
            //}
            //#endregion

            #region
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string tag  = path.Split('/', '\\').Last(s => !string.IsNullOrEmpty(s));
            Console.WriteLine($"这里是 {tag} 启动了。。");
            Task.Run(() =>
            {
                using (RedisListService service = new RedisListService())
                {
                    int k = 1;
                    while (true)
                    {
                        List <string> stringList = new List <string>();
                        for (int i = 0; i < 10; i++)
                        {
                            stringList.Add(string.Format($"{tag}放入任务{i}"));
                        }
                        service.Add("task", stringList);

                        service.RPush("test", $"{tag} {k}这是一个学生RPush1");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush2");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush3");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush4");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush5");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush6");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush7");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush8");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush9");
                        service.RPush("test", $"{tag} {k}这是一个学生RPush10");
                        Thread.Sleep(1000);
                        k++;
                    }
                }
            });
            #endregion
        }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                Student student_1 = new Student()
                {
                    Id   = 11,
                    Name = "Eleven"
                };
                Student student_2 = new Student()
                {
                    Id     = 12,
                    Name   = "Twelve",
                    Remark = "123423245"
                };

                //Student_2_id  12  Student_2_Name  Twelve
                //需要修改对象
                //查询--反序列化--修改--序列化保存
                Console.WriteLine("*****************************************");
                {
                    //RedisStringService service = new RedisStringService();
                    //service.FlushAll();
                    //service.Set("RedisStringService_key1", "RedisStringService_value1");
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //Console.WriteLine(service.GetAndSetValue("RedisStringService_key1", "RedisStringService_value2"));
                    //Console.WriteLine(service.Get("RedisStringService_key1"));

                    //service.Append("RedisStringService_key1", "Append");
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //service.Set("RedisStringService_key1", "RedisStringService_value", DateTime.Now.AddSeconds(5));
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                    //Thread.Sleep(5000);
                    //Console.WriteLine(service.Get("RedisStringService_key1"));
                }

                Console.WriteLine("*****************************************");
                {
                    //RedisHashService service = new RedisHashService();
                    //service.FlushAll();
                    //service.SetEntryInHash("Student", "id", "13");
                    //service.SetEntryInHash("Student", "Name", "Thirteen");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "1234567");
                    //var listResult = service.GetHashValues("Student");
                    //listResult = service.GetHashKeys("Student");

                    //var dicList = service.GetAllEntriesFromHash("Student");

                    //service.SetEntryInHash("Student", "id", "14");//同一条数据,覆盖
                    //service.SetEntryInHash("Student", "Name", "Fourteen");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678");//同一条数据,不覆盖

                    //listResult = service.GetHashValues("Student");
                    //service.RemoveEntryFromHash("Student", "Remark");
                    //service.SetEntryInHashIfNotExists("Student", "Remark", "2345678");
                    //listResult = service.GetHashValues("Student");

                    //service.StoreAsHash<Student>(student_1);
                    //Student student1 = service.GetFromHash<Student>(11);
                    //service.StoreAsHash<Student>(student_2);
                    //Student student2 = service.GetFromHash<Student>(12);
                }
                Console.WriteLine("*****************************************");
                {
                    ////key--values
                    //RedisSetService service = new RedisSetService();
                    //service.FlushAll();
                    //service.Add("Advanced", "111");
                    //service.Add("Advanced", "112");
                    //service.Add("Advanced", "113");
                    //service.Add("Advanced", "114");
                    //service.Add("Advanced", "115");
                    //service.Add("Advanced", "111");

                    //service.Add("Begin", "111");
                    //service.Add("Begin", "112");
                    //service.Add("Begin", "113");
                    //service.Add("Begin", "116");
                    //service.Add("Begin", "117");
                    //service.Add("Begin", "111");

                    //service.Add("Internal", "111");
                    //service.Add("Internal", "112");
                    //service.Add("Internal", "117");
                    //service.Add("Internal", "118");
                    //service.Add("Internal", "119");
                    //service.Add("Internal", "111");

                    //var result = service.GetAllItemsFromSet("Advanced");
                    //var result2 = service.GetRandomItemFromSet("Advanced");
                    //result = service.GetAllItemsFromSet("Begin");
                    //result2 = service.GetRandomItemFromSet("Begin");

                    //var result3 = service.GetIntersectFromSets("Advanced", "Begin", "Internal");//交
                    //result3 = service.GetDifferencesFromSet("Advanced", "Begin", "Internal");//差
                    //result3 = service.GetUnionFromSets("Advanced", "Begin", "Internal");//并

                    //service.RemoveItemFromSet("Advanced", "111");
                    //result = service.GetAllItemsFromSet("Advanced");
                    //service.RandomRemoveItemFromSet("Advanced");
                    //result = service.GetAllItemsFromSet("Advanced");
                }
                Console.WriteLine("*****************************************");
                {
                    //RedisZSetService service = new RedisZSetService();
                    //service.FlushAll();
                    //service.Add("score", "111");
                    //service.Add("score", "112");
                    //service.Add("score", "113");
                    //service.Add("score", "114");
                    //service.Add("score", "115");
                    //service.Add("score", "111");

                    //service.AddItemToSortedSet("user", "Eleven1", 1);

                    //service.AddItemToSortedSet("user", "Eleven2", 2);

                    //var list = service.GetAll("score");
                    //list = service.GetAllDesc("score");
                }
                Console.WriteLine("*****************************************");
                {
                    RedisListService service = new RedisListService();
                    service.FlushAll();

                    List <string> stringList = new List <string>();
                    for (int i = 0; i < 10; i++)
                    {
                        stringList.Add(string.Format("放入任务{0}", i));
                    }

                    service.LPush("test", "这是一个学生1");
                    service.LPush("test", "这是一个学生2");
                    service.LPush("test", "这是一个学生3");
                    service.LPush("test", "这是一个学生4");
                    service.Add("task", stringList);

                    //Console.WriteLine(service.Count("test"));
                    //Console.WriteLine(service.Count("task"));
                    //var list = service.Get("test");
                    //list = service.Get("task", 2, 4);


                    //new Action(() =>
                    //{
                    //    while (true)
                    //    {
                    //        var result = service.BlockingPopItemFromLists(new string[] { "test", "task" }, TimeSpan.FromHours(3));
                    //        Console.WriteLine("这里是队列获取的消息 {0} {1}", result.Id, result.Item);
                    //    }
                    //}).BeginInvoke(null, null);

                    Action act = new Action(() =>
                    {
                        while (true)
                        {
                            Console.WriteLine("************请输入数据**************");
                            string testTask = Console.ReadLine();
                            service.LPush("test", testTask);
                        }
                    });
                    act.EndInvoke(act.BeginInvoke(null, null));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
Exemple #3
0
        public static void Show()
        {
            Student student_1 = new Student()
            {
                Id   = 11,
                Name = "Eleven"
            };
            Student student_2 = new Student()
            {
                Id     = 12,
                Name   = "Twelve",
                Remark = "123423245"
            };

            // string key-value
            Console.WriteLine("*****************************************");
            {
                //using (RedisStringService service = new RedisStringService())
                //{
                //    service.Set<string>("student1", "梦的翅膀");
                //    Console.WriteLine(service.Get("student1"));

                //    service.Append("student1", "20180802");
                //    Console.WriteLine(service.Get("student1"));

                //    Console.WriteLine(service.GetAndSetValue("student1", "程序错误"));
                //    Console.WriteLine(service.Get("student1"));

                //    service.Set<string>("student2", "王", DateTime.Now.AddSeconds(5));
                //    Thread.Sleep(5100);
                //    Console.WriteLine(service.Get("student2"));

                //    service.Set<int>("Age", 32);
                //    Console.WriteLine(service.Incr("Age"));
                //    Console.WriteLine(service.IncrBy("Age", 3));
                //    Console.WriteLine(service.Decr("Age"));
                //    Console.WriteLine(service.DecrBy("Age", 3));
                //}
            }


            // hash key-keys[]-value
            Console.WriteLine("*****************************************");
            {
                //using (RedisHashService service = new RedisHashService())
                //{
                //    service.SetEntryInHash("student", "id", "123456");
                //    service.SetEntryInHash("student", "name", "张xx");
                //    service.SetEntryInHash("student", "remark", "高级班的学员");

                //    var keys = service.GetHashKeys("student");
                //    var values = service.GetHashValues("student");
                //    var keyValues = service.GetAllEntriesFromHash("student");
                //    Console.WriteLine(service.GetValueFromHash("student", "id"));

                //    service.SetEntryInHashIfNotExists("student", "name", "太子爷");
                //    service.SetEntryInHashIfNotExists("student", "description", "高级班的学员2");

                //    Console.WriteLine(service.GetValueFromHash("student", "name"));
                //    Console.WriteLine(service.GetValueFromHash("student", "description"));
                //    service.RemoveEntryFromHash("student", "description");
                //    Console.WriteLine(service.GetValueFromHash("student", "description"));
                //}
            }

            // set 无序,不重复
            Console.WriteLine("*****************************************");
            {
                using (RedisSetService service = new RedisSetService())
                {
                    //service.FlushAll();//清理全部数据

                    //service.Add("advanced", "111");
                    //service.Add("advanced", "112");
                    //service.Add("advanced", "114");
                    //service.Add("advanced", "114");
                    //service.Add("advanced", "115");
                    //service.Add("advanced", "115");
                    //service.Add("advanced", "113");

                    //var result = service.GetAllItemsFromSet("advanced");

                    //var random = service.GetRandomItemFromSet("advanced");//随机获取
                    //service.GetCount("advanced");//独立的ip数
                    //service.RemoveItemFromSet("advanced", "114");

                    //{
                    //    service.Add("begin", "111");
                    //    service.Add("begin", "112");
                    //    service.Add("begin", "115");

                    //    service.Add("end", "111");
                    //    service.Add("end", "114");
                    //    service.Add("end", "113");

                    //    var result1 = service.GetIntersectFromSets("begin", "end");
                    //    var result2 = service.GetDifferencesFromSet("begin", "end");
                    //    var result3 = service.GetUnionFromSets("begin", "end");
                    //    //共同好友   共同关注
                    //}
                }
            }
            //Zset 可设置分数
            Console.WriteLine("*****************************************");
            {
                //using (RedisZSetService service = new RedisZSetService())
                //{
                //    service.FlushAll();//清理全部数据

                //    service.Add("advanced", "1");
                //    service.Add("advanced", "2");
                //    service.Add("advanced", "5");
                //    service.Add("advanced", "4");
                //    service.Add("advanced", "7");
                //    service.Add("advanced", "5");
                //    service.Add("advanced", "9");

                //    var result1 = service.GetAll("advanced");
                //    var result2 = service.GetAllDesc("advanced");

                //    service.AddItemToSortedSet("Sort", "BY", 123234);
                //    service.AddItemToSortedSet("Sort", "走自己的路", 123);
                //    service.AddItemToSortedSet("Sort", "redboy", 45);
                //    service.AddItemToSortedSet("Sort", "大蛤蟆", 7567);
                //    service.AddItemToSortedSet("Sort", "路人甲", 9879);
                //    service.AddRangeToSortedSet("Sort", new List<string>() { "123", "花生", "加菲猫" }, 3232);
                //    var result3 = service.GetAllWithScoresFromSortedSet("Sort");

                //    //交叉并
                //}
            }

            // list
            Console.WriteLine("*****************************************");
            {
                //using (RedisListService service = new RedisListService())
                //{
                //    service.FlushAll();

                //    service.Add("article", "eleven1234");
                //    service.Add("article", "kevin");
                //    service.Add("article", "大叔");
                //    service.Add("article", "C卡");
                //    service.Add("article", "触不到的线");
                //    service.Add("article", "程序错误");
                //    service.RPush("article", "eleven1234");
                //    service.RPush("article", "kevin");
                //    service.RPush("article", "大叔");
                //    service.RPush("article", "C卡");
                //    service.RPush("article", "触不到的线");
                //    service.RPush("article", "程序错误");

                //    var result1 = service.Get("article");
                //    var result2 = service.Get("article", 0, 3);
                //    //可以按照添加顺序自动排序;而且可以分页获取

                //    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                //    //栈
                //    service.FlushAll();
                //    service.Add("article", "eleven1234");
                //    service.Add("article", "kevin");
                //    service.Add("article", "大叔");
                //    service.Add("article", "C卡");
                //    service.Add("article", "触不到的线");
                //    service.Add("article", "程序错误");

                //    for (int i = 0; i < 5; i++)
                //    {
                //        Console.WriteLine(service.PopItemFromList("article"));
                //        var result3 = service.Get("article");
                //    }
                //    Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                //    // 队列:生产者消费者模型
                //    service.FlushAll();
                //    service.RPush("article", "eleven1234");
                //    service.RPush("article", "kevin");
                //    service.RPush("article", "大叔");
                //    service.RPush("article", "C卡");
                //    service.RPush("article", "触不到的线");
                //    service.RPush("article", "程序错误");

                //    for (int i = 0; i < 5; i++)
                //    {
                //        Console.WriteLine(service.PopItemFromList("article"));
                //        var result4 = service.Get("article");
                //    }
                //    //分布式缓存,多服务器都可以访问到,多个生产者,多个消费者,任何产品只被消费一次
                //}

                #region 生产者消费者
                using (RedisListService service = new RedisListService())
                {
                    service.Add("test", "这是一个学生Add1");
                    service.Add("test", "这是一个学生Add2");
                    service.Add("test", "这是一个学生Add3");

                    service.LPush("test", "这是一个学生LPush1");
                    service.LPush("test", "这是一个学生LPush2");
                    service.LPush("test", "这是一个学生LPush3");
                    service.LPush("test", "这是一个学生LPush4");
                    service.LPush("test", "这是一个学生LPush5");
                    service.LPush("test", "这是一个学生LPush6");

                    service.RPush("test", "这是一个学生RPush1");
                    service.RPush("test", "这是一个学生RPush2");
                    service.RPush("test", "这是一个学生RPush3");
                    service.RPush("test", "这是一个学生RPush4");
                    service.RPush("test", "这是一个学生RPush5");
                    service.RPush("test", "这是一个学生RPush6");

                    List <string> stringList = new List <string>();
                    for (int i = 0; i < 10; i++)
                    {
                        stringList.Add(string.Format($"放入任务{i}"));
                    }
                    service.Add("task", stringList);

                    Console.WriteLine(service.Count("test"));
                    Console.WriteLine(service.Count("task"));
                    var list = service.Get("test");
                    list = service.Get("task", 2, 4);

                    Action act = new Action(() =>
                    {
                        while (true)
                        {
                            Console.WriteLine("************请输入数据**************");
                            string testTask = Console.ReadLine();
                            service.LPush("test", testTask);
                        }
                    });
                    act.EndInvoke(act.BeginInvoke(null, null));
                }
                #endregion

                #region 发布订阅:观察者,一个数据源,多个接受者,只要订阅了就可以收到的,能被多个数据源共享
                Task.Run(() =>
                {
                    using (RedisListService service = new RedisListService())
                    {
                        service.Subscribe("Eleven", (c, message, iRedisSubscription) =>
                        {
                            Console.WriteLine($"注册{1}{c}:{message},Dosomething else");
                            if (message.Equals("exit"))
                            {
                                iRedisSubscription.UnSubscribeFromChannels("Eleven");
                            }
                        });//blocking
                    }
                });
                Task.Run(() =>
                {
                    using (RedisListService service = new RedisListService())
                    {
                        service.Subscribe("Eleven", (c, message, iRedisSubscription) =>
                        {
                            Console.WriteLine($"注册{2}{c}:{message},Dosomething else");
                            if (message.Equals("exit"))
                            {
                                iRedisSubscription.UnSubscribeFromChannels("Eleven");
                            }
                        });//blocking
                    }
                });
                Task.Run(() =>
                {
                    using (RedisListService service = new RedisListService())
                    {
                        service.Subscribe("Twelve", (c, message, iRedisSubscription) =>
                        {
                            Console.WriteLine($"注册{3}{c}:{message},Dosomething else");
                            if (message.Equals("exit"))
                            {
                                iRedisSubscription.UnSubscribeFromChannels("Twelve");
                            }
                        });//blocking
                    }
                });
                using (RedisListService service = new RedisListService())
                {
                    Thread.Sleep(1000);

                    service.Publish("Eleven", "Eleven123");
                    service.Publish("Eleven", "Eleven234");
                    service.Publish("Eleven", "Eleven345");
                    service.Publish("Eleven", "Eleven456");

                    service.Publish("Twelve", "Twelve123");
                    service.Publish("Twelve", "Twelve234");
                    service.Publish("Twelve", "Twelve345");
                    service.Publish("Twelve", "Twelve456");
                    Console.WriteLine("**********************************************");

                    service.Publish("Eleven", "exit");
                    service.Publish("Eleven", "123Eleven");
                    service.Publish("Eleven", "234Eleven");
                    service.Publish("Eleven", "345Eleven");
                    service.Publish("Eleven", "456Eleven");

                    service.Publish("Twelve", "exit");
                    service.Publish("Twelve", "123Twelve");
                    service.Publish("Twelve", "234Twelve");
                    service.Publish("Twelve", "345Twelve");
                    service.Publish("Twelve", "456Twelve");
                }
                //观察者模式:微信订阅号---群聊天---数据同步--
                //MSMQ---RabbitMQ---ZeroMQ---RedisList:学习成本 技术成本
                #endregion
            }
        }