2009-05-12 4 views
0

웹 기반 응용 프로그램에서 Lucene을 사용하고 들어오는 모든 요청에 ​​대해 Indexsearcher의 동일한 인스턴스를 다시 사용하려고합니다.IndexSearcher 재사용

이 논리 (C# 사용)는 의미가 있습니까? 제안하십시오.

DateTime lastWriteTime = System.IO.Directory.GetLastWriteTime(myIndexFolderPath); 

if (HttpRuntime.Cache["myIndexSearcher"] == null) //Cache is empty 

{ 
    searcher = new IndexSearcher(myIndexFolderPath); 
    HttpRuntime.Cache.Insert("myIndexSearcher", searcher); 
    HttpRuntime.Cache.Insert("myIndexTimeStamp", lastWriteTime); 
} 
else //Cache is not empty 
{ 
    DateTime cachedDateTime = (DateTime)HttpRuntime.Cache["myIndexTimeStamp"]; 
    if (cachedDateTime == lastWriteTime)//Cache is not yet stale 
    { 
     searcher = (IndexSearcher)HttpRuntime.Cache["myIndexSearcher"]; 
    } 
    else 
    { 
     searcher = new IndexSearcher(myIndexFolderPath); //index folder is modified...update searcher 
     HttpRuntime.Cache.Insert("myIndexSearcher", searcher); 
     HttpRuntime.Cache.Insert("myIndexTimeStamp", lastWriteTime); 
    } 
} 
+0

모든 코드 4 칸을 들여 쓰면 읽을 수 있습니다. – itsadok

+0

감사합니다. 이것을 염두에 두시기 바랍니다. –

답변

0

경쟁 조건을 피하기 위해 Searcher 생성을 동기화해야합니다. 또한, == 연산자로 DateTime 객체를 비교하는 것이 올바른 방법인지는 잘 모르겠습니다. 나는 C# 전문가는 아니지만. 검색 자 생성은 조건 1과 조건 3을 결합하여 한 곳에서 수행 할 수 있습니다.

+0

http://stackoverflow.com/questions/899542/problem-using-same-instance-of-indexsearcher-for-multiple-requests 이 질문은 관련 위의 질문 ... 어떤 생각? 감사합니다. - Steve –