public JsonResult SearchByContent(string content) { int totalCount = 0; int pageId = Convert.ToInt32(QueryString("page")); int pageSize = Convert.ToInt32(QueryString("rows")); var resList = SearchIndexManager.GetInstance().SearchAll(content); var resIds = resList.Select(a => a.Id); var finalRes = UnitOfWork.GetByPage <MFDocument, DateTime>(out totalCount, pageSize, pageId, a => a.CreateTime, false, a => resIds.Contains(a.Id)); return(Json(finalRes)); }
/// <summary> /// 在第一次启动WebApp时会调用这个方法,注册各种配置和规则。 /// 相当于程序的入口 /// </summary> protected void Application_Start() { //程序一运行,就开启线程扫描队列将数据取出来写到Lucene.Net中 SearchIndexManager.GetInstance().StartThread(); //读取log4Net的配置信息 log4net.Config.XmlConfigurator.Configure(); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //在WebApi开始运行的时候,在线程池中加载一个线程操作队列 var fileLogPath = Server.MapPath("/Log/"); //新开一个线程去轮询消息队列,如果有异常消息,则取出执行。 //用CLR维护的线程池里的线程性能更好 //WaitCallback ThreadPool.QueueUserWorkItem((a) => { //必须死循环,否则执行后线程释放 while (true) { //if (MyExceptionAttribute.ExceptionQueue.Count > 0) //{ // //异常消息->出列,FIFO; // var ex = MyExceptionAttribute.ExceptionQueue.Dequeue(); // //自定义 Log // //var fileName = DateTime.Now.ToString("yyyy-M-d") + ".txt"; // //File.AppendAllText(fileLogPath + fileName, ex.ToString(), Encoding.Default); // //log4net // var logger = LogManager.GetLogger("errorMsg"); // logger.Error(ex.ToString()); //} if (MyExceptionAttribute.redisClient.GetListCount("errorMsg") > 0) { var ex = MyExceptionAttribute.redisClient.DequeueItemFromList("errorMsg"); var logger = LogManager.GetLogger("errorMsg"); logger.Error(ex); } else { //线程休息,节约CPU开销 Thread.Sleep(3000); } } }, fileLogPath); }
protected void Application_Start() { log4net.Config.XmlConfigurator.Configure(); //读取Log4Net配置信息 SearchIndexManager.GetInstance().StartThread(); //开启线程 QuartzApp.GetInstance().StartQuartz(); //开启定时任务 QuartzApp.GetInstance().StartQuartzRatings(); //开启定时任务 执行生成离线评分文件 AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //开启线程 将错误信息写到日志文件里 //设置记录错误信息的文件路径 string filePath = Server.MapPath("/Log/"); ThreadPool.QueueUserWorkItem(o => { while (true) { //判断队列中有没有异常信息 if (MyExceptionAttribute.ExceptionQueue.Count > 0) { //接收异常信息 Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue(); if (ex != null) { //创建文件并记录文件的完整路径 //string fullPath = filePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; //将错误信息追加到.txt的文件里 // File.AppendAllText(fullPath,ex.ToString(),Encoding.Default); ILog logger = LogManager.GetLogger("errorMsg");//参数可以是类型也可以是个字符串,只是个标识 logger.Error(ex); } else { Thread.Sleep(3000); } } else { Thread.Sleep(3000);//如果没有信息 就让线程休息3秒钟 防止CPU的空转 } } }, filePath); }
protected override void BeforeAdd(Dictionary <string, object> dic) { string browsFile = dic.GetValue("BrowsFile"); if (!string.IsNullOrEmpty(browsFile)) { string fileStorePath = ConfigurationManager.AppSettings["FileStorePath"]; var fileNameArr = browsFile.Split(','); foreach (var name in fileNameArr) { string tmpPath = fileStorePath + name; string physicalPath = Server.MapPath("/" + tmpPath); IndexOperation opera = IndexOperation.GetAddOpera(physicalPath); opera.Id = dic.GetValue("Id"); opera.Title = dic.GetValue("Name"); SearchIndexManager.GetInstance().AddOpreation(opera); } } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ControllerBuilder.Current.SetControllerFactory(new MFControllerFactory()); DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath); MefDependencyResolver res = new MefDependencyResolver(new DisposableWrapperCatalog(catalog, true)); DependencyResolver.SetResolver(res); MF_Project.DatabaseInitializer.Initialize(); string fileIndexPath = ConfigurationManager.AppSettings["FileIndexPath"]; string physicalPath = Server.MapPath("/" + fileIndexPath); SearchIndexManager.GetInstance().StartThread(physicalPath); }
public ActionResult TestAddQueue() { Model.Book model = new Model.Book(); model.AuthorDescription = "adsfad"; model.Author = "老赵"; model.CategoryId = 1; model.Clicks = 1; model.ContentDescription = "Asp.Net MVC高级编程"; model.EditorComment = "adfasdf"; model.ISBN = "984598234"; model.PublishDate = DateTime.Now; model.PublisherId = 72; model.TOC = "aaaaaaa"; model.UnitPrice = 22.3m; model.WordsCount = 1234; //先将数据储存到数据库中 //然后再向队列中添加 SearchIndexManager.GetInstance().AddQueue("2323", model.Title, model.ContentDescription); return(Content("ok")); }
public void AddBooksInfo(Books book) { //上传图片 HttpPostedFileBase upload = Request.Files["img"]; string s = upload.FileName; string[] str = s.Split('.'); if (str[1] != "jpg") { WebCommon.GoBack("对不起,图片只能上传jpg格式的!"); } else { string path = "/Images/BookCovers/" + book.ISBN + ".jpg"; var addbooks = booksbll.AddEntity(book); SearchIndexManager.GetInstance().AddQueue(book.Id, book.Title, book.ISBN, book.UnitPrice, book.ContentDescription, book.Discount); if (addbooks != null) { upload.SaveAs(Server.MapPath(path)); WebCommon.ShowUrl("添加成功", "/Admin/AdminHome/ShowBooksInfo"); } else { WebCommon.GoBack("添加失败"); } } //var list = booksbll.LoadEntities(c => true).ToList(); //foreach (var book in list) //{ // SearchIndexManager.GetInstance().AddQueue(book.Id, book.Title, book.ISBN, book.UnitPrice, book.ContentDescription, book.Discount); //} //WebCommon.ShowUrl("添加成功111", "/Admin/AdminHome/ShowBooksInfo"); }