Krishh
Krishh

Reputation: 4231

Using Output Cache in MVC for Object Parameter

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

Upvotes: 3

Views: 4336

Answers (2)

anAgent
anAgent

Reputation: 2780

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:

Upvotes: 5

Shyju
Shyju

Reputation: 218782

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

Upvotes: 3

Related Questions