The caching options available in ASP.NET MVC applications don’t come from the ASP.NET MVC Framework, but from the core ASP.NET Framework.

1. Request-Scoped Caching

Every ASP.NET request begins with the ASP.NET Framework creating a new instance
of the System.Web.HttpContext object to act as the central point of interaction between
components throughout the request.

One of the many properties of the HttpContext is the HttpContext.Items property, a
dictionary that lives throughout the lifetime of the request and which any component
may manipulate.

eg:

// how to store data in the collection:
HttpContext.Items["IsFirstTimeUser"] = true; // Retrieving data from the dictionary is just as easy:
bool IsFirstTimeUser = (bool)HttpContext.Items["IsFirstTimeUser"];

2. User-Scoped Caching

ASP.NET session state allows you to store data that persists between multiple requests.

// store the username in a session
HttpContext.Session["username"] = "Hrusi"; // retrieve and cast the untyped value:
string name = (string)HttpContext.Session["username"];

<system.web>
  <sessionState timeout="30" />
</system.web>

3. The ASP.NET Cache

System.Web.Cache is a key/value store

4. The Output Cache

ASP.NET provides the ability to operate at a higher level, caching the HTML that is generated as a result of a request.

[OutputCache(Duration=, VaryByParam="none")]
public ActionResult Contact()
{
ViewBag.Message = DateTime.Now.ToString();
return View();
}

Configuring the cache location

For example, say you want to cache a page that displays the current user’s name. If you
use the default Any setting, the name of the first person to request the page will incorrectly
be displayed to all users.

To avoid this, configure the output cache with the Location property set to Output
CacheLocation.Client and NoStore set to true so that the data is stored only in the user’s
local web browser:

[OutputCache(Duration = , VaryByParam = "none", Location = OutputCacheLocation.Client, NoStore = true)]
public ActionResult About()
{
ViewBag.Message = "The current user name is " + User.Identity.Name;
return View();
}

Varying the output cache based on request parameters

For example, say you have a controller action named Details that displays the details
of an auction:

[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Details(string id)
{
var auction = _repository.Find<Auction>(id);
return View("Details", auction);
}

Caching in ASP.NET MVC的更多相关文章

  1. 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面

    Donut Caching是缓存除了部分内容以外的整个页面的最好的方式,在它出现之前,我们使用"输出缓存"来缓存整个页面. 何时使用Donut Caching 假设你有一个应用程序 ...

  2. ASP.NET MVC 缓存扩展 - Donut Caching

    项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...

  3. asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践

    最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...

  4. asp.net MVC 应用程序的生命周期

    下面这篇文章总结了 asp.net MVC 框架程序的生命周期.觉得写得不错,故转载一下. 转载自:http://www.cnblogs.com/yplong/p/5582576.html       ...

  5. [Asp.net mvc]OutputCacheAttribute

    什么是Cache? 缓存在web应用中是一种以空间换去时间的技术,把频繁访问并且不经常变化的数据存储到内存中,以达到快速访问的目的.在web应用中是比较常见的优化方式. OutputCacheAttr ...

  6. 微冷的雨ASP.NET MVC之葵花宝典(MVC)

    微冷的雨ASP.NET MVC之葵花宝典 By:微冷的雨 第一章 ASP.NET MVC的请求和处理机制. 在MVC中: 01.所有的请求都要归结到控制器(Controller)上. 02.约定优于配 ...

  7. ASP.NET MVC 从零开始 - 请求处理

    这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...

  8. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

  9. asp.net MVC之 自定义过滤器(Filter) - shuaixf

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...

随机推荐

  1. shell -Z- d等等代表

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [ -a FILE ]   ...

  2. 【转】mac终端安装node时候,显示“-bash: brew: command not found”,怎么解决?

    原文网址:https://segmentfault.com/q/1010000004221389/a-1020000004221408 mac终端安装node时候,显示“-bash: brew: co ...

  3. ECSHOP seo修改建议

    ECSHOP是一个非常优秀的商城程序,以丰富的模板.稳定开源.非常快的执行速度赢得广大网店主的青眯.可是新建站30多天,目前百度只收录了首页,而google收录正常.我检查了他的网站一切正常,没有任何 ...

  4. HDU 5429 Geometric Progression

    题意:给出一个大数数列,问是不是等比数列. 解法:拿java大数搞,注意全是0的情况也是Yes.我把公比用分数表示了,灰常麻烦,题解说只要判a[i - 1] * a[i + 1] == a[i] * ...

  5. DBHelper (支持事务与数据库变更) z

    using System; using System.Data; using System.Data.Common; using Project.BaseFramework; using System ...

  6. c#写入Mysql中文显示乱码 解决方法 z

    mysql字符集utf8,c#写入中文后,全部显示成?,一个汉字对应一个? 解决方法:在数据库连接字符串中增加字符集的说明,Charset=utf8,如 MySQLConnection con = n ...

  7. E asy Boo t 6.51 启动易 制作启动光盘的软件(附注册码)

    内建ISO文件生成器,可直接生成可启动ISO文件,并支持N合1优化. -------中文版注册码------- 用户名:中华人民共和国 注册码:2898-5448-5603-BB2D -------英 ...

  8. IOS color 颜色值比较

    /生成采样对照颜色(黑色) UIColor* sampleColor = [UIColor colorWithRed:(0/255.0f) green:(0/255.0f)  blue:(0/255. ...

  9. IOS NSNotificationCenter 通知的使用

    1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@" ...

  10. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...