Caching in ASP.NET MVC

本文介绍了ASP.NET MVC应用中的四种缓存选项:请求范围缓存、用户范围缓存、ASP.NET缓存和输出缓存。详细解释了每种缓存的使用场景和配置方法,如如何在请求中存储数据、利用会话状态持久化数据、使用System.Web.Cache进行键值存储及缓存HTML输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值