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. Oracle 手工清除回滚段的几种方法

    关于回滚段的问题,之前也小整理过一个,参考: Current online Redo 和 Undo 损坏的处理方法 http://blog.csdn.net/tianlesoftware/articl ...

  2. TCP/IP详解学习笔记(9)-TCP协议概述

    终于看到了TCP协议,这是TCP/IP详解里面最重要也是最精彩的部分,要花大力气来读.前面的TFTP和BOOTP都是一些简单的协议,就不写笔记了,写起来也没啥东西. TCP和UDP处在同一层---运输 ...

  3. 组以逗号分隔的子串及跨平update join

    下列语句可以对组以逗号分隔的子串 set @device_cd_array += ', ' set @device_cd_array += @nodeid ,, '') update时要join表要先 ...

  4. Java 小片段

    public static String listToString(List<String> stringList){ if (stringList==null) { return nul ...

  5. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  6. Storm入门教程 第二章 构建Topology[转]

    2.1 Storm基本概念 在运行一个Storm任务之前,需要了解一些概念: Topologies Streams Spouts Bolts Stream groupings Reliability ...

  7. 《Python CookBook2》 第四章 Python技巧 - 若列表中某元素存在则返回之 && 在无须共享引用的条件下创建列表的列表

    若列表中某元素存在则返回之 任务: 你有一个列表L,还有一个索引号i,若i是有效索引时,返回L[i],若不是,则返回默认值v 解决方案: 列表支持双向索引,所以i可以为负数 >>> ...

  8. 完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog

    完全参照系统自带的DatePickerDialog.TimePickerDialog的源代码仿写的DateTimePickerDialog.具有同时选择日期.时间的功能.在2.2.2.3平台,显示的效 ...

  9. html 5新特性 --用SVG绘制的微信logo

    一个简单的SVG绘制图片的小案例. 效果图: 代码如下: <style> * { ; ; } body { background-color: #d5d3d4; } .container ...

  10. 数往知来C#面向对象准备〈二〉

    面向对象(OOP→Object-Oriented Programming) 1.什么是面向对象? 一种分析问题的方式. 2.面向对象三大特征: 封装(隐蔽代码实现/复用/修改方便).继承.多态. 3. ...