[转载]MVC3缓存:使用页面缓存
2014-01-12 21:15
323 查看
在以前的WebForm的开发中,在页面的头部加上OutputCache即可启用页面缓存,而在MVC3中,使用了Razor模板引擎的话,该如何使用页面缓存呢?
如何启用
在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可。
我们建一个Demo来测试一下,在此Demo中,在View的Home目录下的Index.cshtml中让页面输入当前的时间。
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
<h2>
现在时间:@DateTime.Now.ToString("T")</h2>
</div>
</body>
</html>
在Controllers中添加对应的Action,并加上OutputCache属性。
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = 5, VaryByParam = "none")]
public ActionResult Index()
{
return View();
}
}
刷新页面即可看到页面做了一个10秒的缓存。当页面中数据不是需要实时的呈现给用户时,这样的页面缓存可以减小实时地对数据处理和请求,当然这是针对整个页面做的缓存,缓存的粒度还是比较粗的。
缓存的位置
可以通过设置缓存的Location属性,决定将缓存放置在何处。
Location可以设置的属性如下:
· Any
· Client
· Downstream
· Server
· None
· ServerAndClient
Location的默认值为Any。一般推荐将用户侧的信息存储在Client端,一些公用的信息存储在Server端。
加上Location应该像这样。
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = 5, VaryByParam = "none", Location = OutputCacheLocation.Client, NoStore = true)]
public ActionResult Index()
{
return View();
}
}
缓存依赖
VaryByParam可以对缓存设置缓存依赖条件,如一个产品详细页面,可能就是根据产品ID进行缓存页面。
缓存依赖应该设置成下面这样。
在MVC3中对输出缓存进行了改进,OutputCache不需要手动指定VaryByParam,会自动使用Action的参数作为缓存过期条件。(感谢”散客游“提醒)
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Index()
{
return View();
}
}
另一种通用的设置方法
当我们需要对多个Action进行统一的设置时,可以在web.config文件中统一配置后进行应用即可。
在web.config中配置下Caching节点
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache1Hour" duration="3600" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
那么在Action上使用该配置节点即可,这样的方法对于统一管理配置信息比较方便。
[HandleError]
public class HomeController : Controller
{
[OutputCache(CacheProfile = "Cache1Hour")]
public ActionResult Index()
{
return View();
}
}
原文地址:http://blog.sina.com.cn/s/blog_55e42da601011lyg.html
如何启用
在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可。
我们建一个Demo来测试一下,在此Demo中,在View的Home目录下的Index.cshtml中让页面输入当前的时间。
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
<h2>
现在时间:@DateTime.Now.ToString("T")</h2>
</div>
</body>
</html>
在Controllers中添加对应的Action,并加上OutputCache属性。
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = 5, VaryByParam = "none")]
public ActionResult Index()
{
return View();
}
}
刷新页面即可看到页面做了一个10秒的缓存。当页面中数据不是需要实时的呈现给用户时,这样的页面缓存可以减小实时地对数据处理和请求,当然这是针对整个页面做的缓存,缓存的粒度还是比较粗的。
缓存的位置
可以通过设置缓存的Location属性,决定将缓存放置在何处。
Location可以设置的属性如下:
· Any
· Client
· Downstream
· Server
· None
· ServerAndClient
Location的默认值为Any。一般推荐将用户侧的信息存储在Client端,一些公用的信息存储在Server端。
加上Location应该像这样。
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = 5, VaryByParam = "none", Location = OutputCacheLocation.Client, NoStore = true)]
public ActionResult Index()
{
return View();
}
}
缓存依赖
VaryByParam可以对缓存设置缓存依赖条件,如一个产品详细页面,可能就是根据产品ID进行缓存页面。
缓存依赖应该设置成下面这样。
在MVC3中对输出缓存进行了改进,OutputCache不需要手动指定VaryByParam,会自动使用Action的参数作为缓存过期条件。(感谢”散客游“提醒)
[HandleError]
public class HomeController : Controller
{
[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Index()
{
return View();
}
}
另一种通用的设置方法
当我们需要对多个Action进行统一的设置时,可以在web.config文件中统一配置后进行应用即可。
在web.config中配置下Caching节点
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache1Hour" duration="3600" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
那么在Action上使用该配置节点即可,这样的方法对于统一管理配置信息比较方便。
[HandleError]
public class HomeController : Controller
{
[OutputCache(CacheProfile = "Cache1Hour")]
public ActionResult Index()
{
return View();
}
}
原文地址:http://blog.sina.com.cn/s/blog_55e42da601011lyg.html
相关文章推荐
- MVC3缓存之一:使用页面缓存
- ASP.NET MVC中使用OutputCache实现服务器端页面级缓存注意问题
- ASP.NET MVC 3 使用页面缓存 OutputCache 需要注意的问题
- Ehcache 整合Spring 使用页面、对象缓存(转载)
- 使用Donut Caching和Donut Hole Caching在ASP.NET MVC应用中缓存页面
- [转]MVC3缓存之一:使用页面缓存
- 勤于思考: ASP.NET MVC 注销后 使用浏览器 【后退】 不使用缓存页面
- MVC3缓存:使用页面缓存
- ASP.NET MVC3缓存之一:使用页面缓存
- WebView打开网页,设置支持Javascript,浏览的网页后退而不是退出浏览器,判断页面加载过程,缓存的使用
- 转载留用 使用jquery 扩展 实现在页面鼠标光标指定位置后面加字符
- Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存
- 艾伟_转载:浅析ASP.NET页面缓存的几点体会
- 如何不允许用户使用浏览器的『后退按钮』查看前面缓存下来的页面。
- 使用MVC模型的几个常见误区 【转载】Li XianJing
- Substitution 使用方法 页面缓存时的局部刷新
- MVC使用脚本刷新部分页面
- MVC缓存(转载)
- 【转载】MVC使用HandleErrorAttribute自定义异常
- 大叔手记(20):ASP.NET MVC中使用jQuery时的浏览器缓存问题