我有一個用C#編寫的函數,它在檢查並插入緩存後返回業務實體(make)的集合。
數據緩存從函數返回空值的機會
public static Collection<CProductMakesProps> GetCachedSmartPhoneMake(HttpContext context)
{
var allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
if (allMake == null)
{
context.Cache.Insert("SmartPhoneMake", new CModelRestrictionLogic().GetTopMakes(), null,
DateTime.Now.AddHours(Int32.Parse(ConfigurationManager.AppSettings["MakeCacheTime"])),
Cache.NoSlidingExpiration);
allMake = context.Cache["SmartPhoneMake"] as Collection<CProductMakesProps>;
}
return allMake;
}
我在其他頁面使用它,如下所示
var lobjprodMakeCol = CBrandCache.GetCachedSmartPhoneMake(Context);
//CBrandCache is the class which contain the method
是否有可能,我得到的lobjprodMakeCol
由於空值。
編輯
注new CModelRestrictionLogic().GetTopMakes()
是從數據庫中提取記錄的功能。
它將返回0或更多的收集天氣。
感謝您的建議和答覆。但我想知道你和@slugster方法有什麼區別? –
@krshekhar,正如我所說的,即使緩存過期時間爲零(表示不需要緩存),上面的代碼也不會返回null。值得注意的是,它消除了緩存未命中情況下不必要的緩存查找,但是在方案中這並不重要!此外,你可以進一步重新考慮上面的代碼,從靜態構造函數中讀取緩存過期時間,但是再次... – VinayC