---
title: "Caching concept In ASP.NET MVC"  
description: "The caching is used for improving the performance in ASP.NET MVC. The caching is a technique which stores something in memory"  
author: "Anonymous User"  
published: 2019-07-05  
updated: 2019-12-26  
canonical: https://www.mindstick.com/articles/126234/caching-concept-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["mvc", "mvc3", "mvc2", "mvc4", "mvc5"]  
reading_time: 4 minutes  

---

# Caching concept In ASP.NET MVC

### 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](https://answers.mindstick.com/qa/111685/how-do-i-optimize-my-code-for-better-performance).

Into the MVC, the [OutputCache attribute](https://www.mindstick.com/forum/156059/what-is-outputcache-attribute-in-asp-dot-net-mvc) is used for applying Caching. OutputCheching will store the output of a [**Controller in memory**](https://www.mindstick.com/articles/13132/how-can-export-data-from-webgrid-to-excel-in-mvc) and if any other request comes for the same, it will return it from the cached result.

![Caching concept In ASP.NET MVC](https://www.mindstick.com/mindstickarticle/84b8933e-a0a5-45c2-b6ba-f01f54cb73d2/images/b460dda0-d629-4be5-bb9a-0a2f5870e29d.png)\

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](https://www.mindstick.com/forum/161972/is-sql-more-vital-for-solving-complex-business-problems-or-power-bi-tableau-for-insights) 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](https://www.mindstick.com/mindstickarticle/84b8933e-a0a5-45c2-b6ba-f01f54cb73d2/images/854424a1-1a97-4e9a-969d-83e09306e5f9.png)

### 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](https://www.mindstick.com/mindstickarticle/84b8933e-a0a5-45c2-b6ba-f01f54cb73d2/images/98874097-7f2e-4bf7-9959-70843d576cf1.png)

### Location

This specifies where the cache is stored. Here some [options are available](https://answers.mindstick.com/qa/112073/what-financial-aid-options-are-available-for-students-at-universities) 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](https://www.mindstick.com/interview/34005/how-can-you-handle-cache-consistency-issues). 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](https://www.mindstick.com/forum/161355/how-does-data-binding-work-in-wpf) 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](https://www.mindstick.com/news/1303/whatsapp-privacy-policy-receives-ultimatum-until-end-of-february-says-eu) 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](https://www.mindstick.com/interview/34346/how-to-enable-cache-for-static-content-in-asp-dot-net-web-development)
- Caching whole or partial page response using OutputCache Attribute
- Caching shared data

![Caching concept In ASP.NET MVC](https://www.mindstick.com/mindstickarticle/84b8933e-a0a5-45c2-b6ba-f01f54cb73d2/images/c4e10f3e-2907-433c-aa69-438bd9d7b02b.png)

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](https://www.mindstick.com/forum/55142/how-can-use-server-side-caching-in-mvc)

---

Original Source: https://www.mindstick.com/articles/126234/caching-concept-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
