05 May 2012

Generate unique URL for unique resources to leverage caching

URL used to access any resource on web. To increase the performance of the web application, the URL used as one of the keys to cache data at different locations. Accessing the resources using unique URLs helps to avoid putting duplicate response in cache and also helps to increase performance of web application

In ASP.NET MVC application we can cache the action output so that we can improve the site performance.

[OutputCache(Duration = 10, VaryByParam = "visitor",
Location = OutputCacheLocation.Server)]
 public ActionResult Index(string visitor = "Guest")
   {
   ViewBag.Message = visitor + 
  ", Welcome to ASP.NET MVC!" + DateTime.Now.ToString();
   return View();
  }

So Index action is cached on the web server for 10 seconds and new cache entry will be created for each different visitor.The same cached action output response will be served for the subsequent request comes within 10-second duration provided the visitor parameter is same.

As you can see the visitor parameter is optional here. So when no value specified for the visitor param, "Guest" will be used as its value.

So, http://example.com/Home/Index/Guest and http://example.com/Home/Index will actually display the same content.

When query strings are used, http://example.com/Home/Index?visitor= Guest and "http://example.com/Home/Index" will actually display the same content.

But the question is the URLs which serve the same content will they make use of the same cached content when?

Applying the same question to the example, http://example.com/Home/Index/Guest URL once servers the response, its output for the "Guest" parameter is cached for the next request within the set duration in seconds. Having said that will the URL, http://example.com/Home/Index, which syntactically similar to the URL which specifies the default value explicitly, will they share the same cached copy?

Answer is NO.

Because the cache is based on the complete URL when ASP.NET MVC decides whether to use available cache for the incoming URL request, it checks whether the URL is exactly same as the cached URL. Since http://example.com/Home/Index/Guest and http://example.com/Home/Index are not the same strings, the action will execute on the server and will result in new output response. So, when generating the URLs for the cached actions, always strive to generate the unique URLs wherever we can reuse the cached copies of the action output response.

When Action parameter is used in VaryByParam, pay attention to the parameter casing. Different output cache will be generated for the same parameter value if they differ in casing.

Let's take an example.

[OutputCache(VaryByParam = "id"
   Location = OutputCacheLocation.ServerAndClient,
   Duration = 60)]
public ActionResult Detail(string id)
 {
   ViewBag.Message = id + " Details are displayed";
   return View();
 }

The URL http://example.com/detail/Detail/apple and http://example.com/detail/Detail/Apple will not use the same output cache. They will create a different output cache. So when such URLs generated in the server side pay attention to the casing.

Its a best practice to always generate the URL in lower case.