..


Sponsored Links

Caching data

The data caching is a useful tool for improving the performance of any software system. The idea is to put frequently used data in a very powerful device. In fact, although the time of access to mass storage devices continue to improve, for example, access to data residing on a hard drive is still a very slower than accessing them while they are in memory. Thus make available the data more quickly used contributes significantly to improve the performance of our applications.

In the ASP.NET Cache is parallel to our applications and is available through HttpContext and System.Web.UI.Page. Use the Cache is very similar to what was seen for the Session object, as you can get access to objects in it via an indexer. In addition you can also control the duration of the set of objects and connections between objects in the cache and the physical data source.

The cache management in ASP.NET is very simple to see how to proceed with an example. Suppose you have a GetData () method that makes a connection to a database and returns a datatable






 protected DataTable GetData ()







 {



  



 DataTable dt = null;



  



 dt = new DataTable ();



  



 strConnection String = "Connection string to DB";



  



 DbProviderFactory f =



  



 DbProviderFactories.GetFactory ("System.Data.SqlClient");



  



 using (DbConnection conn = f.CreateConnection ())



  



 {



    



 conn.ConnectionString = strConnection;



    



 conn.Open ();



    



 DbCommand command = f.CreateCommand ();



    



 command.CommandText = "Select * from TableName";



    



 command.Connection = conn;



    



 IDataReader reader = Command.ExecuteReader ();



    



 dt.Load (reader);



    



 reader.Close ();



    



 conn.Close ();



  



 }



  



 return dt;







 }



We also have another method BindData () that associates the data returned by GetData () to a DataList present in one of our web form






 BindData protected DataTable ()







 {



  



 DataTable dt;



  



 this.GetData dt = ();



  



 this.DataList1.DataSource = dt;



  



 this.DataBind ();



  



 return dt;







 }



Another method that we need for our example is CreaTabella that returns the structure of a table according to a certain pattern






 CreaTabella protected DataTable (DataTable tableSchema)







 {



  



 DataTable table = new DataTable ();



  



 foreach (DataColumn dc tableSchema.Columns)



  



 {



    



 tabella.Columns.Add (dc.ColumnName,



    



 dc.DataType);



  



 }



  



 return table;







 }



Methods GetData () and BindData () are called in the Page_Load event as follows






 protected void Page_Load (object sender, EventArgs e)







 {



  



 if (IsPostBack)



  



 {



    



 DataTable dt = BindData ();



    



 DataTable elementiTabella this.CreaTabella = (dt);



    



 Session ["elementiTabella"] = elementiTabella;



  



 }







 }



and then each time the page is created you connect to the database and reloads the data. In a context where requests come from within the client what might be acceptable for applications sized to meet the demands of thousands of clients this is not acceptable. In fact, the database access operations very expensive gift in time and should be minimized.

At this point we can make some estimates of the nature of the data managed by our application. Just wondering if you need to reload the data every time if they do not change often. If there is such a need we can think of storing such data on a medium that allows access to the same very fast and without connections to the database (for example, the computer's internal memory). In this way the application would meet many more concurrent requests from clients. Obviously if the data processed by our application would change very often this approach would not be appropriate.

The steps to do data caching are:

  1. Check whether a given data is in cache
  2. If there is cached using
  3. If there is no cache to get it to connect to database
  4. Store the element just loaded in the cache for future use

At this point we are going to change the methods that we saw at the beginning of this lesson for managing data caching. Here is the method GetData () modified






 protected DataTable GetData ()







 {



  



 DataTable dt = null;

  

  



 dt = (DataTable) Cache ["TabellaInCache"];

  

  



 if (dt == null)



  



 {



    



 dt = new DataTable ();



    



 strConnection String = "Connection string to DB";



    



 DbProviderFactory DbProviderFactories.GetFactory = f ("System.Data.SqlClient");



    



 using (DbConnection conn = f.CreateConnection ())



    



 {



      



 conn.ConnectionString = strConnection;



      



 conn.Open ();



      



 DbCommand command = f.CreateCommand ();



      



 command.CommandText = "Select * from TableName";



      



 command.Connection = conn;



      



 IDataReader reader = Command.ExecuteReader ();



      



 dt.Load (reader);



      



 reader.Close ();



      



 conn.Close ();



    



 }



  



 }



  



 Cache ["TabellaInCache"] = dt;



  



 return dt;







 }



In the new method GetData () is first checked the existence of the table in the cache. If the table does not exist if (dt == null) it is created as was done previously, but if there is a part of the database connection and data extraction is completely bypassed. In any case, before returning to the table it is stored in the cache (Cache ["TabellaInCache"] = dt;). These small changes can significantly reduce the cost of loading the page (if you have already created a clear time).

Clearly, this lesson is to make you sense the potential of caching of data and to explore the various ways to cache management, its methods available and see which ones might be useful from time to time I invite you to consult the official Microsoft .

Help with Visual Studio ASP.Net
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
ASP.NET (Course) ASP.NET (Course)
Full course for building Web applications from 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links