Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 954
Krishh Avatar asked Jun 22 '26 03:06

Krishh


2 Answers

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:

  • http://api.jquery.com/jQuery.ajax/
  • http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs
like image 135
anAgent Avatar answered Jun 23 '26 17:06

anAgent


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

like image 37
Shyju Avatar answered Jun 23 '26 17:06

Shyju