Ran*_*ana 9 c# asp.net-mvc linq-to-sql
我目前正在使用c#中的asp.net mvc 2开发一个网站.我从未在MVC中使用过缓存功能,并希望将其应用于用户配置文件页面.此页面上的内容很少更改,唯一需要实时的部分是用户最近发布的帖子列表.(我使用linq-to-sql从数据库加载数据)
我需要一些关于我应该使用哪种缓存技术以及如何实现它的建议?
更新:下面的Xandy解决方案几乎可以工作,除了我无法传递数据.我怎么用这个重写呢?Html.RenderPartial("UserPosts",ViewData ["UserPosts"])
正如其他答案所述,甜甜圈缓存"有点"在MVC中起作用.
我不推荐它 - 而是我会提供一个替代品:
您有一个用户配置文件的视图,我们称之为" UserProfile.aspx ".
现在在这个视图中,你有一堆HTML,包括"最近的帖子"部分.
现在,我假设这类似于用户的最后10个帖子.
我要做的是将这个HTML /部分放入一个部分视图,并通过一个单独的动作方法,也就是PartialViewResult来提供它:
public class UserProfileController
{
[HttpGet]
[OutputCache (Duration=60)]
public ActionResult Index() // core user details
{
var userProfileModel = somewhere.GetSomething();
return View(userProfileModel);
}
[HttpGet]
public PartialViewResult DisplayRecentPosts(User user)
{
var recentPosts = somewhere.GetRecentPosts(user);
return PartialViewResult(recentPosts);
}
}
Run Code Online (Sandbox Code Playgroud)
使用jQuery渲染部分视图:
<script type="text/javascript">
$(function() {
$.get(
"/User/DisplayRecentPosts",
user, // get from the Model binding
function (data) { $("#target").html(data) } // target div for partial
);
});
</script>
Run Code Online (Sandbox Code Playgroud)
这样,您可以最大限度地输出OutputCache以获取核心详细信息(Index()),但不会缓存最近的帖子.(或者你可以有一个非常小的缓存期).
渲染部分的jQuery方法与RenderPartial不同,因为这样您可以直接从控制器提供HTML,因此您可以相应地控制输出缓存.
最终结果非常类似于圆环缓存(缓存页面的一部分,其他不是).
| 归档时间: |
|
| 查看次数: |
2256 次 |
| 最近记录: |