`
raisun_1988
  • 浏览: 114288 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

MonoRail学习笔记八:页面缓存的使用

    博客分类:
  • .Net
阅读更多

以前版本中MonoRail是不支持页面缓存的,在1.0 RC3版中加入了页面缓存的支持,有了页面缓存之后会对性能有很大的提升。
主要是通过加入了一个新的属性CacheAttribute.cs,其实后台也是同样使用了System.Web下的缓存处理的机制
(以前Yok也写过一篇实现MonoRail页面缓存,他是通过编写自己的缓存类来实现的,效果也不错)

使用方法很简单,如下所示:
下面列出了主要的文件,要使用缓存只是在Controller下红色的一条语句

测试Html文件
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><html>
<head>
<title>测试页面</title>
</head>
<body>
<formaction="/index.rails"method="post">
ID:
<inputtype="text"name="id"value="1"/><br/>
姓名:
<inputtype="text"name="name"value="姓名"/><br/>
年龄:
<inputtype="text"name="age"value="22"/><br/>
生日:
<inputtype="text"name="birthday"value="2007-1-1"/><br/>
<inputid="Submit1"type="submit"value="submit"/>
</form>
</body>
</html>

Controller文件的Index方法:

[Cache(HttpCacheability.Public,Duration=360,VaryByParams="id,name")]
publicvoidIndex(intid,stringname,intage,DateTimebirthday)
{
PropertyBag.Add(
"id",id);
PropertyBag.Add(
"name",name);
PropertyBag.Add(
"age",age);
PropertyBag.Add(
"birthday",birthday);
}

注意上面红色标示的代码
我这里只是列出了一种常用的方式,Duration表示缓存有效期是360秒,VaryByParams指定当id或者name的参数值改变了才重新生成此页面,否则不用调用此方法,直接使用缓存中的页面,HttpCacheability定义可以参照MSDN中的解释:
NoCache:设置 Cache-Control: no-cache 标头。如果没有字段名,则指令应用于整个请求,且在满足请求前,共享(代理服务器)缓存必须对原始 Web 服务器强制执行成功的重新验证。如果有字段名,则指令仅应用于命名字段;响应的其余部分可能由共享缓存提供。
Private:默认值。设置 Cache-Control: private 以指定响应只能缓存在客户端,而不能由共享(代理服务器)缓存进行缓存。
Public:设置 Cache-Control: public 以指定响应能由客户端和共享(代理)缓存进行缓存。
Server:指定响应仅缓存在源服务器上。与 NoCache 选项相似。客户机接收 Cache-Control: no-cache 指令,但文档是在原始服务器上缓存的。
ServerAndNoCache:应用 Server 和 NoCache 的设置指示在服务器上缓存内容,而对服务器以外的其他对象都显式否定其缓存响应的能力。
ServerAndPrivate:指示响应只能在服务器和客户端缓存。代理服务器不能缓存响应。

index.vm文件
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><html>
<head>
<title>测试结果</title>
</head>
<body>
<form>
ID:$id
<br/>
姓名:$name
<br/>
年龄:$age
<br/>
生日:$birthday
<br/>
</form>
</body>
</html>

测试结果(在Index方法上设置断点):
第一次调用:进入断点,正常显示测试结果
第二次调用:(页面输入值不变),不进入断点,正常显示测试结果
第三次调用:(页面中年龄输入值改变),不进入断点,显示结果中年龄值还是之前的值
第四次调用:(页面中姓名输入值改变),进入断点,正常显示测试结果
第五次调用:(6分钟之后)),进入断点,正常显示测试结果
通过以上测试证明:MonoRail中的页面缓存是可以正常使用的,可以有效提高性能

实现机制简单分析:
在每个action执行之后,判断是否有Cache属性,如果有,就调用如下方法:

配置Asp.Net缓存机制
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidICachePolicyConfigurer.Configure(HttpCachePolicypolicy)
{
policy.SetCacheability(cacheability);
policy.SetSlidingExpiration(slidingExpiration);
policy.SetValidUntilExpires(validUntilExpires);
policy.SetAllowResponseInBrowserHistory(allowInHistory);

if(duration!=0)
{
policy.SetExpires(DateTime.Now.AddSeconds(duration));
}


if(varyByCustom!=null)
{
policy.SetVaryByCustom(varyByCustom);
}


if(varyByHeaders!=null)
{
foreach(StringheaderinvaryByHeaders.Split(','))
{
policy.VaryByHeaders[header.Trim()]
=true;
}

}


if(varyByParams!=null)
{
foreach(StringparaminvaryByParams.Split(','))
{
policy.VaryByParams[param.Trim()]
=true;
}

}


if(etag!=null)
{
policy.SetETag(etag);
}

}

好像也蛮简单的-_-,直接根据参数调用HttpCachePolicy类中的相应方法就可以了

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics