我正在尝试缓存数据并一直失败。正如您在下面的代码中看到的,我调用 Web 服务返回一个名为“类别”的数组。这个电话是成功的,我得到了信息。在使用结果填充下拉列表后,我使用 Insert 来存储要缓存的信息。页面刷新后(我想我应该说回发以外的任何内容),我检查缓存以查看信息是否存在。从来都不是。在这个问题上抓挠我的头......
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);
}
}