我在全局中的代码:<script runat="server">
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
OptCache.Add("ysz", "杨思照");
}
...自已定义的类:public class OptCache
{
public OptCache()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void Add(string key, string val)
{
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(OnRemove);
HttpContext.Current.Cache.Insert(
key,
val,
null,
System.DateTime.Now.AddSeconds(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
callBack
);
}
public static void OnRemove(string key, object val, CacheItemRemovedReason rea)
{
if (HttpContext.Current.Cache[key] != null)
{
HttpContext.Current.Cache.Remove(key);
}
HttpContext.Current.Cache.Add("ysz",
"彭小岗",
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(OnRemove)
);
}
}
页面中:protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Cache["ysz"].ToString());
}
页面在5秒钟之后,点击刷新会报错???请大师指点,该如何写,我只是想实现定时更新CACHE!!!
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
OptCache.Add("ysz", "杨思照");
}
...自已定义的类:public class OptCache
{
public OptCache()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void Add(string key, string val)
{
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(OnRemove);
HttpContext.Current.Cache.Insert(
key,
val,
null,
System.DateTime.Now.AddSeconds(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
callBack
);
}
public static void OnRemove(string key, object val, CacheItemRemovedReason rea)
{
if (HttpContext.Current.Cache[key] != null)
{
HttpContext.Current.Cache.Remove(key);
}
HttpContext.Current.Cache.Add("ysz",
"彭小岗",
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.NotRemovable,
new CacheItemRemovedCallback(OnRemove)
);
}
}
页面中:protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Cache["ysz"].ToString());
}
页面在5秒钟之后,点击刷新会报错???请大师指点,该如何写,我只是想实现定时更新CACHE!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货