我正在试验缓存数据并保持失败。正如你在下面的代码中看到的那样,我呼吁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