Reusing IndexSearcher

I am using Lucene in a web application and want to reuse the same Indexsearcher instance for all incoming requests.

Does this logic make sense (using 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


a source to share


1 answer


You need to sync the creation of Searcher to avoid race conditions. Also, I'm not sure if comparing DateTime objects using the == operator is the correct way to do this. However, I am not a C # expert. Searcher creation can be done in one place by combining condition 1 and condition 3.



0


a source







All Articles