public void Push(LibrarySearchResult result) { var cacheKeyword = LibrarySearchResult.GenCacheKeyword(result.Keyword, result.CurrentPage); if(try2Hit(cacheKeyword) == null) { mQueue.Enqueue(result); } }
private LibrarySearchResponse(HttpRequestBase request, LibrarySearchResult result) : base(result.User) { var keyword = result.Keyword; var moreUrl = result.MoreUrl; var books = result.Books; var converter = new UrlToAbsConverter(request); this.Articles = new List<Article>() { new Article { Title = new StringXmlCDataSection(String.Format("搜索 {0} 的结果: {1}/{2}页", keyword, result.CurrentPage, result.PageCount)), Description = new StringXmlCDataSection(keyword), PicUrl = new StringXmlCDataSection(converter.Convert("/Content/images/lib.jpg")), Url = new StringXmlCDataSection(converter.Convert("/Home/About")), } }; if (books != null && books.Count > 0) { var bookArticles = from book in books select new Article { Title = new StringXmlCDataSection(String.Format("[{0} 馆藏:{1}/{2}] {3} ({4})", book.Index, book.Available, book.Total, book.Title, book.Author)), Description = new StringXmlCDataSection(keyword), PicUrl = book.Available > 0 ? new StringXmlCDataSection(converter.Convert("/Content/Images/green_circle.png")) : new StringXmlCDataSection(converter.Convert("/Content/Images/red_circle.png")), Url = new StringXmlCDataSection(getDetailUrl(converter, book.Url)) }; this.Articles.AddRange(bookArticles); this.Articles.Add(new Article { Title = new StringXmlCDataSection(CMD), PicUrl = new StringXmlCDataSection(converter.Convert("/Content/Images/frog.jpg")), Url = new StringXmlCDataSection(converter.Convert("/Home/About")) }); } else { this.Articles.Add(new Article { Title = new StringXmlCDataSection("没有匹配项"), PicUrl = new StringXmlCDataSection(converter.Convert("/Content/Images/frog.jpg")), Url = new StringXmlCDataSection(converter.Convert("/Home/About")) }); } }
public bool Search(LibrarySearchOption option, out LibrarySearchResult result) { /* #if DEBUG * result = getLocalResult(); * result.ResultCount = expectedCount; * if (pageIndex < expectedCount / option.PageSize) * { * pageIndex++; * result.Books = result.Books.GetRange(0, option.PageSize); * } * else * { * result.Books = result.Books.GetRange(0, expectedCount - pageIndex * option.PageSize); * } * return true; #endif */ List <Book> books = null; int pageCount = 0; int resultCount = 0; var url = getSearchUrl(option); try { var request = WebRequest.Create(url) as HttpWebRequest; Stream stream = (request.GetResponse() as HttpWebResponse).GetResponseStream(); Parse(stream, out books, out pageCount, out resultCount); result = new LibrarySearchResult { Books = books, PageCount = pageCount, ResultCount = resultCount, Error = null }; return(true); } catch (WebException e) { result = new LibrarySearchResult { Error = e }; return(false); } }
public static LibrarySearchResponse Create(HttpRequestBase request, LibrarySearchResult result) { return new LibrarySearchResponse(request, result); }
public bool Search(LibrarySearchOption option, out LibrarySearchResult result) { /* #if DEBUG result = getLocalResult(); result.ResultCount = expectedCount; if (pageIndex < expectedCount / option.PageSize) { pageIndex++; result.Books = result.Books.GetRange(0, option.PageSize); } else { result.Books = result.Books.GetRange(0, expectedCount - pageIndex * option.PageSize); } return true; #endif */ List<Book> books = null; int pageCount = 0; int resultCount = 0; var url = getSearchUrl(option); try { var request = WebRequest.Create(url) as HttpWebRequest; Stream stream = (request.GetResponse() as HttpWebResponse).GetResponseStream(); Parse(stream, out books, out pageCount, out resultCount); result = new LibrarySearchResult { Books = books, PageCount = pageCount, ResultCount = resultCount, Error = null }; return true; } catch(WebException e) { result = new LibrarySearchResult { Error = e }; return false; } }
public LibrarySearchResult SearchBooksFor(HttpSessionStateBase session, LibrarySearchOption option, out object error) { error = null; var user = option.User; var keyword = option.Keyword; var page = option.Page; LibrarySearchResult cached; if ((cached = mResultCache.Try2Hit(keyword, page)) != null) { ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" + keyword + " " + page + " hited"); option.PageCount = cached.PageCount; return cached; } option = option == null ? DEFAULT : option; var url = getSearchUrl(keyword, option); try { var request = WebRequest.Create(url) as HttpWebRequest; Stream stream = (request.GetResponse() as HttpWebResponse).GetResponseStream(); List<Book> books = null; int pageCount = 0; Parse(stream, out books, out pageCount); cached = new LibrarySearchResult { Keyword = keyword, User = user, Books = books, MoreUrl = url, PageCount = pageCount, CurrentPage = option.Page }; //记录查询结果的总页数 option.PageCount = pageCount; mResultCache.Push(cached); ApplicationLogger.GetLogger().Info("(" + session.SessionID + ")" + "push cache: " + keyword + " " + page + " current cache count: " + mResultCache.Count); return cached; } catch (WebException e) { error = e; return null; } }