Asp.net MVC 2 caching
Asked Answered
S

3

9

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?

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"])

Selfemployed answered 4/11, 2010 at 0:45 Comment(3)
See also #4083326Squeak
anyone know the answer? to my update?Selfemployed
you need to use the fourth overload for RenderPartial (msdn.microsoft.com/en-us/library/dd470561.aspx) try : Html.RenderPartial("UserPosts.ascx", Model.UserPosts, new ViewDataDictionary { Model = Model.UserPosts }.Padgett
F
5

Phil Hack's fragment caching tricks no longer work in MVC2.

At StackOverflow we build html fragments as text and cache them using HttpRuntime.Cache and more.

Feedback answered 4/11, 2010 at 8:21 Comment(0)
P
5

As other answers have stated, donut caching "sort of" works in 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".

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

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

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);
   }
}

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>

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).

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).

Padgett answered 6/11, 2010 at 3:23 Comment(2)
So what about clients without javascript?Deceptive
Was that a requirement the OP stated?Padgett
D
1

ASP.Net has a tutorial on output caching for MVC.

Partial (aka Donut) Caching which would work for MVC2.

Deceptive answered 4/11, 2010 at 0:50 Comment(6)
Yes, I saw that too. But how can I cache the page while making part of it not cached?Selfemployed
the next page of that talks about partial caching. asp.net/mvc/tutorials/…Deceptive
Is this still the preferred method in MVC 2?Selfemployed
Phil Haccked has an article talk about this, see my edited answer.Deceptive
how do you pass ViewData to a user control? Is it possible?Selfemployed
This solution almost works, except I cannot pass in data. How would I rewrite this using the <server-side tag approach>? Html.RenderPartial("UserPosts", ViewData["UserPosts"])Selfemployed

© 2022 - 2024 — McMap. All rights reserved.