日期:2014-05-17  浏览次数:20491 次

Cache过期问题
slidingExpiration: 最后一次访问所插入对象时与该对象过期时之间的时间间隔。如果该值等效于 20 分钟,则对象在最后一次被访问 20 分钟之后将过期并被从缓存中移除。如果使用可调过期,则absoluteExpiration 参数必须为 System.Web.Caching.Cache.NoAbsoluteExpiration。

在a.aspx页面用ajax写一个cache,5秒后过期:
var userinfo = new Dictionary<string, string>();
userinfo.Add("name", "张三");
userinfo.Add("age", "30");

HttpRuntime.Cache.Add("UserInfo", userinfo, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5),
  CacheItemPriority.High, null);

在b.aspx页面每间隔5秒,用ajax读这个cache:
if (HttpRuntime.Cache["StudentUID"] == null)
  return Content("NO Chache");

var userinfo = (Dictionary<String, String>)HttpRuntime.Cache["UserInfo"];
return Content(userinfo["name"] + userinfo["age"]);

现在的结果是:
张三30
NO Chache
NO Chache
"对象在最后一次被访问 20 分钟之后将过期并被从缓存中移除",好像不是这样啊,请问什么原因??


------解决方案--------------------
需要把cache的时间设置得更长一点,比如8秒,因为在你AJAX去请求的时候也需要时间出来,FW处理也需要时间