MVC教程八:缓存过滤器

缓存过滤器用来输出页面缓存,其用法如下图所示:

注意:

Duration:表示缓存多少秒;VaryByParam:表示缓存是否随地址参数而改变。OutputCache除了可以定义在Action方法上面以外,还可以定义在控制器上面。

演示示例:

新建一个MVC应用程序,添加一个名为Cache的控制器,Cache控制器的Index方法里面将当前时间输出到页面中,Cache控制器定义如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace _2_缓存过滤器.Controllers
 8 {
 9     public class CacheController : Controller
10     {
11         [OutputCache(Duration =5,VaryByParam ="none")]
12         // GET: Cache
13         public ActionResult Index(int? id)
14         {
15             ViewData["CurrentTime"] = "现在的时间是:" + DateTime.Now;
16             return View();
17         }
18     }
19 }

2、Cache控制器的Index视图定义如下:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <h2>@ViewData["CurrentTime"]</h2>
    </div>
</body>
</html>

 3、程序运行结果

刷新页面的时候,只有时间过了5秒以后,页面上面显示的时间才会刷新。

如果把VaryByParam的值改为id,那么在5秒的时间范围内,页面显示的时间会随着id值的改变而改变,即只要id的值改变一次,页面显示的时间就会改变。

在MVC程序中使用缓存过滤器的时候,由于控制器的代码需要编译后才能发布,在发布之后,如果要修改缓存的策略,就很麻烦,这时可以采用如下图所示的方法,把缓存策略写在配置文件里面,这样即使在程序发布之后,我们也可以随时调整缓存的策略。

配置文件修改如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
    <!--缓存策略-->
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="cpfile" duration="5" varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
  </system.webServer>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=&quot;Web&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>

程序代码修改如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 
 7 namespace _2_缓存过滤器.Controllers
 8 {
 9     public class CacheController : Controller
10     {
11 
12         [OutputCache(CacheProfile = "cpfile")]
13         // GET: Cache
14         public ActionResult Index(int? id)
15         {
16             ViewData["CurrentTime"] = "现在的时间是:" + DateTime.Now;
17             return View();
18         }
19     }
20 }

运行结果和上面的结果一样。 

原文地址:https://www.cnblogs.com/dotnet261010/p/9005523.html