zoukankan      html  css  js  c++  java
  • ASP.NET缓存OutputCache和Response.Cache之C#后台设置

    一、ASPX页面缓存
    页面缓存的使用方法非常的简单,只需要在aspx页的顶部加一句声明<%@ OutputCache Duration="60" VaryByParam="none" %>
     这样整个页面的内容都会被缓存,页面中的ASP.NET代码、数据源在缓存期间都不会被运行,而是直接输出缓存的页面内容。
     页面缓存是针对所有这个页面的访问者。这样1个访问者和1万个访问者、一次访问和100万次访问对数据库的压力是一样的。
    二、outpuCache参数
    Duration
    :缓存时间,单位秒
    VaryByParam:缓存参数,
      VaryByParam=none 无参数缓存,可用于首页;
      VaryByParam="*" 如果想让任何不同的查询字符串都创建不同的缓存,则设置VaryByParam="*",一般情况下设置“*”就足够了。
      VaryByParam="id" 因为?id=2、?id=3只是页面的不同参数而已,为了能让不同的新闻各种缓存,因此可以设置VaryByParam="id"
      DiskCacheable="true|false"  意思是要不要把缓存放到硬盘上

    注意:Buffer = true;以下设置才会生效,页面默认就等于true

       三、以下是代码示例的@ OutputCache指令和等效的编程代码。

    • 若要将输出缓存存储指定的持续时间

      声明性方法:
      <%@ OutputCache Duration="60" VaryByParam="None" %>

      编程的方法:
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
      Response.Cache.SetCacheability(HttpCacheability.Public);
    • 在其中发出请求的浏览器客户端上存储输出缓存

      声明性方法:
      <%@ OutputCache Duration="60" Location="Client" VaryByParam="None" %>

      编程的方法:
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
      Response.Cache.SetCacheability(HttpCacheability.Private);
    • 在任何 HTTP 1.1 支持缓存的设备,包括代理服务器和发出请求的客户端上存储输出缓存

      声明性方法:
      <%@ OutputCache Duration="60" Location="Downstream" VaryByParam="None" %>

      编程的方法:
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
      Response.Cache.SetCacheability(HttpCacheability.Public);
      Response.Cache.SetNoServerCaching();
    • 若要将输出缓存存储在 Web 服务器上

      声明性方法:
      <%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>

      编程的方法:
      TimeSpan freshness = new TimeSpan(0,0,0,60); 
      DateTime now = DateTime.Now; 
      Response.Cache.SetExpires(now.Add(freshness)); 
      Response.Cache.SetMaxAge(freshness); 
      Response.Cache.SetCacheability(HttpCacheability.Server); 
      Response.Cache.SetValidUntilExpires(true);
    • 缓存到达时,使用一个不同的城市每个 HTTP 请求的输出:

      声明性方法:
      <%@ OutputCache duration="60" varybyparam="City" %>

      编程的方法:
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
      Response.Cache.SetCacheability(HttpCacheability.Public);
      Response.Cache.VaryByParams["City"] = true;

      更新缓存:HttpResponse.RemoveOutputCacheItem("/index.aspx");
      注意:地址必须是绝对虚拟路径
  • 相关阅读:
    Informix日期获取上周上月昨天去年SQL
    PDI-KETTLE-4 使用Kettle完成通用DB生成指定文件并通过FTP上传
    日常问题解决记录二:DOS下切换盘符和工作目录
    PDI-KETTLE-3:数据库连接
    window下安装node.js
    【原创】正则断言的使用--为自动生成的get方法添加注解字段
    【原创】文本工具的使用--根据数据库字段快速生成该表对应的Model类属性
    【原创】字符串工具类--驼峰法与下划线法互转
    【原创】字符串工具类--获取汉字对应的拼音(全拼或首字母)
    【原创】关于oracle11G空表无法导出问题的解决方法
  • 原文地址:https://www.cnblogs.com/webapi/p/4151657.html
Copyright © 2011-2022 走看看