I am setting up a WCF web service that hosts a Lucene.Net / AzureDirectory search engine.
For speed purposes, the recommended way to use AzureDirectory is to have an singleton IndexSearcher instance that can be used by all service calls. Here is what I have to do that:
public class LuceneHelper
{
private static IndexSearcher _searcher;
public static IndexSearcher searcher
{
get
{
if (_searcher == null)
initSearcher();
return _searcher;
}
}
public static void initSearcher()
{
_searcher = new IndexSearcher(azureDirectory);
}
...
}
Since that object caches the index it is searching I need to reinitialize that object on an interval asynchronously from any client calls… like every minute or so. So I tried to setup a process running in another thread on my azure instance that does this…
LuceneHelper.initSearcher();
while (true)
{
Thread.Sleep(60000);
LuceneHelper.initSearcher();
}
I can tell from my testing that they both are working but independently of each other. So I guess there is an IndexSearcher in my web service and one in my worker thread.
My Question: Is there a way to run my infinite ‘while’ statement in my webservice asynchronously or any other way to reinitialize an object without a call from a client?
Answering your direct question, yes, it’s possible to have the while statement run in the background of your webservice. All you have to do is create an instance of the
Threadclass and have it run your loop and store a reference to it in your application (your singleton class would be a good place).That said, I wouldn’t recommend this, as you don’t need to open/close/reopen the
IndexSearcherclass. The recommended practice is to maintain a sharedIndexWriterinstance for the life of your app and then call theGetReadermethod to get anIndexReaderinstance on-demand (from which you can then create anIndexSearcher). This is generally much faster and more maintainable than recreating the index (and it’s real-time as well, as the call toGetReaderwill have all the changes to the index).