LINQ CACHING
LINQ CACHING
Importance of Caching
1. All the data needed in an application are not stored in the same source. The source
could be a relation database, some business object, XML file, or a web service.
2. Accessing in-memory object is simpler and less expensive than accessing data from a
database or XML file.
3. The data accessed are not used directly, but needs to be sorted, ordered, grouped,
altered etc.
LINQ is set of extensions to the .Net Framework 4.5 and its managed languages that set the
query as an object. It defines a common syntax and a programming model to query different
types of data using a common language.
.Net framework 4.5, which support the LINQ syntax makes it possible to work with a configured
data store without resorting to ADO.NET.
Caching in ASP.NET is the ability to store a website page or data in the memory for rapid access.
In ASP.NET, you need not hit the server for the same response again and again. Store it in the
computer memory and fetch it faster. This works only with static data because dynamic data
varies with every request made to the server.
Any site is developed for a large number of users, in other words for the same request to work
the same for a large number of clients so it is hard to provide the best performance for the
server so we use caching.
1
When the data is accessed again, applications can get the data from the cache instead of
retrieving it from the original source.
This can improve performance and scalability.
In addition, caching makes data available when the data source is temporarily unavailable.
The caching classes in the System.Runtime.Caching namespace provide functionality for
caching data in ASP.NET.
1. Page Caching
2. Fragment Caching
3. Data Caching
Output Caching
Rendering a page may involve some complex processes such as, database access, rendering
complex controls etc. Output caching allows bypassing the round trips to server by caching data
in memory. Even the whole page could be cached.
2
When we compile our code it is first it compiled into Microsoft Intermediate Language (MSIL)
then The MSIL is converted into Native Code using the JIT Compiler. Now if there is a page that
changes frequently then the JIT needs to compile it every time. So, rather than generate a page
on each request we can cache the page using Page Output Caching so that it can be accessed
from the cache itself.
3
Add a web form
WebForm1.aspx code
4
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Create a table
5
WebForm1.aspx.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace CachingExp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = DateTime.Now.ToString();
//call the Drop_down_Bind method in pageload
Drop_Down_Bind();
}
}
private void Drop_Down_Bind()
{
//bind the data in drop down
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-I4T0H4I\
SQLEXPRESS;Initial Catalog=Sales;Integrated Security=True");
string query = "select UserName from registeration";
SqlDataAdapter da = new SqlDataAdapter(query, con);
6
DataSet ds = new DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "UserName";
DropDownList1.DataBind();
}
private void databind(string UserName)
{
//bind the data in gridview
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-I4T0H4I\
SQLEXPRESS;Initial Catalog=Sales;Integrated Security=True");
string query = "select UserName,Password from registeration where UserName='" +
UserName + "'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
And now if the Other Option of Drop Down List is chosen then we can't get the data back. That
happens because when I choose the First Option of the Drop Down List the request goes to the
7
server and the page is saved in cached memory so no matter if we choose the other option, To
resolve this problem we need to use
VaryByParam="*"
and it allows the multiple responses on a single Web Form.
8
Data Caching
Data Caching is basically used for reducing database contention and round-trips, in other words
by using Data Caching we store required data in a cache so the Web Server does not send a
request to the database for every request so that the database performance will also increase.
9
10
Add two web forms
11
12
Add code in WebForm1.aspx code
13
Add Code in Button1_Click , WebForm1.aspx.cs
14
User Name:-<asp:TextBox ID="tbUserName" runat="server"></asp:TextBox>
<br />
<br />
Password:-<asp:TextBox ID="tbpwd" runat="server"></asp:TextBox>
<br />
15
Code for WebForm2.aspx.cs ( Page_Load)
16
//cache value is assign on the text box
tbUserName.Text = Cache["UserName"].ToString();
tbpwd.Text = Cache["Pwd"].ToString();
17
Enter the username and password and click Submit.
18
Fragment Caching
Caching of the entire page is not good because some part of the page is common for the entire
application. So Fragmentation Catching is used for some portion of the page to be cached and we can do
Fragmentation Caching using a User Control.
19
20
21