views:

174

answers:

1

Hi,

Am using Lucene in a web based application and want to reuse the same instance of Indexsearcher for all the incoming requests.

Does this logic(using C#) make sense?Please suggest.

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); 
    }
}
A: 

You need to synchronize creation of Searcher to avoid race conditions. Also, I am not sure if comparison of DateTime objects by == operator is the right way of doing it. I am not a C# expert, though. Creation of Searcher can be done at one place by combining condition 1 and condition 3.

Shashikant Kore
http://stackoverflow.com/questions/899542/problem-using-same-instance-of-indexsearcher-for-multiple-requeststhis question is related to the question above...any thoughts ?Thanks--Steve
Steve Chapman