120 lines
6.5 KiB
HTML
120 lines
6.5 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
|
|
<TITLE>Caching Data</TITLE>
|
|
<SCRIPT LANGUAGE="JavaScript">
|
|
<!--
|
|
TempString = navigator.appVersion
|
|
if (navigator.appName == "Microsoft Internet Explorer"){
|
|
// Check to see if browser is Microsoft
|
|
if (TempString.indexOf ("4.") >= 0){
|
|
// Check to see if it is IE 4
|
|
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
|
}
|
|
else {
|
|
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
|
}
|
|
}
|
|
else if (navigator.appName == "Netscape") {
|
|
// Check to see if browser is Netscape
|
|
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
|
|
}
|
|
else
|
|
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
|
|
//-->
|
|
</script>
|
|
<SCRIPT LANGUAGE="VBScript">
|
|
<!--
|
|
Sub Window_OnLoad()
|
|
Dim frmContents
|
|
On Error Resume Next
|
|
If Not Parent Is Nothing Then
|
|
Set frmContents = Parent.Contents
|
|
If Not frmContents Is Nothing Then
|
|
frmContents.Window.TOCSynch_Click
|
|
End If
|
|
End If
|
|
End Sub
|
|
//--></SCRIPT><META NAME="DESCRIPTION" CONTENT="Internet Information Services reference information">
|
|
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
|
|
<META NAME="MS.LOCALE" CONTENT="EN-US">
|
|
<META NAME="MS-IT-LOC" Content="Internet Information Services">
|
|
</HEAD>
|
|
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
|
|
|
|
<H3><A NAME="_k2_caching_data"></A><SUP></SUP>Caching Data</H3>
|
|
|
|
<P>When clients access your ASP page, there are basically two ways to provide them with the information they need:
|
|
|
|
<UL type=disc>
|
|
<LI>Your ASP page can either obtain information from server resources, such as from data that has been persisted to a database, or</li>
|
|
|
|
<LI>Your ASP page can obtain information from within the application.</li>
|
|
</UL>
|
|
|
|
<P>Retrieving data from a resource outside the application will require more processing steps, and will therefore require more time than generating the data from within the application space.</P>
|
|
|
|
<P>If the data you are going to send to the browser has already been prepared by a previous request, your application will be able to retrieve that data faster if you store it in a cache. This form of caching is called <I>output caching</I>. Output caching is particularly suitable when you expect to return 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.</P>
|
|
|
|
<P>The following script is an example of output caching. In this example, the <B>getSportsListBox</B> 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.</P>
|
|
|
|
<PRE><CODE><%@ 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;
|
|
}
|
|
%>
|
|
</CODE></PRE>
|
|
|
|
<P>In some circumstances, your application will receive many different requests for the same data, but you will need to change the presentation of that data for each request. In that case, you use <I>input caching</I>. With input caching you save the data, but not the presentation of it. You accomplish this by caching the data with a <B>Dictionary</B> object provided by VBScript, or with an ADO recordset.</P>
|
|
|
|
<P>The following example demonstrates caching 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 following two ASP scripts demonstrate this technique.</P>
|
|
|
|
<P>Excerpt from Global.asa:</P>
|
|
|
|
<PRE><CODE><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
|
|
%>
|
|
</CODE></PRE>
|
|
|
|
<P>The application's Global.asa file creates the recordset and adds it to the application space. The ASP script then populates the recordset and makes it connectionless by setting the <B>ActiveConnection</B> 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.</P>
|
|
<hr class="iis" size="1">
|
|
<p align="center"><em><a href="../../../common/colegal.htm">© 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
|
|
</BODY>
|
|
</HTML>
|