至於其他的答案已經指出,甜甜圈緩存「之類的」在MVC作品緩存。
我不會推薦它 - 相反,我會提供一個alterantive:
你必須爲用戶帶來查看資料,讓我們把它稱爲「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);
}
}
使用呈現出局部視圖的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>
這樣,你可以最大化OutputCache的核心細節(Index()),但最近的帖子沒有被緩存。 (或者你可以有一個非常小的緩存期)。
呈現部分的jQuery方法與RenderPartial不同,因爲這樣您可以直接從控制器提供HTML,因此您可以相應地控制輸出緩存。
最終結果與甜甜圈緩存非常相似(緩存部分頁面,其他部分不緩存)。
另請參閱http://stackoverflow.com/questions/4082826/when-and-how-to-go-about-performing-caching-in-asp-net-mvc/4091232#4091232 – 2010-11-06 03:27:42
任何人都知道答案?我的更新? – Rana 2010-11-08 21:09:46
您需要使用RenderPartial的第四個重載(http://msdn.microsoft.com/zh-cn/library/dd470561.aspx)嘗試:'Html.RenderPartial(「UserPosts.ascx」,Model.UserPosts,new ViewDataDictionary {Model = Model.UserPosts}'。 – RPM1984 2010-11-09 04:18:59