Who's Online
12 visitors online now
0 guests, 12 bots, 0 members
Support my Sponsor
  • An error has occurred, which probably means the feed is down. Try again later.

Caching in MVC 5 Asp.net

Hello Everyone,

In this blog I want to share advantages of caching in MVC. Its a very important to know how caching is used in MVC websites.
By using Caching, we can store content at client’s browser and/or server. We can easily access these data and content when required.Caching data or content provides following advantages:

  • Minimize Request To Hosting Server
  • Minimize Request To Database Server
  • Reduce Network Traffic
  • Save Time and resources
  • Above advantages helps in improving the performance of MVC Website.

There are two types of method for caching available in MVC.

  • Page Output Caching
  • Application Caching

In this blog we will discuss on Page Output Caching.We can achieve this by
adding OutputCache attribute to either an individual controller action or an
entire controller class.

OutputCache Filter Parameter :

Parameter Type Description
CacheProfile string It is the name of the output cache profile which is defined with in tag of Web.config.

Example: Creating Cache profile in cache.
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name=”Test1″ duration=”30″ varyByParam=”none”/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>

And this cache profile is used in the action method as shown below.

[OutputCache(CacheProfile = “Test1”)]
public ActionResult Index()
{
return View();
}

Parameter Type Description
Duration int It specify the duration in sec to cache the content.

Example : The output of the Index() action is cached for 30 seconds.

using System.Web.Mvc;

namespace MvcApplication1.Controllers

{
public class HomeController : Controller
{
[OutputCache(Duration=30, VaryByParam=”none”)]
public ActionResult Index()
{
return View();
}
}
}

Note: If you will not defined the duration, it will cached it for
by default cache duration 60 sec.

 

Parameter Type Description
Location OutputCacheLocation It specify the location of the output to be cached.
Location property can be any one of the following values:

  • Any: It is default location attribute.The cache can be located on the browser client , on a proxy server participating in the request, or on the server .
  • Client: Store content on the browser
  • Downstream: The output cache can be stored in any HTTP 1.1 cache-capable devices other than the origin server.
  • Server:Store content on the Server
  • None: The output cache is disabled for the requested page.
  • ServerAndClient:Store content on both browser and server

Example: Cache stored at client browser.
using System.Web.Mvc;
using System.Web.UI;

namespace MvcApplication1.Controllers
{
public class UserController : Controller
{
[OutputCache(Duration=3600, VaryByParam=”none”, Location=OutputCacheLocation.Client)]
public ActionResult Index()
{
return View();
}

}
}

Parameter Type Description
VaryByParam  string This property enables us to create different cached versions of
the very same content when a form parameter or query
string parameter varies.
If not specified, the default value is none.
VaryByParam=”none” specifies that caching doesn’t depend
on anything.
VaryByCustom  string This is used for custom output cache requirements
VaryByHeader  string This specify list of HTTP header names that are used tovary the cache content.

Example:

public class TestController : Controller

{

[OutputCache(Duration = 30, VaryByParam = “none”)]
public ActionResult Index()
{
return View();
}
[OutputCache(Duration = 30, VaryByParam = “id”)]
public ActionResult Index1()
{
return View();
}
}

Parameter Type Description
NoStore bool It enable/disable where to use HTTP Cache-Control.
This is used only to protect very sensitive data.
SqlDependency string It specify the database and table name pairs on which
the cache content depends on. The cached data will expire
automatically when the data changes in the database.

 

For more information visit here
I hope this will help you.Your valuable feedback and comments are important for me.

Comments are closed.