1.OutputCache 属性 contact.cshtml

   [OutputCache(Duration=10)]

public ActionResult Contact()

  {
      ViewBag.Message ="Your contact page.";
      returnView();
  }

       action 结果会缓存10秒钟

 
2.带参数的OutputCache

[OutputCache(Duration=3600, VaryByParam="searchTerm")]
publicActionResult Contact(stringsearchTerm)

 

 如:http://localhost:xxxx/Home/Contact?searchTerm=a,这个结果就会缓存一个小时.下次访问相同的链接,缓存期内则会返回缓存结果, 下一步如果换成
 http://localhost:xxxx/Home/Contact?searchTerm=b呢?程序会执行action,并将结果缓存,此时再访问http://localhost:xxxx/Home/Contact?searchTerm=a
 会返还a的缓存结果,这里缓存了两个不同的结果,就是会根据searchTerm的不同值进行对应的缓存。
 
 
 如果想对每个不同的参数进行缓存,可以设置varbyparam="*";如果对任何参数都缓存相同的结果,可以设置varbyparam=“none”
 
 
3.缓存child action  partialviewtest.cshtml
 
  

  <p>
    This is a partial view.
  </p>
 

publicActionResult PartialViewTest()
{
    returnView();
}
 
    contact.cshtml中,加入如下代码@Html.Action("PartialViewTest")
  
    在partialviewtest action上加入[OutputCache(Duration=3600)]设置,partialviewtest的缓存时间不会受contact action缓存时间的影响
 
4 指定缓存位置Specify the cache location
    [OutputCache(Duration = 3600, Location=System.Web.UI.OutputCacheLocation.ServerAndClient)]
   
    默认值是serverandclient
 
5.根据header值进行缓存
  例如多区域语言情况下 [OutputCache(Duration = 3600, VaryByHeader="Accept-Language")],可以根据不同的语言进行缓存。
  

other type of header value will be important for AJAX requests. Let’s imagine that the Contact action is also available to AJAX calls. In other words only that portion of the screen will be refreshed which makes up the View result of the Contact action. The rest, i.e. the header, footer, menu etc. will stay constant. If a user then bookmarks a specific search result, e.g.:

http://localhost:xxxx/Home/Contact?searchTerm=a

…and will call this search directly from his/her browser then the Contact action will only serve up a cached version of the Contact view and nothing else. The reason is that user’s browser will send a full GET request and not an asynchronous one. All the other parts of the ‘normal’ full screen, i.e. everything that the AJAX call didn’t touch will be left out. The result will be very weird: a page devoid of styles – apart from any inline style tags in the Contact view – headers, footers etc. The problem lies with a specific header value: X-Requested-With. This is the only difference between the headers of the AJAX and the GET calls: it will be present on an AJAX request but absent on GET requests. To the caching engine there’s no difference between AJAX calls and GET calls because you didn’t specify this in the OutputCache attribute. Two things need to be done:

1. Update the OutputCache attribute to vary the output by this specific header type:

1
[OutputCache(Duration = 3600, VaryByHeader="X-Requested-With")]

Now the caching mechanism will be able to differentiate between two requests: one that wants a full page render and one that will only serve a portion of the page.

Unfortunately there are cases when this update is not enough. The default setting is that we’re letting the action result be cached on both the client and the server. However, some older browsers are not “clever” enough to detect the difference between full GET and partial AJAX requests.

2. To be on the safe side we need to specify that caching only should occur on the server:

You can achieve this as follows:

1
[OutputCache(Duration = 3600, VaryByHeader="X-Requested-With", Location=System.Web.UI.OutputCacheLocation.Server)]

Now the server will send instructions to the browser not to cache the action result. The browser will always have to access the server for a response. The server will then serve up a cached result if there’s any in its memory.

The safest and most elegant solution is of course to create separate action methods for full GET and partial AJAX calls

6 sql缓存依赖 Sql dependency caching
  有个叫SqlDependency的OutputCache的参数,可以用来指定当一个数据库表的的内容发生改变后进行缓存刷新,这个听起来很不错,但是使用很少,原因是在sql查询语句 类型方面有一些限制。
7 自定义缓存
   可以使用VaryByCustom进行自定义缓存机制,详细内容参考msdn
