MVC缓存

转:http://www.cnblogs.com/iamlilinfeng/p/4419362.html#t2

一、MVC缓存简介

缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期。在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法。 MVC缓存本质上还是.NET的一套缓存体系,只不过该缓存体系应用在了MVC框架上。下面的示例把缓存应用在MVC上。

缓存的常用场景:

数据被频繁的使用,并且很少发生变化或对即时性的要求不高。

二、Control缓存

Control缓存即是把缓存应用到整个Control上,该Control下的所有Action都会被缓存起来。Control缓存的粒度比较粗,应用也比较少些。

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MvcCache.Control.Controllers
 8 {
 9     [OutputCache(Duration = 10)]
10     public class ControlController : Controller
11     {
12         //
13         // GET: /Home/
14         public ActionResult Index()
15         {
16             ViewBag.CurrentTime = System.DateTime.Now;
17             return View();
18         }
19 
20         public ActionResult Index1()
21         {
22             ViewBag.CurrentTime = System.DateTime.Now;
23             return View();
24         }
25 
26     }
27 }
复制代码

在名为Control的Control中加入了OutputCache,并设置持续时间为10秒(Duration=10),即每10秒后过期当再次触发时更新缓存。下面是View中的代码,打印ViewBag的时间。

复制代码
 1 @{
 2     ViewBag.Title = "Index";
 3 }
 4 
 5 <h2>@ViewBag.CurrentTime</h2>
 6 
 7 @{
 8     ViewBag.Title = "Index1";
 9 }
10 
11 <h2>@ViewBag.CurrentTime</h2>
复制代码

三、Action缓存

即把缓存用到Action上,Action缓存为比较常用的缓存方式,该方式粒度细一些。使用方法类似Control缓存。

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace MvcCache.Control.Controllers
 8 {
 9     //Control不加缓存
10     public class ActionController : Controller
11     {
12         //该Index的Action加缓存
13         [OutputCache(Duration = 10)]
14         public ActionResult Index()
15         {
16             ViewBag.CurrentTime = System.DateTime.Now;
17             return View();
18         }
19 
20         //该Action不加缓存
21         public ActionResult Index1()
22         {
23             ViewBag.CurrentTime = System.DateTime.Now;
24             return View();
25         }
26 
27     }
28 }
复制代码

Index加入了缓存,而Index1没有加。此时Index1每次刷新页面都会取到当前的时间并打印。

复制代码
 1 @{
 2     ViewBag.Title = "Index";
 3  
 4 }
 5 
 6 <h2>@ViewBag.CurrentTime</h2>
 7 
 8 @{
 9     ViewBag.Title = "Index1";
10 }
11 
12 <h2>@ViewBag.CurrentTime</h2>
复制代码

四、使用配置文件

当我们需要将N个Control或Action加入缓存,并且缓存的参数是一致的情况下,我们可以把相关的设置放到Web.config中,并在程序中加入相应的配置。

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--
 3   For more information on how to configure your ASP.NET application, please visit
 4   http://go.microsoft.com/fwlink/?LinkId=169433
 5   -->
 6 
 7 <configuration>
 8   <appSettings>
 9     <add key="webpages:Version" value="2.0.0.0" />
10     <add key="webpages:Enabled" value="false" />
11     <add key="PreserveLoginUrl" value="true" />
12     <add key="ClientValidationEnabled" value="true" />
13     <add key="UnobtrusiveJavaScriptEnabled" value="true" />
14   </appSettings>
15   <system.web>
16     <!--配置缓存-->
17     <caching>
18       <outputCacheSettings>
19         <outputCacheProfiles>
20           <add name="TestConfigCache" duration="10"/>
21         </outputCacheProfiles>
22       </outputCacheSettings>
23     </caching>
24     <!--配置缓存-->
25     <httpRuntime targetFramework="4.5" />
26     <compilation debug="true" targetFramework="4.5" />
27     <pages>
28       <namespaces>
29         <add namespace="System.Web.Helpers" />
30         <add namespace="System.Web.Mvc" />
31         <add namespace="System.Web.Mvc.Ajax" />
32         <add namespace="System.Web.Mvc.Html" />
33         <add namespace="System.Web.Routing" />
34         <add namespace="System.Web.WebPages" />
35       </namespaces>
36     </pages>
37   </system.web>
38   <system.webServer>
39     <validation validateIntegratedModeConfiguration="false" />
40 
41     <handlers>
42       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
43       <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
44       <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
45       <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
46       <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition

转载于:https://www.cnblogs.com/love201314/p/4995492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值