articles

Home / DeveloperSection / Articles / Caching concept In ASP.NET MVC

Caching concept In ASP.NET MVC

Caching concept In ASP.NET MVC

Anonymous User 2373 05-Jul-2019

Use of Caching

The caching is used for improving the performance in ASP.NET MVC. The caching is a technique that stores something in memory that is being used frequently to provide better performance.

Into the MVC, the OutputCache attribute is used for applying Caching. OutputCheching will store the output of a Controller in memory and if any other request comes for the same, it will return it from the cached result.

Caching concept In ASP.NET MVC

But unfortunately, there are only very few websites that take the minimum time to open and the reasons behind this are as following.

  • Huge content
  • Large and multiple images
  • Complex business logic
  • Too much server requests
  • Poor connection
  • Data Access Layer etc.

So, there are several reasons for the slowness of a website. OutputCache attribute can have a parameter. 

Duration

It describes the time in seconds.

The example of duration is given below.

The example of duration is given below.

[OutputCache(Duration = 60)]  

public ActionResult Index() {
    var std = from e in db.Student
    orderby e.ID
    select e;
    return View(std);
}

Caching concept In ASP.NET MVC

VaryByParam

This describes cache will be stored on the basis of a parameter. The cache will be stored on the basis of the list of semicolon-separated by a string.

An example of VaryByParam is given below.

[OutputCache(Duration = 60, VaryByParam = "Id")]  

public ActionResult Index(int Id) {
    var std = from e in db.Student where e.DeptID = Id
    orderby e.ID
    select e;
    return View(std);
}

Caching concept In ASP.NET MVC

Location

This specifies where the cache is stored. Here some options are available for places(Location).

  • Any (Default)- Content is cached in three locations- the Web Server, any Proxy Servers and the Web Browser.
  • Client- Content is cached on the Web Browser.
  • Server- Content is cached on the Web Server.
  • ServerAndClient- Content is cached on the Web Server and the Web Browser.
  • None- Content is not cached anywhere.

Now, the example of Location is given below.

[OutputCache(Duration = 60, VaryByParam = "Id", , Location = OutputCacheLocation.Client)]  

public ActionResult Index(int Id) {
    var std = from e in db.Student where e.DeptID = Id
    orderby e.ID
    select e;
    return View(std);
}

CacheProfile maintains via the web.config file

CacheProfile is another way to handle cache. You can create profiles in web.config.

An example is given below.

In web.config

<caching>  

    <outputCacheSettings>
        <outputCacheProfiles>
            <add name="Long" duration="60" varyByParam="Id" />
            <add name="Medium" duration="60" varyByParam="none" />
            <add name="Short" duration="10" varyByParam="none" /> </outputCacheProfiles>
    </outputCacheSettings>
</caching>

In Controller

[OutputCache(CacheProfile = "Long")]  

public ActionResult Index(int Id) {
    var std = from e in db.Student where e.DeptID = Id
    orderby e.ID
    select e;
    return View(std);
}

Features of caching

If we define the advantage of caching in one word, that will be “Performance”; and it can be achieved to minimize the following things.

  • Reduce the Server round trips [Hosting Server or Database Server or Any Other Server]
  • Reduce the Network Traffic [Http Call to Server]
  • Avoid repeating the same data binding logic

Tips and Tricks when using ASP.NET MVC caching

  • We should not use caching with the data which changes frequently.
  • We should not use caching with Authentication logic.
  • We should not use caching with the data which is unique for the individual user.
  • We should not use caching with data which use very rare like the privacy policy page.
  • We should not use caching for an Error page.

Use caching with data which is used very frequently and the same data can be used by all user.

  • Always use caching with images or media files.

Generally, we can cache our data using 3 ways in ASP.NET MVC.

  • Caching Static Content
  • Caching whole or partial page response using OutputCache Attribute
  • Caching shared data

Caching concept In ASP.NET MVC

If you want to know more about it then you should follow this link :

https://www.mindstick.com/forum/55142/how-can-use-server-side-caching-in-mvc


Updated 26-Dec-2019
I am a content writter !

Leave Comment

Comments

Liked By