当一份asp.net网页第一次被访问,会被编译成IL,接着是Native Code,而Native Code会缓存成Page类,在下一次用户请求同一页面的时候直接供其使用。当asp.net初始文件被更新,或者超出缓存区的时间设置(Timeout),整份asp.net网页会被重新编译,并再一次置入Page类的缓存区中。

一、超时设置
缓存 Output Cache<%@ OutputCache Duration="600"%>

二、指定绝对到期日
    假如要在10分钟之后重新缓存一次页面,可以设置Response.Cache对象的两个方法,SetExpires与SetCacheability,程序会自动更改HTTP标头。
缓存 Output CacheResponse.Cache.SetExpires(DateTime.Now.AddSeconds(600));
缓存 Output CacheResponse.Cache.SetCacheability(HttpCacheability.Public);
    以上两个语句可以精简地写成这样
缓存 Output CacheResponse.Expires="600";
缓存 Output CacheResponse.CacheControl
="public";

三、指定相对到期日
    用来指定最后一次访问后,多久超时。
    举例来说,某网站可能一天才更新一次,但访问量动辄数万,如果设置为第一次访问后10分钟失效,再缓存一次,似乎没什么帮助,因此改为最后一次被调用后多久才失效,也就是相对到期日。
    关键在于 Response.Cache 对象的 SetSlidingExpiration 属性设置为 True
缓存 Output CacheResponse.Cache.SetExpires(DateTime.Now.AddSeconds(3600));
缓存 Output CacheResponse.Cache.SetCacheability(HttpCacheability.Public);
缓存 Output CacheResponse.Cache.SetSlidingExpiration(True);

相关文章:

  • 2021-11-15
  • 2021-11-14
  • 2021-09-24
  • 2021-07-06
  • 2022-02-28
  • 2021-10-28
  • 2021-05-29
猜你喜欢
  • 2021-07-08
  • 2022-01-15
  • 2021-08-17
  • 2021-08-06
相关资源
相似解决方案