Data Caching in ASP.Net Assignment Help

Assignment Help: >> ASP.Net Programming >> Data Caching in ASP.Net

Data Caching in ASP.Net

Caching is a method of storing frequently used information in memory, so that, when the similar information is needed next time, it should be directly retrieved from the memory instead of being prepared by the application.

Caching is extremely useful for performance boosting in ASP.Net, as the pages and controls are dynamically prepared here. It is especially useful for data related transactions, as these are expensive in parts of response time.

Caching places currently used data in quickly accessed media like the random access memory of the computer. The ASP.Net runtime includes a key-value map of CLR objects known as cache. This lives with the application and is available via the System.Web.UI.Page and HttpContext.

In some respect, caching is same to storing the state objects. However, the storing data in state objects is deterministic, i.e., you may count on the data being saved there, and caching of data is nondeterministic.

The data cannot be available if its lifetime expires, or the application releases its memory, or caching does not took place for some cause.

You may access items in the cache needing an indexer and can control the lifetime of objects in the cache and set up links between the cached objects and their physical parts.

Caching in ASP.Net:

ASP.Net gives the subsequent different types of caching:

  • Output Caching: Output cache stores a copy of the starting rendered HTML pages or part of pages transmit to the client. When the next client requests for this page, despite of generating the page, a cached copy of the page is transmit, thus saving time.
  • Data Caching: Data caching seems caching data from a data source. As long as the cache is not gone, a request for the data will be filled from the cache. When the cache is gone, fresh data is calculated by the data source and the cache is filled.
  • Object Caching: Object caching is caching the objects on a web page, such as data-bound controls. The cached data is saved in server memory.
  • Class Caching: Web services or Web pages or are compiled into a page class in the assembly, when execute for the first time. Then the assembly is cached in the server. Next time when a request is send for the page or service, the cached assembly is send to. When the source code is modified, the CLR recompiles the assembly.
  • Configuration Caching: Application wide configuration information is saved in a configuration file. Configuration caching saves the configuration information in the server memory.

Output Caching:

Rendering a page can have some complex processes like, rendering complex controls, database access etc. Output caching allows bypassing the round trips to server by caching data in storage. Even the entire page may be cached.

The OutputCache directive is responsible of output caching. It actives output caching and gives certain control over its nature.

Syntax for OutputCache directive:

<%@ OutputCache Duration="15" VaryByParam="None" %>

Put this directive inside the page directive . This gives the environment to cache the page for 15 seconds. The subsequent event handler for page load could help in testing that the page was exactly cached.

protected void Page_Load(object sender, EventArgs e)

{

   Thread.Sleep(10000); 

   Response.Write("This page was generated and cache at:" +

   DateTime.Now.ToString());

}

The Thread.Sleep() function ends the process thread for the given time. In this example, the thread is stopped for 10 seconds, so when the page is loaded for first time, it will take 10 seconds. But next time you refresh the page, it does not give any time, as the page will saved from the cache without being loaded.

The OutputCache directive has the subsequent attributes, which gives in controlling the nature of the output cache:

Attribute

Values

Description

DiskCacheable

true/false

Denotes that output could be written to a disk based cache.

NoStore

true/false

Denotes that the "no store" cache control header is sent or not.

CacheProfile

String name

Name of a cache profile as to be saved in web.config.

VaryByParam

None
*
Param- name

Semicolon delimited list of string Denotes query string gives in a GET request or variable in a POST request.

VaryByHeader

*
Header names

Semicolon delimited list of strings denoting headers that might be submitted by a client.

VaryByCustom

Browser
Custom string

Tells ASP.Net to vary the output cache by browser version and name or by a custom string.

Location

Any
Client
Downstream
Server
None

Any: page can be cached anywhere.
Client: cached content remains at browser.
Downstream: cached content saved in downstream and server both.
Server: cached content saved only on server.
None: disables caching.

Duration

Number

Number of page or control is cached.

Let us include a text box and a button to the last example and include this event handler for the button.

protected void btnmagic_Click(object sender, EventArgs e)

