April 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Improving Your Web App's Performance with Aggressive Data Caching, Page 2

  • October 15, 2004
  • By Mike Amundsen
  • Send Email »
  • More Articles »

Coding the Cache Object Class

The Cache Utility is nice, but it doesn't really make using the ASP.NET Cache class in a real Web application much easier. For that, you need one more class—a helper class—that provides caching for commonly used data objects. For example, a method that makes it easy to handle a cached DataSet or an XML document. The following code handles a cached DataSet object:

public DataSet GetSqlDataSet(string key, string connection,
   string query, bool refresh, int ttlSeconds, int idleSeconds)
{
   DataSet ds = null;

   try
   {
      if(refresh==false)
         ds = (DataSet)CacheUtility.GetCacheItem(key);
   }
   catch {}

   if(ds == null)
   {
      try
      {
         ds = new DataSet();
         SqlDataAdapter da = new SqlDataAdapter(query,connection);
         da.Fill(ds);
      }
      catch (Exception ex)
      {
         throw new ApplicationException("GetSqlDataSet failed.",ex);
      }

   if(idleSeconds>0 && ttlSeconds>0)
      CacheUtility.SetCacheItem(key,ds,ttlSeconds,idleSeconds);
   if(idleSeconds<1 && ttlSeconds>0)
      CacheUtility.SetCacheItem(key,ds,ttlSeconds);
   else
      CacheUtility.SetCacheItem(key,ds);
   }
   return ds;
   }
}

As you can see, this method allows you to pass the information needed to retrieve the DataSet from the cache collection (key), as well as the details needed to create the DataSet (query and connection) and store it in the cache (ttlSeconds and slidingSeconds). You'll even find an argument that allows you to "force" a refresh of the cache (refresh). The first time you use this method in your code, it will find no item in the cache, execute the database query to populate the DataSet, and then place that DataSet into the cache for later use. All subsequent calls will return the item from the cache until the lifetime or idle time is passed. At that time, the data will be retrieved from the database and again placed in the cache.

The code example that accompanies this article also includes other helper methods with optional argument lists to handle various options. The following is a list of those methods:

public DataSet GetSqlDataSet(string key, string connection,
   string query)
public DataSet GetSqlDataSet(string key, string connection,
   string query, bool refresh)
public DataSet GetSqlDataSet(string key, string connection,
   string query, bool refresh, int ttlSeconds)
public DataSet GetSqlDataSet(string key, string connection,
   string query, bool refresh, int ttlSeconds, int idleSeconds)

public DataSet GetXmlDataSet(string key, string filename)
public DataSet GetXmlDataSet(string key, string filename,
   bool refresh)
public DataSet GetXmlDataSet(string key, string filename,
   bool refresh, bool depends)

public XmlDocument GetXmlDocument(string key, string url)
public XmlDocument GetXmlDocument(string key, string url,
   bool refresh)
public XmlDocument GetXmlDocument(string key, string url,
   bool refresh, int ttlSeconds)
public XmlDocument GetXmlDocument(string key, string url,
   bool refresh, int ttlSeconds, int idleSeconds)

public XslTransform GetXslTransform(string key, string url)
public XslTransform GetXslTransform(string key, string url,
   bool refresh)
public XslTransform GetXslTransform(string key, string url,
   bool refresh, int ttlSeconds)
public XslTransform GetXslTransform(string key, string url,
   bool refresh, int ttlSeconds, int idleSeconds)

Now, all that's left is to test this class library in an ASP.NET Web page.

Testing the Cache Library

A simple way to test the library is to build an ASP.NET Web page that displays the results of a DataSet. The following code shows how you can handle a button click event that calls for the display of data from the pubs database in a data bound grid:

void btnGetData_OnClick(object sender, EventArgs args)
{
   CacheObjects co = new CacheObjects();
   bool refresh = cbxRefresh.Checked;
   int ttlSeconds = Int32.Parse(txtTTLSeconds.Text);
   int slidingSeconds = Int32.Parse(txtSlidingSeconds.Text);
   DataSet ds = co.GetSqlDataSet(key,connection,query,
      refresh,ttlSeconds,slidingSeconds);
   dgAuthors.DataSource = ds;
   dgAuthors.DataBind();
}

As you can see, using this cached version of the DataSet object requires very little extra code. Figure 1 shows the resulting Web page.



Click here for a larger image.

Figure 1: ASP.NET Web Page Displaying DataSet Results

The code download that accompanies this article also includes examples of handling XML documents, custom objects, and even simple ArrayList collections using the Cache Library.

What Have You Learned?

In this article, you learned how to use the ASP.NET Caching class to create a simple utility that enables you to cache data from various sources (database, disk files, and so forth) and control their lifetime in the cache. You also saw examples of how to write helper methods that make access to both the data and the cache quick and easy. Finally, you saw an example of how to use these helper classes within an ASP.NET Web page without much extra coding.

The length of this article does not allow for thorough coverage of the System.Web.Caching namespace. Also, the helper class examples in the code cover just a few of the possibilities. By digging into the documentation for the Caching namespace and doing a bit of experimenting on your own, you should be able to expand this simple code example into a powerful utility that will add performance and flexibility to your ASP.NET solutions.

Download the Code

To download the accompanying source code for this article, click here.

Biography

Mike Amundsen, an author and lecturer, has more than a dozen Microsoft programming books to his credit. Most recently, he covered Web development using Microsoft's .NET Framework in the Sams Publishing title ASP.NET for Developers.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel