asp.net缓存

protected void Page_Load(object sender, EventArgs e)
        {
            string ids="";
            Maticsoft.BLL.ScriptsBak bll = new Maticsoft.BLL.ScriptsBak();
            List<Maticsoft.Model.ScriptsBak> list = new List<Maticsoft.Model.ScriptsBak>();
            list = bll.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                ids += list[i].ScriptId.ToString()+"--";
            }
            ids = ids + "完";  //这里的ids为从数据库中读取表中的id值然后用--链接起来的一个字符串
            if (Cache["key"] == null)
            {
                Cache.Insert("key", ids, null, DateTime.Now.AddSeconds(40), System.Web.Caching.Cache.NoSlidingExpiration);  //这里给数据加缓存,设置缓存时间
                //"key"为缓存的键,ids为缓存起来的值,null为缓存依赖项,这里没有使用缓存依赖项,所以为null,下面会详细介绍缓存依赖项
                   //null后面为缓存的时间为40秒
                  //最后一个参数为设置时间的格式,ASP.NET允许你设置一个绝对过期时间或滑动过期时间,但不能同时使用,
                  //我们这里设置的为绝对过期时间,也就是没刷新一次页面后缓存数据为40秒,40秒后会从数据库中重新获取。 
                Response.Write("cache加载为---" + Cache["key"] + "</br>");
            }
            else
            {
                Response.Write("cache加载为---" + Cache["key"] + "</br>");
            }
            Response.Write("直接加载为---" + ids + "</br>");
        }

清除缓存

System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove("date");

posted @ 2017-09-20 09:41  晓涵在路上  阅读(192)  评论(0编辑  收藏  举报