VaryByParam Location and CacheProfiles

VaryByParam Location and CacheProfiles in OutputCache Attribute

In this article, I am going to discuss the VaryByParam Location and CacheProfiles Properties of OutputCache Attribute. 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:

If you want to implement different cache version of the output based on some input parameters then you need to use the VarByParam option of OutputCache attribute. For example, we have a details 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.

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.

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 server where the request was processed.

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.

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.

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.

None: When you set the Location value to None, then the output cache is disabled for the requested page.

ServerAndClient: When you set the Location value as ServerAndClient, then the output cache can be stored only at the origin server or at the requesting client. Proxy servers are not allowed to cache the response. This value corresponds to the combination of the System.Web.HttpCacheability.Private and System.Web.HttpCacheability.Server enumeration values.

Modify the Index action method as shown below to use the Location property:
[OutputCache(Duration = 10, Location = System.Web.UI.OutputCacheLocation.Client)]
public ActionResult Index()
    return View(db.Employees.ToList());
CacheProfiles in OutputCache Attribute:

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 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
        <add name="1MinuteCache" duration="60" varyByParam="none"/>

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:
  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 will discuss customizing the output cache attribute in ASP.NET MVC application.

In this article, I try to explain VaryByParam Location and CacheProfiles in ASP.NET MVC application step by step with a simple example. I hope you understood the need and use of VaryByParam, Location and CacheProfiles options in OutputCache Attribute. 

1 thought on “VaryByParam Location and CacheProfiles”

Leave a Reply

Your email address will not be published. Required fields are marked *