Monday, June 18, 2018

ASP.NET MVC : OutPutCache Action Filter

The outputCache, inbuilt action filter in MVC, enables you to cache the action result content by a MVC controller action. By default MVC Output cache is enabled. So for this you don’t need to do anything, if client will send same request, browser will not forward request to MVC controller and it returns previous cached result.

There are few major properties of OutPutCache attribute
·         Location
·         NoStore
·         Duration
·         VaryByParm
·         VaryByHeader

OutPutCache Location:

The output cache can be located on the browser client (where the request originated), on a proxy server (or any other server) participating in the request, or on the server where the request was processed. There are following possible values for output cache location and default value is ‘ANY’.
  • Any - the output cache can be located on client browser, proxy server.
  • Client - Cache will be stored on client browser as private.
  • Downstream - The output cache will be stored in any HTTP 1.1 cache-capable devices other than the origin server
  • None - The output cache will be disabled
  • Server - The output cache will be stored on the server where request was handled and processed.
  • ServerAndClient – the output cache will be stored on both server and client browser. 
Here is an example, output cache location is Client.

public class HomeController : Controller

    {
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Client)]

public ActionResult LoadInfo(int pageNumber)

{

            List<Info> infos = new List<Info>();

            for (int i = 0; i < 10; i++)

            {

                int ivalue = rand.Next();

                Info info = new Info()

                {

                    Header = "Header" + ivalue,

                    InfoBody = "Body",

                };

                infos.Add(info);

            }

            return PartialView("_Info", infos);

}

    }


OutputCache NoStore:

NoStore is Boolean property, which tells that cache value will be stored or not. Default value is true.If NoStore value is true, it prevent the caching your data.

OutputCache Duration:

It tell the time duration (in second) that the cache data is stored.

OutputCache VaryByParm:

This property enables you to create different cached versions of the very same content when a form parameter or query string parameter varies.
  • VaryByParam = “City,CustomerID” :
     It creates different cached versions whenever any of the form or query string parameters in the list “City, CustomerID” varies.

[OutputCache(NoStore = true, Duration = 1000, VaryByParam = " City,CustomerID", Location = System.Web.UI.OutputCacheLocation.Client)]

public ActionResult GetCustomerInfo(int customerID, string city)

{

}

  • VaryByParam = "none" :
If you don’t want to store the Cache data, set VaryByParam = "none" it never cache data and always calls to repository to get data from database.

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "none", Location = System.Web.UI.OutputCacheLocation.Client)]

public ActionResult GetCustomerInfo(int customerID, string city)

{

}
  • VaryByParam = "*" :
It create a different cached version, whenever a form or query string parameters varies

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.Client)]

public ActionResult GetCustomerInfo(int customerID, string city)

{

}

Other related topics 
Thanks for visiting!! 

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...