什么是Cache?

缓存在web应用中是一种以空间换去时间的技术,把频繁访问并且不经常变化的数据存储到内存中,以达到快速访问的目的。在web应用中是比较常见的优化方式。

OutputCacheAttribute

表示一个特性,该特性用于标记将缓存其输出的操作方法。

OutpuCacheAttribute定义

代码片段

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class OutputCacheAttribute : ActionFilterAttribute, IExceptionFilter

从上面的代码中可以看到该特性可以应用在类,方法上面。在mvc中,就可以直接在控制器上面或者控制器中的Action上面直接使用,做到细粒度的对缓存的控制。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;namespace Wolfy.OutputCacheDemo.Controllers
{[OutputCache(Duration = 10)]public class HomeController : Controller{// GET: Homepublic string Index(){return DateTime.Now.ToString();}}
}

上面的代码是将OutputCache特性标记在了控制器类上,以达到该控制器上所有的Action都将应用该特性,过期时间设置为10s。10s后缓存过期,再访问就会更新时间。

OutputCache特性也可以设置在Action方法上面,以达到更细粒度的控制缓存。

代码片段

    public class HomeController : Controller{[OutputCache(Duration = 10)]// GET: Homepublic string Index(){return DateTime.Now.ToString();}}

此时,只有Index的页面进行了缓存。

WebConfig

如果多个控制器或者Action使用相同的缓存配置,可以在配置文件中进行统一配置。

  <system.web><caching><outputCacheSettings><outputCacheProfiles ><add name='myoutputcache' duration='10'/></outputCacheProfiles></outputCacheSettings></caching><compilation debug="true" targetFramework="4.5"/><httpRuntime targetFramework="4.5"/></system.web>

应用名称为myoutputcache的缓存

    public class HomeController : Controller{[OutputCache(CacheProfile = "myoutputcache")]// GET: Homepublic string Index(){return DateTime.Now.ToString();}}

Note:

当控制器和Action同时使用了OutputCache特性时,以Action为主。

OutputCache参数

#region Assembly System.Web.Mvc.dll, v5.2.2.0
#endregionusing System;
using System.Web.UI;namespace System.Web.Mvc
{// Summary://     Represents an attribute that is used to mark an action method whose output//     will be cached.[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]public class OutputCacheAttribute : ActionFilterAttribute, IExceptionFilter{// Summary://     Initializes a new instance of the System.Web.Mvc.OutputCacheAttribute class.public OutputCacheAttribute();// Summary://     Gets or sets the cache profile name.//// Returns://     The cache profile name.public string CacheProfile { get; set; }//// Summary://     Gets or sets the child action cache.//// Returns://     The child action cache.public static System.Runtime.Caching.ObjectCache ChildActionCache { get; set; }//// Summary://     Gets or sets the cache duration, in seconds.//// Returns://     The cache duration.public int Duration { get; set; }//// Summary://     Gets or sets the location.//// Returns://     The location.public OutputCacheLocation Location { get; set; }//// Summary://     Gets or sets a value that indicates whether to store the cache.//// Returns://     true if the cache should be stored; otherwise, false.public bool NoStore { get; set; }//// Summary://     Gets or sets the SQL dependency.//// Returns://     The SQL dependency.public string SqlDependency { get; set; }//// Summary://     Gets or sets the vary-by-content encoding.//// Returns://     The vary-by-content encoding.public string VaryByContentEncoding { get; set; }//// Summary://     Gets or sets the vary-by-custom value.//// Returns://     The vary-by-custom value.public string VaryByCustom { get; set; }//// Summary://     Gets or sets the vary-by-header value.//// Returns://     The vary-by-header value.public string VaryByHeader { get; set; }//// Summary://     Gets or sets the vary-by-param value.//// Returns://     The vary-by-param value.public string VaryByParam { get; set; }// Summary://     Returns a value that indicates whether a child action cache is active.//// Parameters://   controllerContext://     The controller context.//// Returns://     true if the child action cache is active; otherwise, false.public static bool IsChildActionCacheActive(ControllerContext controllerContext);//// Summary://     This method is an implementation of System.Web.Mvc.IActionFilter.OnActionExecuted(System.Web.Mvc.ActionExecutedContext)//     and supports the ASP.NET MVC infrastructure. It is not intended to be used//     directly from your code.//// Parameters://   filterContext://     The filter context.public override void OnActionExecuted(ActionExecutedContext filterContext);//// Summary://     This method is an implementation of System.Web.Mvc.IActionFilter.OnActionExecuting(System.Web.Mvc.ActionExecutingContext)//     and supports the ASP.NET MVC infrastructure. It is not intended to be used//     directly from your code.//// Parameters://   filterContext://     The filter context.public override void OnActionExecuting(ActionExecutingContext filterContext);//// Summary://     This method is an implementation of System.Web.Mvc.IExceptionFilter.OnException(System.Web.Mvc.ExceptionContext)//     and supports the ASP.NET MVC infrastructure. It is not intended to be used//     directly from your code.//// Parameters://   filterContext://     The filter context.public void OnException(ExceptionContext filterContext);//// Summary://     This method is an implementation of System.Web.Mvc.IResultFilter.OnResultExecuted(System.Web.Mvc.ResultExecutedContext)//     and supports the ASP.NET MVC infrastructure. It is not intended to be used//     directly from your code.//// Parameters://   filterContext://     The filter context.public override void OnResultExecuted(ResultExecutedContext filterContext);//// Summary://     Called before the action result executes.//// Parameters://   filterContext://     The filter context, which encapsulates information for using System.Web.Mvc.AuthorizeAttribute.//// Exceptions://   System.ArgumentNullException://     The filterContext parameter is null.public override void OnResultExecuting(ResultExecutingContext filterContext);}
}

常用的属性

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

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

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

namespace System.Web.UI
{// Summary://     Specifies the valid values for controlling the location of the output-cached//     HTTP response for a resource.public enum OutputCacheLocation{// Summary://     The output cache can be located on the browser client (where the request//     originated), on a proxy server (or any other server) participating in the//     request, or on the server where the request was processed. This value corresponds//     to the System.Web.HttpCacheability.Public enumeration value.Any = 0,//// Summary://     The output cache is located on the browser client where the request originated.//     This value corresponds to the System.Web.HttpCacheability.Private enumeration//     value.Client = 1,//// Summary://     The output cache can be stored in any HTTP 1.1 cache-capable devices other//     than the origin server. This includes proxy servers and the client that made//     the request.Downstream = 2,//// Summary://     The output cache is located on the Web server where the request was processed.//     This value corresponds to the System.Web.HttpCacheability.Server enumeration//     value.Server = 3,//// Summary://     The output cache is disabled for the requested page. This value corresponds//     to the System.Web.HttpCacheability.NoCache enumeration value.None = 4,//// Summary://     The output cache can be stored only at the origin server or at the requesting//     client. Proxy servers are not allowed to cache the response. This value corresponds//     to the combination of the System.Web.HttpCacheability.Private and System.Web.HttpCacheability.Server//     enumeration values.ServerAndClient = 5,}
}

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

结语

由于没找到缓存依赖mysql的办法,关于OutputCache的内容就先到这里。

关于缓存依赖的内容,可参考这篇文章

http://www.cnblogs.com/iamlilinfeng/p/4419362.html

转载于:https://www.cnblogs.com/wolf-sun/p/6219245.html

[Asp.net mvc]OutputCacheAttribute相关推荐

  1. asp.net mvc 学习

    Routing讲解: http://www.cnblogs.com/wangiqngpei557/p/3379095.html Filter讲解: http://www.cnblogs.com/ymn ...

  2. AOP in Asp.net MVC

    不同的观点,开拓迥然不同的世界. Another point of view can sometimes open up a whole new world. ---- HSBC 本文内容包括: So ...

  3. ASP.NET MVC编程——控制器

    每一个请求都会经过控制器处理,控制器中的每个方法被称为控制器操作,它处理具体的请求. 1操作输入参数 控制器的操作的输入参数可以是内置类型也可以是自定义类型. 2操作返回结果 结果类型 调用方法 备注 ...

  4. ASP.NET MVC:缓存功能的设计及问题

    ASP.NET MVC:缓存功能的设计及问题 这是非常详尽的asp.net mvc中的outputcache 的使用文章. [原文:陈希章 http://www.cnblogs.com/chenxiz ...

  5. ASP.NET MVC Caching with OutputCache

    ASP.NET MVC Caching with OutputCache [原文:http://tech.pro/tutorial/1434/aspnet-mvc-caching-with-outpu ...

  6. 写自己的ASP.NET MVC框架(上)

     开始 ASP.NET程序的几种开发方式 介绍我的MVC框架 我的MVC框架设计架构 回忆以往AJAX的实现方式 MyMVC中实现AJAX的方式 如何使用MyMVC框架中的AJAX功能 配置MyM ...

  7. asp.net MVC之AuthorizeAttribute浅析

    AuthorizeAttribute是asp.net MVC的几大过滤器之一,俗称认证和授权过滤器,也就是判断登录与否,授权与否.当为某一个Controller或Action附加该特性时,没有登录或授 ...

  8. ASP.NET MVC 2示例Tailspin Travel

    Tailspin Travel 是一个旅游预订的应用程序示例,最新版本采用ASP.NET MVC 2技术构建,主要使用 DataAnnotations 验证, 客户端验证和ViewModels,还展示 ...

  9. Asp.net MVC中的ViewData与ViewBag

    在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...

最新文章

  1. android更换工具链
  2. 游族网络:已获得《三体》系列小说游戏开发、改编等权利
  3. 平均要取多少个(0,1)中的随机数才能让和超过1
  4. L1-041 寻找250 (10 分)—团体程序设计天梯赛
  5. VS2010 安装包打包(转)
  6. 用MATLAB解决实际数学问题,利用MATLAB解决高等数学问题.doc
  7. python代码转java工具_Python代码转为java代码?
  8. php网页怎么设置背景音乐,怎么给网页添加背景音乐
  9. c语言读取文件属性,Java File类(文件操作类)详解
  10. 软件开发方法 | 软件开发过程 辨析
  11. 大淘客cms源码修改二次开发
  12. assigning to rvalue解决解决
  13. 【程序源代码】小程序商城系统(CoreShop)
  14. 月份和星期的英语(请不要再弄错了)
  15. 可怕!一部手机失窃而揭露的黑色产业链
  16. 20万台空气净化机生产线商业计划书
  17. k8s pod OOMKilled 错误原因
  18. Adroid游戏开发实例讲解(五)-哄娃神器之随机五彩泡(附源码)
  19. 一个酒鬼有20美元,三美元可以买一瓶酒,三个空瓶子可以换一瓶酒
  20. 机器视觉 手部关键点检测(手部识别)安卓应用App(Hand Tracking)基于mediapipe。

热门文章

  1. 宇宙膨胀背后的故事(卅三):宇宙之有生于无
  2. ​忆阻器会成为“存储墙”的破局者么
  3. 【最新】三位深度学习创始人共同获得了2019年公布的图灵奖
  4. 信息哲学给哲学带来根本性革命了吗
  5. 杂谈 | 微软复兴,它与苹果竟有这么多相似之处!
  6. Yoshua Bengio团队通过在网络「隐藏空间」中使用降噪器以提高深度神经网络的「鲁棒性」...
  7. 腾讯云年度最强技术大会召开在即,这次只谈技术和代码
  8. 有点酸!中外程序员都是怎么炫富的? | 每日趣闻
  9. 如何和相亲对象无限聊天?程序员甩了这份架构图……| 每日趣闻
  10. 『Python Web框架之Django』第几节: AJAX