Lucene搜索结果分页 query+cache 输出 (转)

导读:
  在lucene搜索分页过程中,可以有两种方式
  一种是将搜索结果集直接放到session中,但是假如结果集非常大,同时又存在大并发访问的时候,很可能造成服务器的内存不足,而使服务器宕机
  还有一种是每次都重新进行搜索,这样虽然避免了内存溢出的可能,但是,每次搜索都要进行一次IO操作,如果大并发访问的时候,你要保证你的硬盘的转速足够的快,还要保证你的cpu有足够高的频率
  而我们可以将这两种方式结合下,每次查询都多缓存一部分的结果集,翻页的时候看看所查询的内容是不是在已经存在在缓存当中,如果已经存在了就直接拿出来,如果不存在,就进行查询后,从缓存中读出来.
  比如:现在我们有一个搜索结果集 一个有100条数据,每页显示10条,就有10页数据.
  安装第一种的思路就是,我直接把这100条数据缓存起来,每次翻页时从缓存种读取
  而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询
  第三种思路就变成了
  我第一页仅需要10条数据,但是我一次读出来50条数据,把这50条数据放入到缓存当中,当我需要10--20之间的数据的时候,我的发现我的这些数据已经在我的缓存种存在了,我就直接存缓存中把数据读出来,少了一次查询,速度自然也提高了很多. 如果我访问第六页的数据,我就把我的缓存更新一次.这样连续翻页10次才进行两次IO操作
  同时又保证了内存不容易被溢出.而具体缓存设置多少,要看你的服务器的能力和访问的人数来决定
  下面我给出我的代码,仅仅是测试代码,写的比较凌乱,但是应该能够解释我刚才的思路:
  class CacheList
  {
  
  private int pageIndex=1 private int cacheSize;
  public List<document> CachePage = new List<document>(); <br>  private bool isFirst = true //是否是第一次搜索 public bool IsFirst <br>  { <br>  get { return isFirst; } set { isFirst = value; } } <br>  //设置索引的开始页 public int PageIndex <br>  { <br>  get{ return pageIndex;} set{ pageIndex = value;} } //设置缓存页数 public int CacheSize <br>  { <br>  get { return cacheSize; } set { cacheSize = value; } } //判断是否在缓存中 public bool InCache(int pindex) <br>  { <br>  if (pindex = pageIndex &amp;&amp; !isFirst) <br>  { <br>  return true } //如果不在缓存中将第一次搜索的标示更新 isFirst = true return false } //清空缓存 public void flushCache() <br>  { <br>  for(int i=0i<cachepage.count>  { <br>  CachePage.RemoveAt(i); <br>  } } //增加缓存 public void AddCache(Document doc) <br>  { <br>  CachePage.Add(doc); <br>  } //读取缓存 <br>  //pindex 为页数 public List<document pindex readcache>  { <br>  int index = (pindex-1)%CacheSize * 10 int end = index + 10 if (CachePage.Count   { <br>  end = CachePage.Count; <br>  } if (end &gt;= index) <br>  { <br>  return CachePage.GetRange(index, end - index); <br>  } else { <br>  return new List<document>(); <br>  } <br>  } } <br>  搜索方法代码: <br>  void search() <br>  { <br>  cList.CacheSize = 5 if (cList.InCache(Convert.ToInt32(PageIndex.Text))) <br>  { <br>  Console.WriteLine("--------从缓存中输出第" + PageIndex.Text + "页的数据----------------"); <br>  List<document> pageList = cList.ReadCache(Convert.ToInt32(PageIndex.Text)); <br>  for(int i=0i<pagelist.count>  { <br>  Console.WriteLine(pageList[i].Get("prodname")); <br>  } } else { <br>  cList.PageIndex = Convert.ToInt32(PageIndex.Text); <br>  Analyzer analyzer = new KTDictSegAnalyzer(); <br>  IndexSearcher isearcher = new IndexSearcher(Index_Store_Path); <br>   <br>  QueryParser parser = new QueryParser("prodname", analyzer); <br>  Query query = parser.Parse(this.textBox1.Text); <br>   <br>  Hits hits = isearcher.Search(query); <br>  Console.WriteLine("--------开始更新缓存----------------"); <br>  //先清空缓存 cList.flushCache(); <br>  int startIndex = (Convert.ToInt32(PageIndex.Text) - 1) * 10 int EndIndex = (Convert.ToInt32(PageIndex.Text) - 1) * 10 +(10*cList.CacheSize); <br>  if (EndIndex &gt; hits.Length()) <br>  { <br>  EndIndex = hits.Length(); <br>  } for (int i = startIndex; i   { <br>  cList.AddCache(hits.Doc(i)); <br>  }<br>isearcher.Close();<br><br>List<span style="COLOR: #000000"><span style="COLOR: #000000">Document</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">pageList</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">cList.ReadCache(Convert.ToInt32 (PageIndex.Text));<br></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"></span><span style="COLOR: #800080">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000"><span style="COLOR: #000000">pageList.Count;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br></span><span id="Codehighlighter1_1769_1856_Open_Text"><span style="COLOR: #000000">{<br>Console.WriteLine(pageList[i].Get(</span><span style="COLOR: #800000">"</span><span style="COLOR: #800000">prodname</span><span style="COLOR: #800000">"</span><span style="COLOR: #000000">));<br>}</span></span><span style="COLOR: #000000"><br>cList.IsFirst</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>}</span><span style="COLOR: #000000"><br>}</span><span style="COLOR: #000000"><br></span><br>本文转自 <br><a href="http://www.cnblogs.com/suyuan/archive/2008/04/03/1136288.html">http://www.cnblogs.com/suyuan/archive/2008/04/03/1136288.html</a></span></span></pagelist.count></document></document></document></cachepage.count></document></document>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值