Exemple #1
0
        public static void PutMqttData(RedisHashService service, string cachekey, string state)
        {
            //如果状态已经是当前要操作的状态
            string state0 = service.GetValueFromHash("DeviceStatus", cachekey);

            //不一致的状态再去修改
            if (state0 != state)
            {
                service.SetEntryInHash("DeviceStatus", cachekey, state);
                string openUid = service.GetValueFromHash("DuerOSOpenUid_Device", cachekey);
                //存在百度音响openUid的同步到百度音响平台
                if (!string.IsNullOrEmpty(openUid))
                {
                    using (RedisListService service2 = new RedisListService())
                    {
                        service2.Publish("YunZigStateChangeQueue", cachekey);
                        log.Debug($"《《《《《《《《《《《《《《《《发布YunZigStateChangeQueue状态同步消息 {cachekey}");
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 private RedisTools()
 {
     //在构造函数中初始化list 服务,保证服务线程实例唯一
     _rListService = new RedisListService();
 }
Exemple #3
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();
        }
        public static void Show()
        {
            Console.WriteLine("*****************************************");
            {
                ////key-value  get/set  Append GetAndSetValue Incr DecrBy
                ////为啥花里胡哨? 不仅少一次命令,这个redis提供的操作,可以原子性操作
                ////不可分割,一定成功/失败,不会中间状态
                ////GetAndSetValue 如果两步,高并发时,可能别的请求改了
                ////数据库:秒杀的时候,10件商品,100个人想买,假定大家一瞬间都来了,
                ////A 查询还有没有--有---1更新
                ////B 查询还有没有--有---1更新
                ////C 查询还有没有--有---1更新
                ////可能会卖出12  12甚至20件商品
                ////程序中处理,lock  单线程(多服务器呢)
                ////Redis
                //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));
                //}
            }

            Console.WriteLine("*****************************************");
            {
                ////key-value局限性:一个对象序列化保存--读出来反序列化--修改---序列化保存
                ////空间浪费问题:默认空间  即使value就是一个1  也要那么多空间
                ////缓存用户id-name

                ////hashtable:一个解决序列化的问题
                ////可以更节约内存空间,一个hash保存多个key-value  IdNameMapping----id-name
                //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"));
                //}
            }
            Console.WriteLine("*****************************************");
            {
                ////Set居然有顺序了,,疑难问题

                ////Set:key-List<value>  去重  用户ip记录; 关键词;
                //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");
                //        //共同好友   共同关注
                //    }
                //}
            }
            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);
                //    var result3 = service.GetAllWithScoresFromSortedSet("Sort");

                //    //交叉并
                //}
            }

            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", "程序错误");

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

                    //1 内存操作,分页   最新微博  最新评论  最近登录用户
                    //个人blog  第一页为了速度,直接读redis;还需要继续读,说明真的感兴趣,那得去把数据库读一下
                    //80%的请求  都在20%数据上
                    //一个list可以放入2的32次方  20亿条数据



                    ////栈
                    //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 result1 = service.Get("article");
                    //}



                    //// 队列:生产者消费者模型
                    //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 result1 = service.Get("article");
                    //}
                    ////分布式缓存,多服务器都可以访问到,多个生产者,多个消费者,任何产品只被消费一次
                }

                #region 生产者消费者
                using (RedisListService service = new RedisListService())
                {
                    //List<string> stringList = new List<string>();
                    //for (int i = 0; i < 10; i++)
                    //{
                    //    stringList.Add(string.Format($"放入任务{i}"));
                    //}

                    //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");
                    //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");
                }
                #endregion
            }
        }
        public static void Show()
        {
            Student student_1 = new Student()
            {
                Id   = 11,
                Name = "Eleven"
            };
            Student student_2 = new Student()
            {
                Id     = 12,
                Name   = "Twelve",
                Remark = "123423245"
            };


            Console.WriteLine("*****************************************");
            {
                //key  value 都是string   假如是个对象呢?序列化一下
                //假如要修改某一个属性的值   读--反序列化--修改--序列化 memcached
                using (RedisStringService service = new RedisStringService())
                {
                    service.KeyFulsh();
                    service.StringSet("RedisStringService_key1", "RedisStringService_value1");
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    Console.WriteLine(service.StringGetSet("RedisStringService_key1", "RedisStringService_value2"));
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));

                    service.StringAppend("RedisStringService_key1", "Append");
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    service.StringSet("RedisStringService_key1", "RedisStringService_value", new TimeSpan(0, 0, 0, 5));
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    Thread.Sleep(5000);
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                }
            }

            //保存 查询对象:
            //Student_2_id  12  Student_2_Name Twelve
            // 序列化后保存一个对象没问题,
            //查询--反序列化--修改--序列化--保存

            Console.WriteLine("*****************************************");
            {
                using (RedisHashService service = new RedisHashService())
                {
                    //service.KeyFulsh();
                    //service.SetEntryInHash("lisi", "id", "15");

                    //service.SetEntryInHash("zhangsan", "id", "13");
                    //service.SetEntryInHash("zhangsan", "Name", "Thirteen");
                    //service.SetEntryInHashIfNotExists("zhangsan", "Remark", "1234567");
                    //var value13 = service.GetHashValues("zhangsan");
                    //var key13 = service.GetHashKeys("zhangsan");

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

                    //service.SetEntryInHash("zhangsan", "id", "14");//同一条数据,覆盖
                    //service.SetEntryInHash("zhangsan", "Name", "Fourteen");
                    //service.SetEntryInHashIfNotExists("zhangsan", "Remark", "2345678");//同一条数据,不覆盖
                    //service.SetEntryInHashIfNotExists("zhangsan", "Other", "234543");//没有数据就添加
                    //service.SetEntryInHashIfNotExists("zhangsan", "OtherField", "1235665");


                    //var value14 = service.GetHashValues("zhangsan");
                    //service.RemoveEntryFromHash("zhangsan", "Remark");
                    //service.SetEntryInHashIfNotExists("zhangsan", "Remark", "2345678");
                    //value14 = service.GetHashValues("zhangsan");

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

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

                //    service.Add("Internal", "111");
                //    service.Add("Internal", "112");
                //    service.Add("Internal", "117");
                //    service.Add("Internal", "119");
                //    service.Add("Internal", "118");
                //    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");//交
                //    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("*****************************************");
            {
                //using (RedisZSetService service = new RedisZSetService())
                //{
                //     service.KeyFulsh();
                //    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);
                //    service.AddItemToSortedSet("user", "Eleven3", 5);
                //    service.AddItemToSortedSet("user", "Eleven4", 3);
                //    service.AddItemToSortedSet("user", "1Eleven2", 4);


                //    var list = service.GetAll("score");
                //    var listDesc = service.GetAllDesc("score");

                //    var user = service.GetAll("user");
                //    var userDesc = service.GetAllDesc("user");
                //}
            }
            Console.WriteLine("*****************************************");
            {
                using (RedisListService service = new RedisListService())
                {
                    service.KeyFulsh();

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

                    service.ListLeftPush("test", "这是一个学生1");
                    service.ListLeftPush("test", "这是一个学生2");
                    service.ListLeftPush("test", "这是一个学生3");
                    service.ListLeftPush("test", "这是一个学生4");
                    service.ListLeftPush("test", "这是一个学生5");
                    service.ListLeftPush("test", "这是一个学生6");

                    service.ListLeftPush("task", stringList);

                    Console.WriteLine(service.ListLength("test"));
                    Console.WriteLine(service.ListLength("task"));
                    var list = service.ListRange <string>("test");

                    Action act = new Action(() =>
                    {
                        while (true)
                        {
                            Console.WriteLine("************请输入数据**************");
                            string testTask = Console.ReadLine();
                            service.ListLeftPush("test", testTask);
                        }
                    });
                    act.EndInvoke(act.BeginInvoke(null, null));
                }
            }
        }
Exemple #6
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
            }
        }