8 缓存配置
 
  上述过多的使用硬编码的,如果缓存策略有变化,你不得不去一一更改,再发布代码,更好的方法是在web.config文件中进行配置。
<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <addname="Aggressive"duration="1600"/>
      <addname="Short"duration="20"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>
可以这样使用:
 [OutputCache(CacheProfile="Aggressive")]
 
原文链接:http://blog.csdn.net/kufeiyun/article/details/9402063

MVC页面缓存的更多相关文章

  1. Mvc 页面缓存 OutputCache VaryByCustom

    优化网站,dotNet MVC 可以通过(OutputCache)特性在某些Action上使用缓存,如果我们想要自定义缓存依据可以通过如下方式进行: 第一步, 在 global.asax.cs 文件中 ...

  2. asp.net mvc 页面缓存

    在任务中需要实现点击浏览器back按钮,加载的前一页面需要强制刷新. 想要在前端通过js来绑定数据实现,但是觉得太麻烦,还是用另一种方式来解决: 不缓存该页面. 简单易懂: Response.Cach ...

  3. [转]Asp.net mvc 网站之速度优化 -- 页面缓存

    网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...

  4. Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

    Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

  5. 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题

    一.ASP.NET MVC中使用OutputCache实现服务器端页面级缓存 在ASP.NET MVC中,假如我们想要将某个页面(即某个Action)缓存在服务器端,可以在Action上标上以下特性: ...

  6. 实现asp.net mvc页面二级缓存,提高访问性能

    实现的mvc二级缓存的类 //Asp.Net MVC视图页面二级缓存 public class TwoLevelViewCache : IViewLocationCache { private rea ...

  7. Asp.net mvc 网站之速度优化 -- 页面缓存

    网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...

  8. ASP.NET MVC 阻止当前请求的视图页面缓存OutputCache

    设置缓存 [OutputCache(Duration =333,VaryByCustom ="Index")] 缓存: //在action中,临时阻止该次请求的视图页面缓存 Res ...

  9. 探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)

    其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...

随机推荐

  1. 构建NetCore应用框架之实战篇(七):BitAdminCore框架登录功能源码解读

    本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.简介 1.登录功能完成后,框架的雏形已经形成,有必要进行复习. 2 ...

  2. ASP.NET webform多次提交表单问题

    最近几天遇到一个头疼的问题,项目采用的是webform开发,每个界面都有个提交按钮,点击多次提交按钮导致提交按钮的OnClick事件执行了多次, 每次OnClick里面都有一些逻辑处理,执行了多次导致 ...

  3. poj 3250 Bad Hair Day(栈的运用)

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  4. 统计进程打开了多少文件,定位too many open files

    [root@ostack1 ~]# lsof -p 18628 | wc -l 114

  5. 【UVA11107 训练指南】Life Forms【后缀数组】

    题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...

  6. Bootstrap框架(一)

    day57 参考:https://www.cnblogs.com/liwenzhou/p/8214637.html 下载:http://www.bootcss.com/   选择用于生产环境的 Boo ...

  7. 操作系统(Operating System,OS)

    操作系统(Operating System,OS) 是配置在计算机硬件上的第一层软件,是对计算机硬件系统的首次扩充,是一个计算机系统最基础,也是最重要的系统软件. 操作系统的作用 1 实现对计算机资源 ...

  8. FunDA(6)- Reactive Streams:Play with Iteratees、Enumerator and Enumeratees

    在上一节我们介绍了Iteratee.它的功能是消耗从一些数据源推送过来的数据元素,不同的数据消耗方式代表了不同功能的Iteratee.所谓的数据源就是我们这节要讨论的Enumerator.Enumer ...

  9. robot framework 测试/预发/线上环境快捷切换

    通常情况下布署的三套环境:测试.预发及线上环境.调试或者辅助验证测试时,切环境改变量甚是麻烦.这些变量包括但不限于:一些url信息,数据库信息,预置用户信息等. 切换环境方法一:使用变量文件,通过判断 ...

  10. hexo安装总结

    博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 hexo真心是一个不错的东西呢,安装简单(然而对博主来说并不是这样,伤心脸),主题样式简洁优雅,还有多种选择. 流程如下 安 ...