ASP.NET MVC——OutputCache

示例程序
1.在Action上标记启用缓存,如:[OutputCache(Duration = 5, VaryByParam = 'none')]
2.在Action上标记禁用本地缓存,如:[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
3.在Action上标记使用缓存依赖,如:[OutputCache(Duration = 9999, VaryByParam = 'none', SqlDependency ='Demo:UserInfo')]
使用缓存依赖的要点
1.在Web.config中添加数据库连接字符串,如下:
<connectionStrings>
..<add name="DemoConn" connectionString="server=.\SQLEXPRESS;database=Demo;uid=sa;pwd=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>
2.在Web.config中添加缓存配置,如下:
<caching>
..<sqlCacheDependency enabled="true" pollTime="2000">
....<databases>
......<add name="Demo" connectionStringName="DemoConn" />
....</databases>
..</sqlCacheDependency>
</caching>
3.在Global.asax.cs中以编程的方式启用缓存依赖,如下:
protected void Application_Start()
{
..string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["DemoConn"].ConnectionString;
..System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr);
..System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, "UserInfo");
}
4.使用命令行注册依赖,如下:
aspnet_regsql -S .\sqlexpress -E -d DBName -ed
aspnet_regsql -S .\sqlexpress -E -d DBName -t TableName -et
示例如下:
[HandleError]

public class CacheHandlerController : Controller

{

public ActionResult Index()

{

return View();

}



[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]

public ActionResult RenderWithOutCache()

{

ViewData["Now"] = DateTime.Now;

return View("RenderCache");

}



[OutputCache(Duration = 5, VaryByParam = "none")]

public ActionResult RenderCache()

{

ViewData["Now"] = DateTime.Now;

return View();

}



[OutputCache(Duration = 9999, VaryByParam = "none", SqlDependency = "Demo:UserInfo")]

public ActionResult SqlCacheDependency()

{

ViewData["Now"] = DateTime.Now;

return View("RenderCache");

}

}

自定义过滤器 
1.ASP.Net MVC内置四类过滤器分别为: IAuthorizationFilter IActionFilter IResultFilter IExceptionFilter
2.关于过滤器的说明上述过滤器在使用上存在优先级,且依序从高到低可通过实现对应的接口创建自定义的过滤器
3.自定义过滤器,示例(...)
示例如下
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]

public class TraceAttribute : FilterAttribute, IActionFilter, IResultFilter

{

#region Properties

public string LogName { get; set; }

#endregion


#region IActionFilter Members


public void OnActionExecuting(ActionExecutingContext filterContext)

{

LogMessage(

filterContext.RouteData.Values["controller"].ToString(),

filterContext.RouteData.Values["action"].ToString(),

"Action executing..."

);

}



public void OnActionExecuted(ActionExecutedContext filterContext)

{

LogMessage(

filterContext.RouteData.Values["controller"].ToString(),

filterContext.RouteData.Values["action"].ToString(),

"Action executed."

);

}

#endregion


#region IResultFilter Members


public void OnResultExecuting(ResultExecutingContext filterContext)

{

LogMessage(

filterContext.RouteData.Values["controller"].ToString(),

filterContext.RouteData.Values["action"].ToString(),

"Result executing..."

);

}



public void OnResultExecuted(ResultExecutedContext filterContext)

{

LogMessage(

filterContext.RouteData.Values["controller"].ToString(),

filterContext.RouteData.Values["action"].ToString(),

"Result executed."

);

}


#endregion


#region Helper methods


private void LogMessage(string controller, string action, string message)

{

......

}


#endregion

}
posted @ 2010-12-05 03:30  悲伤的第七音  Views(1274)  Comments(0Edit  收藏  举报