//OutputCache是設置緩存,參數Duration設置緩存的過時時間,OutputCache能夠加到Controller上,也能夠加到Action上,可是當Controller與Action都應用了OutputCache時,以Action的OutputCache爲主。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MvcApplication1.Controllers 8 { 9 [OutputCache(Duration = 10)] 10 public class ControlController : Controller 11 { 12 // 13 // GET: /Control/ 14 [OutputCache(Duration = 20)] 15 public ActionResult Index() 16 { 17 ViewBag.Text = System.DateTime.Now; 18 return View(); 19 } 20 public ActionResult Index2() 21 { 22 ViewBag.Text = System.DateTime.Now; 23 return View(); 24 } 25 } 26 }
當多個Controller或者Action都要設置緩存,而且緩存的數據都是一致的時候,能夠把對緩存的配置寫到web.config的system.web節點下
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="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 47 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 48 </handlers> 49 </system.webServer> 50 51 </configuration>
取web.config中的配置web
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 public class ConfigController : Controller 10 { 11 //TestConfigCache爲在配置文件中配置的緩存節 12 [OutputCache(CacheProfile = "TestConfigCache")] 13 public ActionResult Index() 14 { 15 ViewBag.CurrentTime = System.DateTime.Now; 16 return View(); 17 } 18 19 } 20 }
OutputCache的經常使用屬性api
1)CacheProfile:緩存使用的配置文件的緩存名稱。瀏覽器
2)Duration:緩存時間,以秒爲單位,這個除非你的Location=None,能夠不添加此屬性,其他時候都是必須的。緩存
3)OutputCacheLocation:枚舉類型,緩存的位置。當設置成None時,全部緩存將失效,默認爲Any。服務器
Any:頁面被緩存在瀏覽器、代理服務器端和web服務器端;app
Client:緩存在瀏覽器;less
DownStream:頁面被緩存在瀏覽器和任何的代理服務器端;spa
Server:頁面被緩存在Web服務器端;debug
None:頁面不緩存;代理
ServerAndClient:頁面被緩存在瀏覽器和web服務器端;
4)VaryByParam:用於多個輸出緩存的字符串列表,並以分號進行分隔。默認時,該字符串與GET方法傳遞的參數或與POST方法傳遞的變量相對應。當被設置爲多個參數時,輸出緩存將會爲每一個參數都準備一個與之相對應的文檔版本。可能值包括none,*,以及任何有效的查詢串或POST參數名稱。