3

This is my controller method. Can anyone explain how I could write outputcache for the following method on the server.

    public JsonResult GetCenterByStateCityName(string name, string state, string city, bool sportOnly, bool rvpOnly)
    {
        var result = GetCenterServiceClient().GetCentersByLocation(name, city, state, sportOnly, rvpOnly).OrderBy(c => c.Name).ToList();
        return Json(result);
    }

Thank you

Krishh
  • 4,111
  • 5
  • 42
  • 52

2 Answers2

5

Have you looked at the documentation?

http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.aspx

In a nutshell, just set the Attribute on your Action

[OutputCache(CacheProfile = "SaveContactProfile", Duration = 10)]
public JsonResult SaveContact(Contact contact)
{
    var result = GetContactServiceClient().SaveContact(contact);
    return Json(result);
}

-- UPDATE --

If you're making a direct Ajax call via jQuery, the OutPutCache could be ignored based on the "cache" parameter - which is set to true by default.

For instance, your parameter would be ignored if you're doing something like:

$.ajax({
    url: someUrlVar,
    cache: true, /* this is true by default */
    success : function(data) {

    }
});

Just something to look at as you can cache that call two ways.

Reference:

anAgent
  • 2,550
  • 24
  • 34
  • Thank you. I will try this out now. Should we set the VaryByParam attribute? – Krishh Jul 23 '12 at 15:46
  • I updated the information to include some key details about also using Ajax to cache. As for your question, here is a great article: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs – anAgent Jul 23 '12 at 15:54
3
[OutputCache(Duration = 3600, VaryByParam = "name;state;city;sportOnly;rvpOnly")]
public JsonResult GetCenterByStateCityName(string name, string state, string city, bool sportOnly, bool rvpOnly)
{
        var result = GetCenterServiceClient().GetCentersByLocation(name, city, state, sportOnly, rvpOnly).OrderBy(c => c.Name).ToList();
        return Json(result);
}

The Duration value is 3600 seconds here. Sot the cache will be valid for 1 hour. You need to give the VaryByParam property values because you want different results for different parameters.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Does VaryByParam = "*" accomodate for the condition where if there is a change in name or state, it would not take from previously cached result right? – Krishh Jul 23 '12 at 15:55