[转]MVC3缓存之一:使用页面缓存

本文转自:http://www.cnblogs.com/parry/archive/2011/03/19/OutputCache_In_MVC3.html

在以前的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();     }

}

时间: 2024-08-06 11:41:55

[转]MVC3缓存之一:使用页面缓存的相关文章

如何设置页面缓存或不用页面缓存

一.设置页面缓存 1.直接在页面上用<%@ OutputCache Duration="10" VaryByParam="None" %>声明来缓存页面 2.使用服务端方法: //将Cache-Control标头设置为HttpCacheAbility值 Response.Cache.SetCacheability(HttpCacheability.Public); //将页面的绝对过期时间 Response.Cache.SetExpires(DateTi

Yii的缓存机制之页面缓存

页面缓存是不能通过片段缓存来实现的,因为布局和内容不能同时缓存.只能通过过滤器来生成缓存. 实现方法: 在控制器里使用过滤器来实现 function filters (){ return array( array( 'system.web.widgets.COutputCache + detail', //只针对detail有效 'duration' => 3600, //缓存时间 'varyByParam' => array('id'), //缓存依赖(通过不同的id实现不同的缓存) ),

MVC3缓存:使用页面缓存

在以前的WebForm的开发中,在页面的头部加上OutputCache即可启用页面缓存,而在MVC3中,使用了Razor模板引擎的话,该如何使用页面缓存呢?如何启用另一个参考的博客地址:http://www.cnblogs.com/luminji/archive/2011/09/14/2174751.html在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中,在View的Home目录下的Index.

探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)

其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.html#mvc 本章Demo:https://github.com/dunitian/LoTCodeBase/blob/master/NetCode/6.网页基础/BMVC5/MVC5Base/Controllers/CacheController.cs 这次来篇放松的,咱们不要老是说安全相关的东西.

缓存插件 EHCache 页面缓存CachingFilter

Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.getInstance(); // 或者 cacheManager = CacheManager.create("/config/ehcache.xml"); // 或者 cacheManager = CacheManager.create("http://localhost:8080

Drupal是如何避免页面缓存保存Message信息的

函数page_get_cache代码: function page_get_cache($status_only = FALSE) { static $status = FALSE; global $user, $base_root; if ($status_only) { return $status; } $cache = NULL; if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(d

MVC3缓存之一:使用页面缓存

MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中,在View的Home目录下的Index.cshtml中让页面输入当前的时间. [csharp] view plain copy @{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Index</title>

MVC3缓存(一):页面缓存

启用页面缓存 在MVC3中如果要启用页面缓存,需要在页面对应的Action前面加上一个OutputCache属性. @{ ViewBag.Title = "主页"; } <!DOCTYPE html> <html> <head> <title>页面缓存</title> </head> <body> 现在时间:@DateTime.Now.ToString("T") </body&

Haproxy + Varnish 实现WEB静态页面缓存

一.缓存介绍及Haproxy+Varnish架构图: 1.)简介:现阶段的互联网时代,缓存成为一个必不可少的一环,不论是对于整体架构的优化,减少服务器的压力,加速用户访问速度,都是需要用到缓存.而缓存的种类也是很多,例如CDN,Squid,Memcached,Varnish,已经成为一个中型,大型架构中基本的实现. 2.)CDN缓存技术是根据全国各地的用户,直接缓存到离用户最近的地方. 3.)Squid是处于前端的缓存,并且可以用作为正向代理,反向代理,透明代理. 4.)Memcached主要用