ikmb@163.com 一.添加缓存 /// <summary> /// 绝对时间过期 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { string strnow = DateTime.Now.ToString(); //Cache.Insert("EMP_NAME", strnow, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(60)); Cache.Insert("EMP_NAME", strnow, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration); Response.Write("First cache at:" + strnow); } /// <summary> /// 文件依赖 文件被更改缓存就过期 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { string strnow = DateTime.Now.ToString(); CacheDependency fileDependency = new CacheDependency(Server.MapPath("myxml.xml")); Cache.Insert("EMP_NAME", strnow, fileDependency); Response.Write("First cache at:" + strnow); } 二.测试缓存 string cachedString; cachedString = (string)Cache["EMP_NAME"]; if (cachedString == null) { Response.Write("no cache." ); } else { Response.Write("hit cache.cache data:" + cachedString); }