{

   Response.Write("<br><br>");

   Response.Write("<h2> Hello, " + this.txtname.Text + "</h2>");

}

And change the OutputCache directive :

<%@ OutputCache Duration="60" VaryByParam="txtname" %>

When the program is executed, ASP.Net caches the page on the types of the name in the text box.

Data Caching:

The basic type of data caching is caching the data source controls. We have already defined that the data source controls show data in a data source, like a database or an XML file. These controls derive from the abstract class DataSourceControl and have the subsequent inherited properties for adding caching:

  • CacheDuration - sets the number of types for which the data source will cache data
  • CacheExpirationPolicy - describes the cache nature when the data in cache has expired
  • CacheKeyDependency - checks a key for the controls that auto-expires the content of its cache when replaced
  • EnableCaching - Shows whether or not to cache data

Example:

To demonstrate data caching, take a new website and include a new web form in it. Include to it a SqlDataSource control with the database connection already taken in the data access tutorials.

For this example, include a label to the page, which could show the response time for the page.

<asp:Label ID="lbltime" runat="server"></asp:Label>

Apart from the label, the content page is same as in the data access tutorial. include an event handler for the page load event:

protected void Page_Load(object sender, EventArgs e)

{

   lbltime.Text = String.Format("Page posted at: {0}",

                  DateTime.Now.ToLongTimeString());

}

The design page chould look like the following:

712_data cashing.png

When you execute the page for the first time, nothing different occur, the label denotes that, each time you refresh the page, the page is loaded and the time defined on the label changes.

Next, set the EnableCaching attribute of the data control to be 'true' and given the Cacheduration attribute to '60'. It can implement caching and the cache will end every 60 seconds.

Now the timestamp will modify with every refresh, but if you modify the data in the table within these 60 seconds, it won't denote before the cache expires:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:

ASPDotNetStepByStepConnectionString %>"

ProviderName="<%$ ConnectionStrings:

ASPDotNetStepByStepConnectionString.ProviderName %>"

SelectCommand="SELECT * FROM [DotNetReferences]"

EnableCaching="true" CacheDuration = "60">        

</asp:SqlDataSource>

Example:

Make a page with just a button and a label. Write the subsequent code in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

   if (this.IsPostBack)

   {

      lblinfo.Text += "Page Posted Back.<br/>";

   }

   else

   {

      lblinfo.Text += "page Created.<br/>";

   }

   if (Cache["testitem"] == null)

   {

      lblinfo.Text += "Creating test item.<br/>";

      DateTime testItem = DateTime.Now;

      lblinfo.Text += "Storing test item in cache ";

      lblinfo.Text += "for 30 seconds.<br/>";

      Cache.Insert("testitem", testItem, null,

      DateTime.Now.AddSeconds(30), TimeSpan.Zero);

   }

   else

   {

      lblinfo.Text += "Retrieving test item.<br/>";

      DateTime testItem = (DateTime)Cache["testitem"];

      lblinfo.Text += "Test item is: " + testItem.ToString();

      lblinfo.Text += "<br/>";

   }

   lblinfo.Text += "<br/>";

}

When the page is executed for the first time it says:

Page Created.

Creating test item.

Storing test item in cache for 30 seconds.

If you click on the button again within 30 seconds, the page is loaded back but the label control gives its data from the cache:

Page Posted Back.

Retrieving test item.

Test item is: 14-07-2010 01:25:04

Email based ASP.Net assignment help - homework help at Expertsmind

Are you searching ASP.Net expert for help with Data Caching in ASP.Net questions?  Data Caching in ASP.Net topic is not easier to learn without external help?  We at www.expertsmind.com offer finest service of ASP.Net assignment help and ASP.Net homework help. Live tutors are available for 24x7 hours helping students in their Data Caching in ASP.Net related problems. Computer science programming assignments help making life easy for students. We provide step by step Data Caching in ASP.Net question's answers with 100% plagiarism free content. We prepare quality content and notes for Data Caching in ASP.Net topic under ASP.Net theory and study material. These are avail for subscribed users and they can get advantages anytime.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving ASP.Net queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd