Simple Caching Examples

caching

Image credit:de.wikipedia.org

Simple Caching Examples

Simple Caching Examples focus on one of the best features in .Net FrameWork . With the help of caching, we can make our application and back end database interaction super fast because the page itself or controls can not take the time to go to the back end server and take the unnecessary roundtrip

Approaches in caching

Here, we are discussing Caching Data with the SqlDataSource Control.The SqlDataSource control caches when the DataSourceMode property of it is set to DataSet.We have to enable caching because it is not the default mechanism of Asp.Net.
This example uses caching for SqlDataSource control that is configured to refresh data after every 10 seconds

<%@ Page language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Datasource Caching</title>
</head>
<body>
<form id=”form1″ runat=”server”>

<asp:SqlDataSource
id=”SqlDataSource1″
runat=”server”
DataSourceMode=”DataSet”
ConnectionString=”<%$ ConnectionStrings:MyConnection%>”
EnableCaching=”True”
CacheDuration=”10″
SelectCommand=”SELECT * From table_Admin Where [table_Admin.EmployeeType]=[Officer]”>
</asp:SqlDataSource>

<asp:GridView
id=”GridView1″
runat=”server”
AutoGenerateColumns=”False”
DataSourceID=”SqlDataSource1″>
<columns>
<asp:BoundField HeaderText=”Years Of Experience” DataField=”YOE” />
<asp:BoundField HeaderText=”Salary” DataField=”Sal” />
<asp:BoundField HeaderText=”Age” DataField=”Age” />
</columns>
</asp:GridView>

</form>
</body>
</html>

The next approach is based on SqlCacheDependency object. With this technique, The refresh of the control etc. is triggered whenever the back end table changes occur such as updating the records by another client.

Our Web.config file is given below

<?xml version=”1.0″?>
<configuration>
<connectionStrings>
<add name=”MyConnection”
connectionString=”Data Source=”localhost”;Integrated Security=”SSPI”;Initial Catalog=”MyDatabase””
providerName=”System.Data.SqlClient” />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled=”true”>
<databases>
<add
name=”MyDatabase”
connectionStringName=”MyConnection”
pollTime=”24000″ />
</databases>
</sqlCacheDependency>
</caching>

</system.web>
</configuration>

<%@ Page language=”c#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>SqlCacheDependency</title>
</head>
<body>
<form id=”Form1″ method=”post” runat=”server”>

<asp:gridview
id=”GridView1″
runat=”server”
datasourceid=”SqlDataSource1″ />

<asp:sqldatasource
id=”SqlDataSource1″
runat=”server”
connectionstring=”<%$ ConnectionStrings:MyConnection%>”
selectcommand=”SELECT * From table_Admin Where [table_Admin.EmployeeType]=[Officer]”
enablecaching=”True”
cacheduration=”300″
cacheexpirationpolicy=”Absolute”
sqlcachedependency=”MyDatabase:Officer” />

</form>
</body>
</html>

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.