VaryByParam Location and CacheProfiles in OutputCache Attribute in MVC

VaryByParam Location and CacheProfiles in OutputCache Attribute in MVC

In this article, I am going to discuss the VaryByParam Location and CacheProfiles Properties of OutputCache Attribute in MVC. Please read our previous article before proceeding to this article where we discussed the basics of OutputCache Attribute in MVC application with an example. We are going to work with the same example that we started in our previous article.

VarByParam Option of OutputCache Attribute in ASP.NET MVC:

If you want to implement different cache versions of the output based on some input parameters then you need to use the VarByParam option of the OutputCache attribute in the ASP.NET MVC Application. For example, we have a detailed action method which will display the employee details based on the employee id. That means this method takes employee id as the input parameter and based on that input parameter it displays that employee information. Now we want to create a different cache version of the details view based on the employee id.

In situations like above, we can make use of the VaryByParam option of the OutputCache attribute. This VaryByParam property creates a different cached version of the same content when a form parameter or query string parameter varies. Following is the implementation of Details action.

[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Employee employee = db.Employees.Find(id);
    if (employee == null)
    {
        return HttpNotFound();
    }
    return View(employee);
}

The Details() action method includes a VaryByParam property with the value “Id”. When different values of the Id parameter are passed to the controller action, different cached versions of the Details view are generated. So, in simple words, we can say that VaryByParam means based on the input parameter, it will store different versions of the view in the cache.

Location Property with OutputCache Attribute:

The Location property of OutputCache Attribute is used to specify the location where the output is cached. The Location property takes the values from OutputCacheLocation enum. The definition of OutputCacheLocation enum is shown below.

VaryByParam Location and CacheProfiles OutputCacheLocation Enum

As per your business requirement and security, you can cache the data in the appropriate location as follows:

  1. Any: This is the default value for Location Property. If we have not specified the Location Property in OutputCache Attribute then the value is going to be Any. In this case, the output cache can be located on the browser client (from where the request is originated) on a proxy server (or any other server) participating in the request, or on the resource server where the request was processed.
  2. Client: If you set the Location value to Client, then the output cache is going to be stored on the browser client from where the request is originated.
  3. Downstream: When you set the Location value to Downstream, then the output cache is going to be stored in any HTTP 1.1 cache-capable devices other than the origin server. This includes proxy servers and the client that made the request.
  4. Sever: When you set the Location value as Server, then the output cache is going to be stored on the Web server where the request was processed.
  5. None: When you set the Location value to None, then the output cache is disabled for the requested page.
  6. ServerAndClient: When you set the Location value as ServerAndClient, then the output cache can be stored either at the origin server or at the requesting client. Proxy servers are not allowed to cache the response. 
Modify the Index action method as shown below to use the Location property:

In the below example, we have set the location property value as client and hence, now the output cache is going to be stored on the client machine.

[OutputCache(Duration = 10, Location = System.Web.UI.OutputCacheLocation.Client)]
public ActionResult Index()
{
    return View(db.Employees.ToList());
}
CacheProfiles Properties of OutputCache Attribute in ASP.NET MVC:

To cache the data returned by Index() action method, for 60 seconds, we would use [OutputCache] attribute as shown below.

[OutputCache(Duration = 60)]
public ActionResult Index()
{
    return View(db.Employees.ToList());
}

In the example above, we specified the OutputCache settings within the code i.e. at the action method level. The problem with this approach is that

  1. If you have to apply the same cache settings to several methods then you need to write the same code at multiple places resulting code duplicated.
  2. Later, if you want to change the cache settings, then you need to change it at several places. As a result, maintaining the application code becomes complicated. Also, changing the application code requires to build and re-deployment.

To overcome the above disadvantages, the cache settings can be specified in the web.config file using cache profiles.

Step1: Specify the cache settings in web.config using cache profiles
<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <clear/>
        <add name="1MinuteCache" duration="60" varyByParam="none"/>
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>
Step2: Reference the cache profile in application code
[OutputCache(CacheProfile = "1MinuteCache")]
public ActionResult Index()
{
    return View(db.Employees.ToList());
}

The cache settings are now read from one central location i.e. from the web.config file and this is possible due to the cache profiles.

Advantage of using Cache Profiles in ASP.NET MVC Application:
  1. You have only one place to change the cache settings if required. As a result, maintaining the caching is much easier.
  2. As the changes are done in the web.config, we don’t require to build and redeploy the application. 

In the next article, I am going to discuss customizing the output cache attribute in the ASP.NET MVC application. Here, in this article, I try to explain VaryByParam Location and CacheProfiles in ASP.NET MVC application with Examples. I hope you understood the need and use of VaryByParam, Location, and CacheProfiles options in OutputCache Attribute in ASP.NET MVC Application. 

1 thought on “VaryByParam Location and CacheProfiles in OutputCache Attribute in MVC”

Leave a Reply

Your email address will not be published.