0% found this document useful (0 votes)
8 views

LINQ CACHING

Uploaded by

rupeshkoli921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

LINQ CACHING

Uploaded by

rupeshkoli921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

LINQ ( Language-Integrated Query)

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.

ASP 4.5 Support for Caching

Caching enables you to store data in memory for rapid access.

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.

Type of Caching 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.

Syntax for OutputCache directive:


<%@ OutputCache Duration="30" VaryByParam="*" %>
Or
<%@ OutputCache Duration="30" VaryByParam="None" %>
Put this directive under the page directive.
This tells the environment to cache the page for 30 seconds.
The following event handler for page load would help in testing that the page was really cached
Duration Attribute
This attributes represents the time in seconds of how long the output cache should be stored in
memory. After the defined duration the content stored in the memory will be cleared
automatically.
VarByParam Attribute
This is the most important attributes; you can't afford to miss that in the OutputCache directory
statement. It generally defines the query string parameters to vary the cache (in memory).

Create a new ASP.NET Empty Web Application.

3
Add a web form

WebForm1.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"


Inherits="CachingExp.WebForm1" %>
<%@ OutputCache Duration="30" VaryByParam="None" %>
<!DOCTYPE html>

4
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"


onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
Server Time:-<asp:Label ID="Label1" runat="server"></asp:Label>
<br />
<br />
Client Time:-
<script type="text/javascript">
document.write(Date())
</script>
</div>
</form>
</body>
</html>

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();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)


{
databind(DropDownList1.SelectedValue.ToString());
}
}

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

User Name:-<asp:TextBox ID="tbUserName" runat="server"></asp:TextBox>


<br />
<br />
Password:-<asp:TextBox ID="tbpwd" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />

13
Add Code in Button1_Click , WebForm1.aspx.cs

protected void Button1_Click(object sender, EventArgs e)


{
//textbox value is stored in cache
Cache["UserName"] = tbUserName.Text;
Cache["Pwd"] = tbpwd.Text;
Response.Redirect("WebForm2.aspx");

Now the following is the code for Webform2.aspx.

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();

Run the Program

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.

Add Web Forms User Control

19
20
21

You might also like