Kay*_*son 0 c# lucene lucene.net
在我的解决方案之一中WCF,用户可以在 Lucene 索引上进行搜索。我希望获得有关该方法的意见。
我可以以更好的方式改进这段代码吗?
public class ViewData
{
public static IndexReader indexReader
{
get
{
return IndexReader.Open(FSDirectory.Open("path"), true);
}
private set { indexReader = value; }
}
public static IndexSearcher indexSearcher { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
消费
using (indexSearcher = new IndexSearcher(indexReader))
{
// run the search etc
}
Run Code Online (Sandbox Code Playgroud)
这意味着IndexReader当搜索器以这种方式打开时,永远不会关闭。
但如果我确实像下面这样。
using (indexReader)
using (indexSearcher = new IndexSearcher(indexReader))
{
// run the search etc
}
Run Code Online (Sandbox Code Playgroud)
但这意味着读者总是关闭并重新打开?我可以跳过using(indexReader)并保持打开状态(因为IndexReaderis ,这不是不好的做法吗Disposable)?
您可以针对多个请求重复使用 IndexReader 和 IndexSearcher 实例。它们是线程安全的,可以同时处理多个请求。无需关闭或处置您的阅读器和搜索,垃圾收集将自动处理该问题。事实上,如果您在多个请求中重用实例,则很难做到这一点,因为您不知道是否有人同时使用它。