English 中文(简体)
纽约总部
原标题:
  • 时间:2009-05-12 05:32:53
  •  标签:

页: 1

∗∗∗∗ iii

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); 
    }
}
问题回答

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.





相关问题
热门标签