How to Enhance ASP program performance (1)

Source: Internet
Author: User
Tags dsn object model
Program | How performance enhances ASP program performance (1)
2000-08-10 · Gan Ganping · Yesky

Brief introduction

Performance is a very important feature. You need to design the performance metrics beforehand, otherwise you'll have to rewrite the program for that. is to imagine how to optimize the implementation of ASP programs?

This paper presents some techniques for optimizing ASP application and VBScript, and many techniques and defects have been discussed. The recommendations listed here have been tested on http://www.microsoft.com and other sites and are working very well. This article assumes that you have the basics of ASP development, including VBScript or jscript,asp applications, ASP sessions, and other ASP built-in objects (Request,response and server).

Typically, the performance of an ASP is much more than just the ASP code itself! Performance-related resources are listed at the end of this article, which includes ASP and non-ASP parts, including ActiveX Data Objects (ADO), Component Object Model (COM), database, And the configuration of Internet Information Server (IIS). In addition to these, there are some very good links worth seeing.

Tip 1: Caching frequently used data on a Web server

Typically, an ASP page retrieves data from a background store and then forms the result in Hypertext Markup Language (HTML). Retrieving data from memory is much faster than from a background storage device, regardless of the speed of the database. Reading data from your local hard disk is usually very fast. Therefore, improving performance can be achieved by caching the data on the server, either by caching the data in memory or by the local hard drive.

Caching is the classic "space for Time" compromise. If the cache is properly cached, you can see significant performance improvements. In order for caching to be effective, it is necessary to ensure that cached data is often reused and computationally cumbersome. Caching a cache full of stale data is a waste of memory.

Data that is infrequently changed is a good object for caching because it does not need to be considered at any time when the data is updated for synchronization operations. combo boxes, reference tables, DHTML code, extended Markup language strings, menus, and site configuration variables (including data source name dsns,internet protocol address IP and Web path) are good caching objects. Note: You want to cache the data expression instead of the data itself. If an ASP page changes frequently and is laborious to cache (such as the entire product catalog), consider generating HTML instead of describing it every time a request occurs.

Tip 2: Cache frequently used data in application or session objects

The application and session objects in ASP are convenient containers for caching data in memory. You can assign data to application and session objects, which will remain in memory for the duration of the HTTP call. The data in the session is serviced for each user, and the data in the application is shared by all users.

When do I need to mount data in application and session? Typically, when the application starts or the session begins, the data is loaded. To load the data at this point, add the appropriate code to the application OnStart () or Session OnStart (). These functions are in the file Global.asa and are added if they do not exist. You can also call in when the data is needed for the first time, add code to the ASP page, check whether the data exists, and if it is not found, transfer it. Here is an example of what is called the "lazy evalution" classical processing technology: It is not calculated until it is needed. Examples are as follows:

<%
Function getemploymentstatuslist
Dim D
D = Application ("Employmentstatuslist")
If d = "" Then
' Fetchemploymentstatuslist function (not shown)
' Fetches data from DB, returns an Array
D = fetchemploymentstatuslist ()
Application ("employmentstatuslist") = d
End If
Getemploymentstatuslist = d
End Function
%>

For different data, you can write a similar function code.

What format should the data be saved in? Any variable type can be, because all script variables are different. For example, you can save as a string, integer, or data. Typically, the contents of an ADO recordset are stored in one of these variable types. To extract data from an ADO recordset, you need to manually copy the data to a VBScript variable, one field at a time. Using a function of any ADO recordset functions GetRows (), GetString (), or Save () (ADO 2.5) is very fast and simple, and here's a function that describes how to use GetRows () to return an array of recordset data:

' Get Recordset, return as an Array
Function fetchemploymentstatuslist
Dim RS
Set rs = CreateObject ("ADODB.") Recordset ")
Rs. Open "Select Statusname, Statusid from Employeestatus", _
"Dsn=employees;uid=sa;pwd=;"
Fetchemploymentstatuslist = Rs. GetRows () "Return to data as an Array
Rs. Close
Set rs = Nothing
End Function

A deeper technique for the above code is to cache HTML for the list. Here's a simple example:

' Get Recordset, return as HTML Option list
Function fetchemploymentstatuslist
Dim RS, Fldname, S
Set rs = CreateObject ("ADODB.") Recordset ")
Rs. Open "Select Statusname, Statusid from Employeestatus", _
"Dsn=employees;uid=sa;pwd=;"
s = "<select name=" "Employmentstatus" > "& vbCrLf
Set fldname = Rs. Fields ("Statusname") ' ADO Field Binding
Do Until Rs. Eof
' Next line violates Don ' t does String concats,
' But it ' s OK because we are building a cache
s = S & "<option>" & Fldname & "</option>" & VbCrLf
Rs. MoveNext
Loop
s = S & "</select>" & VbCrLf
Rs. Close
Set rs = Nothing ' Early
Fetchemploymentstatuslist = S ' return data as a String
End Function

In the right environment, the ADO recordset itself can be cached in application or session, but there are 2 hints:

ADO must be a free thread-flagged
Need to use the disconnected recordset method
If you cannot guarantee the above 2 conditions, do not cache the ADO recordset, because this can be very dangerous.

When you save the data in a application or session, the data remains until the program changes it, the session variable expires, or the Web application restarts. What if the data needs to be updated? You can call an ASP page that only an administrator can access to update the data or, periodically, automatically update the data through the function. In the following example, the clock tag is saved with the cached data, and the data is refreshed after a period of time.

<%
' ERROR handing not shown ...
Const update_interval = ' Refresh INTERVAL, in seconds

' Function to return ' employment status list
Function getemploymentstatuslist
Updateemploymentstatus
Getemploymentstatuslist = Application ("Employmentstatuslist")
End Function

' Periodically update the cached data
Sub updateemploymentstatuslist
Dim D, Strlastupdate
Strlastupdate = Application ("Lastupdate")
If (strlastupdate = "") Or _
(Update_interval < DateDiff ("s", Strlastupdate, now) Then

' Note:two or more calls might get in. This is okay and would simply
' result in a few unnecessary fetches (there are a workaround for this)

' Fetchemploymentstatuslist func



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.