public async Task <ActionResult <int> > AddCostAttribute3()
        {
            const int amount = 200000;

            CalculatedElementData[] calculatedElementDatas = TestClass.FillElementList(amount).ToArray();
            Log.Information($"Add costattribute list, begin");
            var result = await _redisListService.LPush <CalculatedElementData>("list_key", calculatedElementDatas);

            Log.Information($"Add costattribute list: {result}");
            return(Ok());
        }
Beispiel #2
0
 /// <summary>
 /// 将对象添加到list集合中
 /// </summary>
 /// <param name="entity"></param>
 public void Add(GM_Commodity entity)
 {
     //一个一个对象放入队列中
     _rListService.LPush(listKey, Common.JsonHelper.ToJSON <GM_Commodity>(entity));
 }
Beispiel #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();
        }
Beispiel #4
0
        private List <Commodity> GetCommodityList(Category category, string url)
        {
            string           html          = HttpHelper.DownloadUrl(url);
            List <Commodity> commodityList = new List <Commodity>();


            try
            {
                if (string.IsNullOrEmpty(html))
                {
                    return(commodityList);
                }
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                string             liPath       = "//*[@id='plist']/ul/li";
                HtmlNodeCollection noneNodeList = doc.DocumentNode.SelectNodes(liPath);
                if (noneNodeList == null || noneNodeList.Count == 0)
                {
                    warnRepository.SaveWarn(category, string.Format("GetCommodityList商品数据为空,Name={0} Level={1} category.Url={2} url={3}", category.Name, category.CategoryLevel, category.Url, url));
                    return(commodityList);
                }
                foreach (var node in noneNodeList)
                {
                    HtmlDocument docChild = new HtmlDocument();
                    docChild.LoadHtml(node.OuterHtml);

                    Commodity commodity = new Commodity()
                    {
                        CategoryId = category.Id
                    };

                    string   urlPath = "//*[@class='p-name']/a";
                    HtmlNode urlNode = docChild.DocumentNode.SelectSingleNode(urlPath);
                    if (urlNode == null)
                    {
                        continue;
                    }
                    commodity.Url = urlNode.Attributes["href"].Value;
                    if (!commodity.Url.StartsWith("http:"))
                    {
                        commodity.Url = "http:" + commodity.Url;
                    }

                    string sId = Path.GetFileName(commodity.Url).Replace(".html", "");
                    commodity.ProductId = long.Parse(sId);

                    //*[@id="plist"]/ul/li[1]/div/div[3]/a/em
                    string   titlePath = "//*[@class='p-name']/a/em";
                    HtmlNode titleNode = docChild.DocumentNode.SelectSingleNode(titlePath);
                    if (titleNode == null)
                    {
                        //Log.Error(titlePath);
                        continue;
                    }
                    commodity.Title = titleNode.InnerText;

                    string   iamgePath = "//*[@class='p-img']/a/img";
                    HtmlNode imageNode = docChild.DocumentNode.SelectSingleNode(iamgePath);
                    if (imageNode == null)
                    {
                        continue;
                    }
                    //前后不一
                    if (imageNode.Attributes.Contains("src"))
                    {
                        commodity.ImageUrl = imageNode.Attributes["src"].Value;
                    }
                    else if (imageNode.Attributes.Contains("original"))
                    {
                        commodity.ImageUrl = imageNode.Attributes["original"].Value;
                    }
                    else if (imageNode.Attributes.Contains("data-lazy-img"))
                    {
                        commodity.ImageUrl = imageNode.Attributes["data-lazy-img"].Value;
                    }
                    else
                    {
                        continue;
                    }
                    if (!commodity.ImageUrl.StartsWith("http:"))
                    {
                        commodity.ImageUrl = "http:" + commodity.ImageUrl;
                    }

                    string   pricePath = "//*[@class='p-price']/strong/i";
                    HtmlNode priceNode = docChild.DocumentNode.SelectSingleNode(pricePath);
                    if (priceNode == null)
                    {
                        continue;
                    }
                    else
                    {
                    }


                    #region 新增Redis写入数据
                    if (service != null)
                    {
                        var commodityJson = JsonHelper.ObjToString(commodity);
                        lock (_lock)
                        {
                            service.LPush("commodity", commodityJson);
                        }
                        #endregion
                    }

                    commodityList.Add(commodity);
                }
                Console.WriteLine("{0}一共获取了{1}条数据", url, commodityList.Count);
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("GetCommodityList出现异常,url={0}", url), ex);
            }
            ///获取价格异常暂时停止调用
            // return GetCommodityPrice(category, commodityList);
            return(commodityList);
        }
Beispiel #5
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
            }
        }