什么是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 = )]
public class HomeController : Controller
{
// GET: Home
public string Index()
{
return DateTime.Now.ToString();
}
}
}

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

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

代码片段

    public class HomeController : Controller
{
[OutputCache(Duration = )]
// GET: Home
public 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: Home
public string Index()
{
return DateTime.Now.ToString();
}
}

Note:

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

OutputCache参数

#region Assembly System.Web.Mvc.dll, v5.2.2.0
#endregion using 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 = ,
//
// 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 = ,
//
// 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 = ,
//
// 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 = ,
//
// Summary:
// The output cache is disabled for the requested page. This value corresponds
// to the System.Web.HttpCacheability.NoCache enumeration value.
None = ,
//
// 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 = ,
}
}

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

结语

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

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

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

[Asp.net mvc]OutputCacheAttribute的更多相关文章

  1. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】

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

  2. 17+个ASP.NET MVC扩展点【附源码】

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.  在自定义的Http ...

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

    http://www.cnblogs.com/fish-li/archive/2012/02/12/2348395.html 阅读目录 开始 ASP.NET程序的几种开发方式 介绍我的MVC框架 我的 ...

  4. ASP.NET MVC 监控诊断、本地化和缓存

    这篇博客主要是针对asp.net mvc项目的一些常用的东东做一个讲解,他们分别是监控诊断.本地化和缓存.虽然前两者跟asp.net mvc看上去好像是没什么关联. 但其实如果真正需要做asp.net ...

  5. ASP.NET MVC 3 之表单和 HTML 辅助方法(摘抄)

    ——选自<ASP.NET MVC3 高级编程(第5章) 孙远帅 译> 第5章 表单和HTML辅助方法 本章内容简介: * 理解表单 * 如何利用HTML辅助方法 * 编辑和输入的辅助方法 ...

  6. 17+个ASP.NET MVC扩展点,含源码{转}

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.在自定义的HttpMo ...

  7. ASP.NET MVC 过滤器开发与使用

    ASP.NET MVC 中给我们提供了内置的过滤器,通过过滤器,我们可以在控制器内的方法前后,添加必须的业务逻辑,如权限验证,身份验证,错误处理等. 今天,我们主要介绍3个过滤器:OutputCach ...

  8. [引]ASP.NET MVC 4 Content Map

    本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...

  9. ASP.NET MVC 缓存使用示例

    应该说,缓存的设计是一门较为复杂的学问,主要考虑的问题包括:要不要缓存?要缓存哪些数据?要缓存多少数据?要缓存多久?如何更新缓存(手动还是自 动)?将缓存放在哪里?本文将以较为通俗易懂的方式,来看一看 ...

随机推荐

  1. Linux系统下面挂载u盘

    1.先插好u盘到Linux服务器,然后查看u盘挂载到哪个目录下面. [root@localhost ~]# /sbin/fdisk -l 2.挂载到u目录下面 [root@localhost ~]# ...

  2. 萌新笔记——linux下(ubuntu)反删除(误删恢复)与回收站制作

    刚刚有个小伙伴不小心删了他写了好几的天代码,为他心疼之余帮他找回了文件. 想到我之前也常常误删一些文件,就干脆分享一下我的反删除方法,并说说我做的回收站(好low的,求大神指点) 首先是反删除软件ex ...

  3. 项目游戏开发日记 No.0x000005

    14软二杨近星(2014551622) 还有一周就要交项目了, 看着周围的人也都忙碌了起来, 看着大部分人的项目都已经初具容貌, 我们团队里面也搞得人心惶惶, 一来是, 时间不多了, 还有很多事情要做 ...

  4. java的JSP技术

    java的JSP技术 [toc] 1.JSP简介 Jsp技术是用来开发java web的页面显示的,所有MVC模型里面的视图层,所以视图层的开发 jsp不是编程语言,三个英文是java server ...

  5. 如何用fiddler对ios抓包

    fiddler端设置:(配置好重启fiddler) 1.首先下载安装fiddler,我安装的是fiddler4 2.Tools->Telerik Fiddler Options->HTTP ...

  6. java.lang.OutOfMemoryError: PermGen space及其解决方法

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决 ...

  7. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  8. java 过滤器

    1.什么是过滤器?  过滤信息,执行的优先级高于 servlet 2.自定义过滤器的步骤 写一个普通java类,实现Filter接口 2.配置过滤器  web.xm中 import java.io.I ...

  9. 快速排序中的partition函数的枢纽元选择,代码细节,以及其标准实现

    很多笔试面试都喜欢考察快排,叫你手写一个也不是啥事.我很早之前就学了这个,对快速排序的过程是很清楚的.但是最近自己尝试手写,发现之前对算法的细节把握不够精准,很多地方甚至只是大脑中的一个映像,而没有理 ...

  10. DirectWrite 模糊问题——如何正确根据DIP计算实际像素

    使用Windows.Graphics.Display.DisplayInformation的时候,一定要根据RawPixelsPerViewPixel计算,而不是RawDpiX或RawDpiY,或许L ...