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");

转载于:https://www.cnblogs.com/njy888888/p/7559889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值