分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

MVC缓存(一)

发布时间:2023-09-06 01:35责任编辑:傅花花关键词:MVC缓存
//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 : Controller11 ????{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中的配置

 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 : Controller10 ????{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的常用属性

1)CacheProfile:缓存使用的配置文件的缓存名称。

2)Duration:缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的。

3)OutputCacheLocation:枚举类型,缓存的位置。当设置成None时,所有缓存将失效,默认为Any。

    Any:页面被缓存在浏览器、代理服务器端和web服务器端;

   Client:缓存在浏览器;

   DownStream:页面被缓存在浏览器和任何的代理服务器端;

   Server:页面被缓存在Web服务器端;

   None:页面不缓存;

   ServerAndClient:页面被缓存在浏览器和web服务器端;

4)VaryByParam:用于多个输出缓存的字符串列表,并以分号进行分隔。默认时,该字符串与GET方法传递的参数或与POST方法传递的变量相对应。当被设置为多个参数时,输出缓存将会为每个参数都准备一个与之相对应的文档版本。可能值包括none,*,以及任何有效的查询串或POST参数名称。

MVC缓存(一)

原文地址:https://www.cnblogs.com/caijiabao/p/8257012.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved