Caching ASP Content
When clients access your ASP page, the response can come from one of the following two sources.
The ASP page can either obtain information from server resources, such as from data that has been persisted to a database, or
The ASP page can obtain information from within the application.
Retrieving data from a resource outside the application requires more processing steps, and therefore requires more time than generating the data from within the application space.
If the requested resource has already been prepared during a previous request, the application can retrieve that data faster if it is stored in a cache. This form of caching is called output caching. Output caching is particularly suitable when returning the same data in the same format for many different requests. For example, one common task for developing an input form is to gather persisted data as members of a drop-down list box. This is preferable to writing the entries directly into the HTML page, because updates to the persisted data will automatically be reflected in the form.
The following example shows you how to use JScript to perform output caching. In this example, the getSportsListBox function creates a list box from persisted data. The list box is added to the application space so that clients can access it more quickly than they could if they populated the list box on an individual basis. The example assumes that a Data Source Name (DSN) called "Sports" is defined on the server.
<%@ LANGUAGE=JavaScript %>
<HTML>
<BODY>
<FORM METHOD=post>
What is your favorite sport? <%= getSportsListBox() %>
<P>
<INPUT TYPE=submit>
</FORM>
</BODY>
</HTML>
<%
function getSportsListBox()
{
SportsListBox = Application("SportsListBox");
If (SportsListBox != null) return SportsListBox;
crlf = String.fromCharCode(13, 10);
SportsListBox = "<select name=Sports>" + crlf;
SQL = "SELECT SportName FROM Sports ORDER BY SportName";
cnnSports = Server.CreateObject("ADODB.Connection");
cnnSports.Open("Sports", "WebUser", "WebPassword");
rstSports = cnnSports.Execute(SQL);
fldSportName = rstSports("SportName");
While (!rstSports.EOF)
{
SportsListBox = SportsListBox + " <option>" + fldSportName + "</option>" + crlf;
rstSports.MoveNext();
}
SportsListBox = SportsListBox + "</select>"
Application("SportsListBox") = SportsListBox;
return(SportsListBox);
}
%>
In some circumstances, an application receives many different requests for the same data, but the presentation of that data needs to change for each request. In this case, use input caching. With input caching the data is saved, but not the presentation of it. A Dictionary object or ADO recordsets can be used for input caching.
Excerpt from Global.asa:
The following examples show you how to use the VBScript scripting language to cache data by adding a connectionless recordset to your application. ASP scripts within the application space can then access the recordset rather than retrieve the data from the database.
The first example exists in the Global.asa file for the application. It creates the recordset and adds it to the application space. The second example is an ASP script in the application that populates the recordset and makes it connectionless by setting the ActiveConnection property to Nothing. The ASP script then clones this recordset and iterates through its values, which is much faster than accessing the database itself. This technique is appropriate only if you know that the data that will be used to populate the recordset is relatively stable.
<OBJECT ID=rsCustomers PROGID="ADODB.Recordset" RUNAT="Server" SCOPE="Application">
</OBJECT>
<!--METADATA TYPE="TypeLib" FILE = "C:\Program Files\Common Files\system\ado\msado15.dll"-->
<%
SQL = "SELECT CompanyName, City FROM Customers"
Cnn = "DSN=AdvWorks" rsCustomers.CursorLocation = adUseClient
rsCustomers.Open SQL, Cnn, adOpenStatic, AdLockReadOnly
rsCustomers.ActiveConnection = Nothing
Set myCustomers = Application("rsCustomers").Clone
Set CompanyName = myCustomers("CompanyName")
Set City = myCustomers("City")
Do Until myCustomers.EOF
%>
<B><%= CompanyName %></B> is located in <B><%= City %></B>.<P>
<%
myCustomers.MoveNext
Loop
%>