---
title: "Caching in ASP.NET"  
description: "In this blog I will talk about caching in ASP.NET  Caching is veryimportant concept for any web application, which is used to store the data temporari"  
author: "priyanka kushwaha"  
published: 2015-02-23  
updated: 2015-02-23  
canonical: https://www.mindstick.com/blog/783/caching-in-asp-dot-net  
category: ".net"  
tags: ["c#", "asp.net"]  
reading_time: 4 minutes  

---

# Caching in ASP.NET

In this blog I will talk about [caching in ASP.NET](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc)\

Caching is very important concept for any [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about), which is used to store the data temporarily either on web server. This data can be stored for a specific time period on the server or client system. It is more useful when any use access the same set of data continuously.

Engine(Google, Yahoo etc.) for searching [relevant information](https://answers.mindstick.com/qa/105781/how-to-interpret-financial-news-and-filter-relevant-information-for-investing). If you open Google page on your system first time then first time Google pages comes from Google database. If you open again Google [search engine](https://www.mindstick.com/services/search-engine-optimization) then this Google page comes from Google server cache.

Accessing a database in an Asp.net applications generally a slow and time consuming process. To [improve the performance](https://www.mindstick.com/forum/157912/what-is-regularization-how-can-it-be-used-to-improve-the-performance-of-a-machine-learning-model) of a asp.net page, we can use Caching concept, which is quicker than retrieving the [data from the database](https://www.mindstick.com/forum/160510/how-to-fetch-data-from-the-database-in-the-dot-net-console-application-using-c-sharp) on each request.

##### Different types of caching:

ASP.NET providing three basic types of caching:

1. Page level output caching

2. Fragment caching

3. Data caching

##### Page level output caching:

In this Caching technique page output and HTTP response stored in a memory cache (RAM) of the computer. You can create more than one custom Output Cache provider.

**System.web.caching.OutputCacheprovider** class is used to create custom output cache provider which can be registered within the web.config file. This caching concept is more useful for static web page. In this caching data can be stored on different-2 location such as client side, server side or proxy side.

There are some techniques to implement the page output caching is [asp.net application](https://www.mindstick.com/forum/540/using-stored-procedures-with-entity-framework-in-an-asp-dot-net-application).

1. Page Output caching Depends on some control values.

2. Page Output caching Depends on some query string key value.

Note:

1. This page is saved in cache memory for 40 seconds.

2. You can use XML file instead of sql [server database](https://www.mindstick.com/forum/155643/how-to-create-sql-server-database-in-google-cloud) for this caching applications.

##### 2. Fragment Caching(partial page caching):

When we want to [implement caching](https://www.mindstick.com/forum/161239/how-do-you-implement-caching-in-an-asp-dot-net-application) on specific part of a web page then we can use [Fragment caching](https://www.mindstick.com/interview/959/what-is-fragment-caching-in-asp-dot-net) or partial caching. This type of caching is implemented by using webusercontrol.

##### Example:

```
<%@Page Language="C#" Trace="true" AutoEventWireup="true" CodeBehind="OutputCacheExample.aspx.cs" Inherits="DataCachingExample.OutputCacheExample" %> <!DOCTYPE html> <%@ OutputCache Duration="60" VaryByParam="state" %><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title> </head><body>    <form id="form1" runat="server">        <i>Last generate on:</i>        <asp:Label ID="TimeMsg" runat="server" Text=""></asp:Label>          <h3>            <font face="Verdana">Using the Output Cache</font></h3>        <b>Authors by State:</b>        <table cellspacing="0" cellpadding="3" rules="all" style="background-color: #AAAADD;            border-color: black; border-color: black; width: 700px; border-collapse: collapse;">            <tr>                <td>                    <a href="OutputCacheExample.aspx?state=CA">CA</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=IN">IN</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=KS">KS</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=MD">MD</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=MI">MI</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=OR">OR</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=M.P.">M.P.</a></td>                <td>                    <a href="OutputCacheExample.aspx?state=U.P.">U.P.</a></td>            </tr>        </table>        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MindStickTaskConnectionString %>" SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [DateOfBirth], [FatherName], [AddressLineFirst], [AddressLineSecond], [City], [State], [Country], [PinCode], [PhoneNo], [EmailAddress] FROM [EmployeeDetail] where State=@state">            <SelectParameters>                <asp:QueryStringParameter Name="state" QueryStringField="state" DefaultValue="CA"/>            </SelectParameters>        </asp:SqlDataSource>        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">            <Columns>                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" InsertVisible="False" ReadOnly="True" />                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />                <asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" />                <asp:BoundField DataField="FatherName" HeaderText="FatherName" SortExpression="FatherName" />                <asp:BoundField DataField="AddressLineFirst" HeaderText="AddressLineFirst" SortExpression="AddressLineFirst" />                <asp:BoundField DataField="AddressLineSecond" HeaderText="AddressLineSecond" SortExpression="AddressLineSecond" />                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />                <asp:BoundField DataField="PinCode" HeaderText="PinCode" SortExpression="PinCode" />                <asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" SortExpression="PhoneNo" />                <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" SortExpression="EmailAddress" />            </Columns>        </asp:GridView>     </form></body></html> 
```

3. Data caching:

Data caching allows caching of data as objects for multiple pages in an application. As the cached objects are available at application level so it is also called Application level caching. The cached data for objects are available until the application is restarted. Data caching allows developers to programmatically maintain data across requests.

##### Example:

##### \
1. Create a login Form

##### LoginForm.aspx

```
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.aspx.cs" Inherits="DataCachingSam.LoginForm" %>    <!DOCTYPE html>     <html xmlns="http://www.w3.org/1999/xhtml">    <head runat="server">        <title></title>    </head>    <body>        <form id="form1" runat="server">         <div>          User Name:-<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>      <br />      <br />          Password:-<asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>      <br />              <asp:Button ID="btnSubmit" runat="server" Text="submit" OnClick="btnSubmit_Click" />        </div>        </form>    </body>    </html> 
```

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; namespace DataCachingSam{    public partial class LoginForm : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {         }         protected void btnSubmit_Click(object sender, EventArgs e)        {            Cache["UserName"] = txtUserName.Text;            Cache["Pwd"] = txtpwd.Text;            Response.Redirect("DataCaching.aspx");        }    }}
```

---

Original Source: https://www.mindstick.com/blog/783/caching-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
