博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Caching in ASP.NET MVC
阅读量:6633 次
发布时间:2019-06-25

本文共 2520 字,大约阅读时间需要 8 分钟。

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 sessionHttpContext.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=60, 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 = 3600, 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
(id); return View("Details", auction);}

 

 

转载于:https://www.cnblogs.com/davidgu/p/3331699.html

你可能感兴趣的文章
Swift中正则使用正则的几种方式
查看>>
SQL Server 2000 : gethostbyname: Error 11004
查看>>
log4j下载地址及日志文件输入位置配置
查看>>
Tomcat下Servlet配置精解
查看>>
吞吐量与网络流量对应关系剖析
查看>>
新功能:OSS访问日志实时分析
查看>>
在DELL服务器上升级ESXI 5.5
查看>>
ubuntu16.04 双网卡绑定
查看>>
LVS+Keepalived实现高可用群集
查看>>
单目运算符重载为友元函数
查看>>
Vmware vSphere常见问题汇总(三)
查看>>
基于OHCI的USB主机 —— USB设备端口特性清除
查看>>
微软宣布MIX11将发布Silverlight 5 Beta
查看>>
2个sqlserver数据库实例之间数据导入导出
查看>>
Oracle数据库安全资源-2
查看>>
大牛教你查库暴库
查看>>
[推荐]在线测试你的网速
查看>>
Android错误:java.lang.ExceptionInInitializerError,java.lang.UnsatisfiedLinkError,
查看>>
Oracle LOCK内部机制及最佳实践系列(五)给出一个导致死锁的SQL示例
查看>>
storm写redis问题小结
查看>>