Asp.net MVC 2的缓存 [英] Asp.net MVC 2 caching

查看:178
本文介绍了Asp.net MVC 2的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发使用asp.net MVC 2在C#中的网站。我从来没有使用MVC中的缓存功能,并想将它应用到用户个人资料页面。此页面很少变化,需要在实时的唯一部件上的内容是最近的职位由用户列表。 (我使用LINQ到SQL来加载数据库中的数据)

I'm currently developing a web site using asp.net mvc 2 in c#. I have never used the caching feature in MVC and would like to apply it to the user profile page. The content on this page rarely changes and the only part that needs to be in realtime is the list of recent posts by the user. (I use linq-to-sql to load data from the database)

我需要什么缓存技术,我应该使用,并提出了一些建议如何实现它?

I need some suggestions on what caching technique I should use and how to implement it?

更新:
Xandy的解决方案几乎以下工作,但我不能在数据传递。我将如何改写这个使用? Html.RenderPartial(UserPosts计算机[UserPosts])

Update: Xandy's solution below almost works, except I cannot pass in data. How would I rewrite this using the ? Html.RenderPartial("UserPosts", ViewData["UserPosts"])

推荐答案

至于其他的答案已经指出,甜甜圈缓存之类的MVC中的作品。

As other answers have stated, donut caching "sort of" works in MVC.

我不会推荐它 - 相反,我会提供一个alterantive:

I wouldn't recommend it - instead i'll offer an alterantive:

您有一个观点的用户配置文件,姑且称之为 UserProfile.aspx

You have a View for the Users Profile, let's call it "UserProfile.aspx".

现在这一观点,你有一堆的HTML,其中包括最近的帖子的部分。

Now on this View, you have a bunch of HTML, including a section for "recent posts".

现在,我假定这是一样的东西在过去10个职位,为用户

Now, i am assuming this is something like the last 10 posts for the user.

我会做的就是把这个HTML /成部分的局部视图,并通过一个单独的操作方法为它服务,也叫做 PartialViewResult:

What i would do is put this HTML/section into a Partial View, and serve it via a seperate action method, aka a 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的局部视图:

Render out the Partial View using 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为核心的细节(指数()),但最近的帖子不会被缓存。 (或者你可以有一个非常小的缓存期)。

That way, you can max out the OutputCache for the core details (Index()), but the recent posts are not cached. (or you can have a very small cache period).

渲染局部的jQuery的方法是不同的的RenderPartial ,因为这种方式,您可以直接从控制器服务于HTML,这样你就可以相应地控制输出缓存。

The jQuery method of rendering the partial is different to RenderPartial, as this way you are serving the HTML directly from the controller, so you can control the output caching accordingly.

最终的结果是非常相似的甜甜圈缓存(页面缓存的部分,其他不是)。

The end result is very similar to donut caching (parts of the page cached, other's not).

这篇关于Asp.net MVC 2的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