简体   繁体   English

Asp.net MVC 2缓存

[英]Asp.net MVC 2 caching

I'm currently developing a web site using asp.net mvc 2 in c#. 我目前正在使用c#中的asp.net mvc 2开发一个网站。 I have never used the caching feature in MVC and would like to apply it to the user profile page. 我从未在MVC中使用过缓存功能,并希望将其应用于用户配置文件页面。 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) (我使用linq-to-sql从数据库加载数据)

I need some suggestions on what caching technique I should use and how to implement it? 我需要一些关于我应该使用哪种缓存技术以及如何实现它的建议?

Update: Xandy's solution below almost works, except I cannot pass in data. 更新:下面的Xandy解决方案几乎可以工作,除了我无法传递数据。 How would I rewrite this using the ? 我怎么用这个重写呢? Html.RenderPartial("UserPosts", ViewData["UserPosts"]) Html.RenderPartial(“UserPosts”,ViewData [“UserPosts”])

Phil Hack's fragment caching tricks no longer work in MVC2. Phil Hack的片段缓存技巧不再适用于MVC2。

At StackOverflow we build html fragments as text and cache them using HttpRuntime.Cache and more. 在StackOverflow,我们将html片段构建为文本,并使用HttpRuntime.Cache等来缓存它们

As other answers have stated, donut caching "sort of" works in MVC. 正如其他答案所述,甜甜圈缓存“有点”在MVC中起作用。

I wouldn't recommend it - instead i'll offer an alterantive: 我不推荐它 - 而是我会提供一个替代品:

You have a View for the Users Profile, let's call it " UserProfile.aspx ". 您有一个用户配置文件的视图,我们称之为“ UserProfile.aspx ”。

Now on this View, you have a bunch of HTML, including a section for "recent posts". 现在在这个视图中,你有一堆HTML,包括“最近的帖子”部分。

Now, i am assuming this is something like the last 10 posts for the user . 现在,我假设这类似于用户的最后10个帖子

What i would do is put this HTML/section into a Partial View, and serve it via a seperate action method, aka a PartialViewResult: 我要做的是将这个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);
   }
}

Render out the Partial View using jQuery: 使用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>

That way, you can max out the OutputCache for the core details (Index()), but the recent posts are not cached. 这样,您可以最大限度地输出OutputCache以获取核心详细信息(Index()),但不会缓存最近的帖子。 (or you can have a very small cache period). (或者你可以有一个非常小的缓存期)。

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. 渲染部分的jQuery方法与RenderPartial不同,因为这样您可以直接从控制器提供HTML,因此您可以相应地控制输出缓存。

The end result is very similar to donut caching (parts of the page cached, other's not). 最终结果非常类似于圆环缓存(缓存页面的一部分,其他不是)。

ASP.Net has a tutorial on output caching for MVC . ASP.Net有一个关于MVC输出缓存教程

Partial (aka Donut) Caching which would work for MVC2. 部分(又名Donut)缓存适用于MVC2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM