Exemplo n.º 1
0
 /// <summary>
 /// 启动redis
 /// </summary>
 public void Start(LuceneTools luceneTools)
 {
     //开启一个异步线程
     Task.Run(() =>
     {
         while (true)
         {
             var commodityList = _rListService.Get(listKey);
             if (commodityList.Count > 0)
             {
                 Console.WriteLine($"当前redis 对象的包含的list集合的数量是{commodityList.Count}");
                 var strList = _rListService.BlockingDequeueItemFromList(listKey, TimeSpan.FromHours(1));
                 //1.将值转换为List集合
                 GM_Commodity entity = Common.JsonHelper.ToObject <GM_Commodity>(strList);
                 //2.检查索引中是否有该对象,如果有那么就更新索引
                 luceneTools.UpdateIndex(entity);
                 ////3:更新成功再次查询数据
                 //luceneTools.QueryList("Title:asus", new Page() { Sort = "Price" }, "[100,200]");
             }
             else
             {
                 Thread.Sleep(1000);
             }
         }
     });
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                #region 1.0 爬虫
                //1:初始化容器和服务
                IOCFactory.InitContainer();
                ICategoryService  categoryService  = IOCFactory.Resolve <ICategoryService>();
                ICommodityService commodityService = IOCFactory.Resolve <ICommodityService>();
                //这个是爬虫的demo
                //CrawlerDemo(categoryService, commodityService);
                #endregion

                #region 2.0 lucene.net
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine($"*****************lucene.net demo*****************");
                LuceneTools luceneTools = new LuceneTools(commodityService);
                //luceneTools.InitLucene();
                luceneTools.QueryList("Title:asus", new Page()
                {
                    Sort = "Price"
                }, "[100,200]");
                #endregion

                #region 3.0 redis 异步队列
                //3.1 启动异步队列
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine($"*****************启动一个异步队列*****************");
                RedisTools.Current.Start(luceneTools);
                //3.2 添加一个实体
                Console.WriteLine($"1.0 添加一个实体到数据");
                GM_Commodity commodity = new GM_Commodity()
                {
                    Title     = "华硕(asus),测试数据",
                    ImageUrl  = "",
                    Price     = 150,
                    ProductId = "11",
                    SkuId     = "11",
                    Url       = "http://www.baidu.com"
                };
                int id = commodityService.Insert(commodity);
                Console.WriteLine($"2.0 添加一个实体到数据库,得到的Id={id}");
                commodity.Id = id;
                //将该数据添加到异步队列中
                Console.WriteLine($"3.0 更新实体到索引中");
                RedisTools.Current.Add(commodity);
                //停留2秒中等待redis中的数据更新到index 中
                Thread.Sleep(2000);
                Console.WriteLine($"4.0 在查询包含 asus的索引");
                luceneTools.QueryList("Title:asus", new Page()
                {
                    Sort = "Price"
                }, "[100,200]");
                #endregion
            }
            catch (Exception ex)
            {
                //如果其中一个线程出现异常,那么就终止所有其他线程
                cts?.Cancel();
                ctsPrice?.Cancel();
                //出现异常中断其他运行的线程
                Console.WriteLine("页面抓取出现异常:{0}", ex.Message);
            }

            Console.ReadLine();
        }