通常,ASP的執行性能遠遠不僅僅依賴ASP代碼本身!在本文的尾部列出了與性能相關的資源,它們含概了ASP和非ASP的部分,包含ActiveX Data Objects(ADO),Component Object Model(COM),數據庫(Database),以及Internet信息服務器(IIS)的配置。除了這些,還有一些非常好的鏈接值得你一看。
<% 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 %>
' 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 data as an Array rs.Close Set rs = Nothing End Function
上述代碼的一個更深的技巧是為列表緩存了HTML。下面是個簡單的例子:
' 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 Do 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 ' See Release Early FetchEmploymentStatusList = s ' Return data as a String End Function
<% ' error handing not shown... Const UPDATE_INTERVAL = 300 ' Refresh interval, in seconds
' Function to return the 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 here. This is okay and will simply ' result in a few unnecessary fetches (there is a workaround for this)
' FetchEmploymentStatusList function (not shown) ' fetches data from DB, returns an Array d = FetchEmploymentStatusList()
' Update the Application object. Use Application.Lock() ' to ensure consistent data Application.Lock Application("EmploymentStatusList") = d Application("LastUpdate") = CStr(Now) Application.Unlock End If End Sub
有另外一個例子,請參閱 World’s Fastest ListBox with Application Data。
Scripting.FileSystemObject 允許你創建、讀取和寫文件 MSXML,Microsoft XML 解析器隨Internet Explorer而來,支持保存和裝入XML文檔 LookupTable對象(比如在MSN上使用)是從磁盤調入簡單列表的很好選擇。 最后,考慮緩存磁盤數據的表達式,而不是數據本身。預處理的HTML可以存儲為.htm或者.asp文件,鏈接直接指向它們。使用諸如XBuilder或者Microsoft SQL Server Internet發布類的商業工具,能夠自動處理這些過程。而且,也可以在.asp文件中包含HTML程序片斷。同樣,也可使用FileSystemObject從磁盤上讀取HTML文件,或者使用XML for early rendering來做這個工作。