Tuesday, August 21, 2012

cache_page directive for ASP.NET MVC

In Ruby on Rails, we should cache the response of the page with cache_page directive on controller.
class SampleController < ApplicationController
  caches_page :home
  caches_action :home_with_filter

  def home
    ...
  end

  def home_with_filter
    ...
  end
end

I learned from the book Programming Microsoft® ASP.NET MVC, Second Edition that we should use System.Web.Mvc.OutputCacheAttribute class on each actions like below:
public class SampleController : Controller

    [OutputCache(Duration=10)]
    public ActionResult Home()
    {
        ...
    }
end

The advantage of rails way is there are two directives: cache_page and cache_action. cache_page does not execute any actions and filters, but, cache_action only executes filters while ASP.NET MVC has only OutputCache attritbute that does not execute any actions and filters too.

On the other hand, the advantage of ASP.NET MVC way is easy to define expiration seconds of caching while rails' one is more difficult.

No comments:

Post a Comment