我正在試驗緩存數據並保持失敗。正如你在下面的代碼中看到的那樣,我呼籲Web服務返回一個名爲「categories」的數組。這個電話很成功,我收到了信息。在用結果填充下拉列表之後,我使用Insert來存儲要緩存的信息。在頁面刷新(我想我應該說除回發之外的任何東西),我檢查緩存,看看信息是否存在。它從來沒有。抓我就這一個頭......Cache.Get always returns null
if (! IsPostBack)
{
//See if the results have been cached. With arrays and checking for null, you have to
//check the array itself before its elements can be checked.
string[] saveCatList = Cache.Get("Categories" + Session["sessionID"]) as string[];
if (saveCatList == null || string.IsNullOrEmpty(saveCatList[0]))
{
WBDEMOReference.getcatlist_itemcategories[] categories;
strResult = callWebServ.getcatlist(Session["sessionID"].ToString(),
out strResultText, out dNumOfCat, out categories);
for (int i = 0; i < categories.Length; i++)
{
//ddCat is the ID number of the category drop down list
ddCat.Items.Add(new ListItem(categories[i].categorydesc.ToString(),
categories[i].categorynumber.ToString()));
}
//Store the array into cache. 'Cache.Remove' will remove the cache for that key
Cache.Insert("Categories" + Session["sessionID"], categories, null,
DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
}
}
你調試了Session [「sessionID」]是什麼嗎?可能是它正在改變,並改變你正在查找緩存中的值的緩存鍵 – Dimitri 2012-07-09 18:47:11
我做過,是的。 Session [「sessionID」]在Insert和Get time時相同。 – 2012-07-09 18:53:52
是否有可能'categories'不是'string []'類型的?在這種情況下,'as'操作符會導致它變爲null – 2012-07-09 19:01:12