Caching in ASP.NET MVC
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的更多相关文章
- 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面
Donut Caching是缓存除了部分内容以外的整个页面的最好的方式,在它出现之前,我们使用"输出缓存"来缓存整个页面. 何时使用Donut Caching 假设你有一个应用程序 ...
- ASP.NET MVC 缓存扩展 - Donut Caching
项目介绍 ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code ...
- asp.net mvc Partial OutputCache 在SpaceBuilder中的应用实践
最近给SpaceBuilder增加OutputCache 时发现了一些问题,贴在这做个备忘,也方便遇到类似问题的朋友查阅. 目前SpaceBuilder表现层使用是asp.net mvc v1.0,使 ...
- asp.net MVC 应用程序的生命周期
下面这篇文章总结了 asp.net MVC 框架程序的生命周期.觉得写得不错,故转载一下. 转载自:http://www.cnblogs.com/yplong/p/5582576.html ...
- [Asp.net mvc]OutputCacheAttribute
什么是Cache? 缓存在web应用中是一种以空间换去时间的技术,把频繁访问并且不经常变化的数据存储到内存中,以达到快速访问的目的.在web应用中是比较常见的优化方式. OutputCacheAttr ...
- 微冷的雨ASP.NET MVC之葵花宝典(MVC)
微冷的雨ASP.NET MVC之葵花宝典 By:微冷的雨 第一章 ASP.NET MVC的请求和处理机制. 在MVC中: 01.所有的请求都要归结到控制器(Controller)上. 02.约定优于配 ...
- ASP.NET MVC 从零开始 - 请求处理
这篇文章是从我的 github 博客 lxconan.github.io 导入的. 这是这个系列的第三篇了.前两篇文章请参见: ASP.NET MVC 从零开始 - Create and Run AS ...
- 17+个ASP.NET MVC扩展点【附源码】
1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig. 在自定义的Http ...
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
随机推荐
- dos文件批量转换成unix文件
对于经常在windows环境下和linux环境同时使用的文件(如在windows系统下编写,在linux环境下编译的文件), 常常存在这样的问题:由于两种系统的格式文件格式不同,导致程序出现不期望的问 ...
- 【应聘】阿里巴巴Java面试题目
原文地址:http://blog.csdn.net/free0sky/article/details/7927275 一.String,StringBuffer, StringBuilder 的区 ...
- 常用的css的技巧
1.在做项目当中,由静态页面来载入到项目中,作为动态数据的部分,若是这个动态数据,前面或者后面有需要图片显示(图片是用background来显示的),一般不用float:left或者right,而是p ...
- Oracle的OracleBulkCopy不支持事务处理
在进行OracleBulkCopy批量数据导入的过程中使用事务后抛出了异常, 没使用事务时可以正确批量导入, ORA-12154:无法解析指定的连接字符串, 但是TNS配置肯定是没有错的, 难道是Co ...
- 硬盘结构介绍--mbr及分区
硬盘刚买来使用时需要经过分区然后格式化才能够使用,硬盘经过分区后,分区软件便会写一个主引导扇区,这个扇区位于硬盘的 0 磁道 0 柱面第1扇区(即0区)(注意:该扇区为隐含扇区,0道0面的全部扇区均为 ...
- 【转】匹配dll(exe)和pdb方法
1. 静态检查windbg 调试工具包中有一个工具symchk.exe, 选项很多, 下面一个简单的用法可以检查一个 test.exe能不能找到与它匹配的PDB: 这是成功的情形. 下面来个失败的作为 ...
- Linux基本命令(4)有关关机和查看系统信息的命令
有关关机和查看系统信息的命令 命令 说明 shutdown 正常关机 reboot 重启计算机 ps 查看目前程序执行的情况 top 查看目前程序执行的情景和内存使用的情况 kill 终止一个进程 d ...
- 【windows核心编程】IO完成端口(IOCP)复制文件小例
1.演示内容 文件复制 2.提要 复制大文件时,使用FILE_FLAG_NO_BUFFERING标志 同时需要注意: 读写文件的偏移地址为 磁盘扇区 的整数倍 读写文件的字节数为 磁盘扇区 的整数倍 ...
- springmvc里面的中文乱码问题
如果是以get方法提交的表单,则可以在comcat服务器的server.xml文件里面设置 <Connector connectionTimeout="20000" port ...
- [Hive - Tutorial] Querying and Inserting Data 查询和插入数据
Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...