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

注意:

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

演示示例:

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace _2_缓存过滤器.Controllers
{
public class CacheController : Controller
{
[OutputCache(Duration =,VaryByParam ="none")]
// GET: Cache
public ActionResult Index(int? id)
{
ViewData["CurrentTime"] = "现在的时间是:" + DateTime.Now;
return View();
}
}
}

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>

程序代码修改如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace _2_缓存过滤器.Controllers
{
public class CacheController : Controller
{ [OutputCache(CacheProfile = "cpfile")]
// GET: Cache
public ActionResult Index(int? id)
{
ViewData["CurrentTime"] = "现在的时间是:" + DateTime.Now;
return View();
}
}
}

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

MVC教程八:缓存过滤器的更多相关文章

  1. MVC教程:授权过滤器

    一.过滤器 过滤器(Filter)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,并不是每个请求都会响应内容,只有那些有特定权限的用户才能响应特定的内容.过滤器理论上 ...

  2. MVC教程八:母版页(布局页)视图

    一.母版页介绍和使用 母版页的扩展名为".cshtml",也叫做视图布局页,它相当于网页的模板.在其他网页中,只要引用了母版页,母版页的页面内容就可以自动显示出来,设计者可以修改引 ...

  3. ASP.NET MVC教程八:_ViewStart.cshtml

    一.引言 _ViewStart.cshtml是在ASP.NET MVC 3.0及更高版本以后出现的,用Razor模板引擎新建项目后,Views目录下面会出现一个这样的文件: 打开_ViewStart. ...

  4. 2017.3.31 spring mvc教程(八) <mvc:annotation-driven />所做的工作

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  5. 自学MVC看这里——全网最全ASP.NET MVC 教程汇总

    MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MVC技术的学习者提供一个整合学习入口.本文从 ...

  6. ASP.NET MVC 教程汇总

    自学MVC看这里——全网最全ASP.NET MVC 教程汇总   MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要 ...

  7. 【转】ASP.NET MVC教程

    转自:http://www.cnblogs.com/QLeelulu/category/123326.html ASP.NET MVC的最佳实践与性能优化的文章 摘要: 就一些文章链接,就不多废话了. ...

  8. 全网最全ASP.NET MVC 教程汇总

    全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MV ...

  9. 自学MVC看这里——全网最全ASP.NET MVC 教程汇总(转)

    自学MVC看这里——全网最全ASP.NET MVC 教程汇总   MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要 ...

随机推荐

  1. 物联网将在2018年实现大规模发展--IBM的四大预测

    物联网将在2018年实现大规模发展--IBM的四大预测    数据是数字化变革的基本组成部分,物联网.人工智能.区块链.边缘计算等技术预计将在来年掀起巨浪, 因为这些技术是收集.分析和存储信息的方法. ...

  2. 学习Git---20分钟git快速上手

    学习Git-----20分钟git快速上手  在Git如日中天的今天,不懂git都不好意思跟人说自己是程序猿.你是不是早就跃跃欲试了,只是苦于没有借口(契机). 好吧,机会就在今天. 给我20分钟,是 ...

  3. windows如何查看某个端口被谁占用

    我们在启动应用的时候经常发现我们需要使用的端口被别的程序占用,但是我们又不知道是被谁占用,这时候我们需要找出“真凶”,如何做到呢? cmd命令中 输入命令:netstat -ano,列出所有端口的情况 ...

  4. Feign请求中报错:Request header is too large 的解决方案。

    现在我们项目中都已迁入spring boot和spring cloud. 服务间调用现在都改成feign的调用方式,但是上次在实际使用过程中出现过:Request header is too larg ...

  5. 关于 NSInvocation

    Invocation   调用的意思. 可想而知NSInvocation 是一个 方法调用 封装的类. 这体现了  面向对象的思想, 及一切皆对象.函数也不例外. 一般编程中,应该很少用到这个. 但是 ...

  6. 好的 IOS 学习网站

    http://www.objc.io/contributors.html codeproject. http://www.codeproject.com/KB/iPhone/

  7. Linux内核设计基础(一)之中断处理

    假设让内核定期对设备进行轮询.以便处理设备,那会做非常多无用功,假设能让设备在须要内核时主动通知内核,会是一个聪明的方式,这便是中断. 在响应一个特定中断时,内核会运行一个函数--中断处理程序. 中断 ...

  8. 每日英语:Pediatricians Set Limits on Screen Time

    Parents should ban electronic media during mealtimes and after bedtime as part of a comprehensive 'f ...

  9. git(8):常用命令

    Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v ...

  10. iOS应用管理(优化)

    // //  ViewController.m //  01-应用管理 //  Created by apple on 17-5-14. //  Copyright (c) 2017年  All ri ...