Classic ASP Data Caching for Performance
December 2nd, 2011 | Published in Web Development
Here is an OLD article I had about data caching in classic asp using the application object. When it comes to application performance on your web servers, ever little bit counts.
Howto build a database caching system
Introduction
In this tutorial I will teach you how to build a simple database caching system to improve the load on your database. This tutorial is meant for anyone, including beginners and experts.
What is a caching system?
A caching system is something which stores frequently accessed data in a file or variable, and allows for quick retrieval. Caching systems can be built in different ways; there are very basic ones and very complex ones. For example, AspIt is running an extremely advanced caching system. In this tutorial I’ll show you how to make a basic caching system.
Let’s begin with the code
First create a new asp page called caching.asp. Then copy and paste the following code in it;
Dim objConn, strQuery '// Get connection Set objConn = Server.CreateObject ("ADODB.Connection") objConn.Open strConnectionString '// Get recordset Set objRS = Server.CreateObject("ADODB.RecordSet") objRS.Open "SELECT userid, username FROM users", objConn '// Loop through results Do While Not objRS.EOF Response.Write objRS("userid") & " - " & objRS("username") & " " objRS.MoveNext Loop '// Close recordset objRS.Close '// Clean up Set objRS = Nothing Set objConn = Nothing
If you don’t know what this does, it simply opens a connection first, then gets a list of records, and finally closes the recordset and connection.[size=1]Please note that there is a better way to get the results (GetRows).[/size]
Everytime someone visits this page the same thing happens over and over again, even though the data doesn’t change that much. It seems useless to keep re-doing this over and over again, no? That’s where a caching system kicks in. As I said earlier, it caches frequently used data, so it would be perfect for our page.
A good place to store cached data in is the Application object, because it can be accessed by anyone! If you store the cached data in the Session object, only one user can access the data, and it’s lost after 20 or so minutes. With the Application object, the data is never lost, until the server is rebooted.
Let’s put our caching system in place. Copy and paste (make sure you overwrite our previous code) the following code into our asp page;
Dim objConn, strQuery, cachedData, htmlString '// Retrieve data from cache (if it's in there) cachedData = Application("userlist") '// Check if data is in cache If IsEmpty(cachedData) = False Then '// Data IS in cache, display the list Response.Write cachedData Else '// Data is NOT in cache, get records from database '// Get connection Set objConn = Server.CreateObject ("ADODB.Connection") objConn.Open strConnectionString '// Get recordset Set objRS = Server.CreateObject("ADODB.RecordSet") objRS.Open "SELECT userid, username FROM users", objConn '// Loop through results Do While Not objRS.EOF htmlString = htmlString & objRS("userid") & " - " & objRS("username") & " " objRS.MoveNext Loop '// Close recordset objRS.Close '// Add records to cache Application.Lock Application("userlist") = htmlString Application.UnLock '// Display string Response.Write htmlString '// Clean up Set objRS = Nothing Set objConn = Nothing End If
After you’ve pasted this code in the asp page, run it twice, and (hopefully) notice the slight speed increase when you ran it the second time. It might not be noticeable because the asp page doesn’t contain much code, but when your pages are bigger, and you’re doing more, you will notice!
One problem, solution?
Now that we’ve got a caching system, there’s one critical problem. What if the data gets changed? The cached data won’t be changed, and if something does get changed, incorrect data will be displayed.
Luckily, this is easily fixed. The cache just needs to be invalidated. There are several ways of doing this. Here, on AspIt, I have it set so that cached data is invalidated when a non-SELECT query (e.g UPDATE) is made to the database. For our example however, we are going to do it easier. Just invalidate the cached data within a certain time, in this example a day.
Let’s add our “invalidator”, so we will have a truly perfect caching sytem! Copy and paste the following code, or only add in the changes into our asp page;
Dim objConn, strQuery, cachedData, htmlString '// Retrieve data from cache (if it's in there) cachedData = Application("userlist") '// Check if data is in cache If IsEmpty(cachedData) = False And Application("cachedata") < Date Then '// Data IS in cache and it's NOT invalid, display the list Response.Write cachedData Else '// Data is NOT in cache or IS invalid, get records from database '// Get connection Set objConn = Server.CreateObject ("ADODB.Connection") objConn.Open strConnectionString '// Get recordset Set objRS = Server.CreateObject("ADODB.RecordSet") objRS.Open "SELECT userid, username FROM users", objConn '// Loop through results Do While Not objRS.EOF htmlString = htmlString & objRS("userid") & " - " & objRS("username") & " " objRS.MoveNext Loop '// Close recordset objRS.Close '// Add records to cache Application.Lock Application("userlist") = htmlString '// Add cache date Application("cachedate") = DateAdd("d", 1, Date) Application.UnLock '// Display string Response.Write htmlString '// Clean up Set objRS = Nothing Set objConn = Nothing End If
You’ve finished your caching system, and (if correctly) applied should improve your database-load by high numbers (I’m guessing atleast 30%). Of course this was a very basic caching system, and I’m sure there are much better ways (Heck, AspIt uses a complete different system, but it’s the same priniciple).