Recommended: Make good use of ASP.NET 2.0 URL mapping Introduction: URL mapping is a new feature provided in ASP.NET 2.0. URL mapping technology helps us map a specific URL to another URL. To help understand, let's assume that you have a page called Homepage.aspx on the site to access the homepage, and all users also use it
9.3.5 Data CacheFirst of all, it is important to note that although both data cache and record set cache are used to improve performance, the two are irrelevant. The data cache is a temporary data storage area that allows the use of data in the cache instead of regenerating new data. This only applies to data that is not frequently altered but is accessed multiple times.
One of the easiest ways to cache data in ASP is to use Application and Session-wide variables. For example, suppose there are some web pages that need to choose a book type. Under normal circumstances, a include file with the following functions may be created.
<%
Function BookTypes()
Dim rsBookTypes
Dim strQuote
strQuote = Chr(34)
Set rsBookTypes = Server.CreateObject (ADODB.Recordset)
' Get the book types
rsBookTypes.Open usp_BookTypes, strConn
Response.Write <SELECT NAME= & strQuote & lstBookType & strQuote & >
While Not rsBookTypes.EOF
Response.Write & <OPTION> & rsBookTypes(Type) & </OPTION>
rsBookTypes.MoveNext
Wend
Response.Write & </SELECT>
rsBookTypes.Close
Set rsBookTypes = Nothing
End Function
%>
This is simply calling a stored procedure to get the type of the book while creating a SELECT list. The disadvantage of the above code is that every time the function is called, the database must be accessed. Therefore, re-modify this function.
<%
Function BookTypes()
Dim rsBookTypes
Dim strQuote
Dim strList
' See if the list is in the cache
strList = Application(BookTypes)
If strList = Then
' Not cached, so build up list and cache it
strQuote = Chr(34)
Set rsBookTypes = Server.CreateObject (ADODB.Recordset)
' Get the book types
rsBookTypes.Open usp_BookTypes, strConn
strList = <SELECT NAME= & strQuote & lstBookType & strQuote & >
While Not rsBookTypes.EOF
strList = strList & <OPTION> & rsBookTypes(type) & </OPTION>
rsBookTypes.MoveNext
Wend
strList = strList & </SELECT>
rsBookTypes.Close
Set rsBookTypes = Nothing
' Check the list
Application(BookTypes) = strList
End If
BookTypes = strList
End Function
%>
This code does not just open the record set, it checks whether the value of the Application variable BookType is empty. If not empty, the contents of the variable are used. If empty, the record set is opened as before. Obviously, once the first person runs this routine, the data is cached, so this is only useful for those data that are not often changed.
If you want to cache data on a user basis, you can use variables in the Session scope, but you must note that the Session has an expiration date. After expiration, the session layer variable will be cancelled together with the session, and the code may terminate the run.
Using the Web Application Stress (WAS) tool, the analysis results in Table 9-4 were obtained:
Table 9-4 Analysis results obtained using WAS tools
method
Page clicks
No cache
190
Have a cache
11000
It is obvious that performance has improved. But don't use the above method to cache everything. After all, this approach is only applicable to data that has been formatted for display. In addition, consider that if the web server only serves a specific person, it is hardly a typical web server usage. Using WAS allows you to simulate multiple users on a single server, which allows you to test your application more realistically.
By simulating a certain number of users, the Web Application Stress tool can test the tolerance of web pages. The tool has a simple graphical interface that is very easy to use. More information can be obtained from http://homer.rte.microsoft.com/ or download the tool.
Cache Objects
What should I do if I want to cache unformatted data? Can it be used in different ways in different places? Of course, you can also do this with Application or Session variables. Consider the title of the book. You may want to use this title in multiple pages, maybe display all titles in one table, or display them in a list box for users to choose from, etc. You might think of caching the record set itself without having to cache HTML text with tags.
Objects can be cached in Application or Session variables, but there are two main issues to be paid attention to:
· Objects stored in Application variables must support free threads, so they must be free thread objects or dual thread objects. This means that components created by VB cannot be cached in the Application variable.
· Storeing a unit thread object in the Session state means that the thread that created the object is the only thread that allows access to it. Therefore, IIS cannot complete thread management better because any page trying to access this object must wait for the original thread to serve the page. This will kill any chance of extending the application.
For discussion of threading problems, see Chapter 15.
By default, ADO is loaded as a unit thread object, mainly because some OLE DB providers are not thread-safe. There is a registry file in the ADO installation directory that converts ADO into a two-threaded model, thereby allowing ADO objects to be stored safely in Application and Session objects.
You might think that all the problems are solved and a significant speed increase can be achieved by using various types of objects, but that is not necessarily the case. Many have realized that since connecting to a database is a relatively expensive operation, caching Connection objects can save a lot of time when connecting again. This is true, but caching a Connection object means that the connection will never be closed, so the connection cache pool is relatively inefficient. One idea that connects to cache pools is actually to reduce the resources used on the server, and caching objects in ASP state obviously cannot reduce the use of resources. In fact, they are also increased because each object cache takes up the server's resources, which will greatly reduce the efficiency of the web server for a busy site.
So Connection objects should not be stored, but what about Recordset objects, especially disconnected recordsets? Assuming ADO has changed from a unit thread to a dual thread, there is no reason not to do this, as long as you know exactly what you are doing. Don't think that this will automatically improve the performance of ASP pages. Each cached record set takes up the server's resources in terms of memory and ASP management, so do not cache large record sets.
Share: ASP Read and Write Registration Table An example: The following is the referenced content: <%Dim ReadComputerName Set ReadComputerName=CreateObject(WScript.Shell) Dim ComputerName,Reg