1. What is ASP caching/why should it be cached? When your web site is initially established using ASP technology, you may feel the convenience brought by ASP dynamic web page technology, as well as the freedom to modify and the free http control. However, as the number of visits increases, you will definitely find that your site access speed is getting slower and slower, and IIS restarts more and more frequently. Next, you must want to optimize asp, such as replacing the database with better performance, creating indexes, writing stored procedures, etc. Some of these measures do not require increased cost pressure, while others require significant cost pressure (such as clustering access to SQL), and the effect is not necessarily certain.
Faced with web access pressure, I think the most economical way is to use cache optimization technology to alleviate web service pressure.
Increased web traffic often means rapid growth in demand for the following resources:
1. The increase in network card traffic requires more CPU to process network traffic and network I/O threads.
2. The need to open/close database connections more frequently (if database technology is used - usually ASP uses databases as data storage), the number of things that seriously consume resources, and deadlocks caused by transactions competing for resources will increase network I/O Or CPU consumption.
3. If session is used, IIS will consume more memory in order to maintain the state, and memory consumption may cause insufficient physical memory, causing frequent exchanges between physical memory and auxiliary memory, causing code execution to pause and web response to be blocked. .
4. Due to the lack of timely response to access, it will cause web page access failure, causing users to refresh, thus exacerbating the demand for resources such as CPU and memory.
In fact, considering common web applications, many times dynamic code execution is unnecessary.
2. The classification of ASP cache can be summarized without authorization. ASP cache can be divided into two categories:
1. File caching. The so-called file caching is based on logical judgment that the specific execution of a certain ASP will not change significantly within a period of time. Therefore, the content is stored in the form of static HTML, and then the web redirection technology is used to allow customers to End-to-end access to static files to reduce the need for CPU, database resources, etc. There are many such applications. For example, many forums regenerate the entire post into a static file when replying to a post, and then redirect it, such as the donews.com forum. There is also a side effect (benefit) of being static - it can be easily indexed by search engines such as Google. Some so-called news release systems have adopted this technology.
2. File fragment caching. The so-called file caching is also based on logical judgment. A certain part of the data (usually obtained by querying a large-capacity database that consumes resources) will not change within a certain period of time, so we can store these data in the form of files. , when needed, data can be obtained by reading files to avoid increasing the burden on the database. For example, we usually store some data in XML format, and then use This is how the CSDN forum is handled.
3. Main memory cache In addition, you can also consider caching in memory, storing content that requires timely response in memory, and once access is required, it will be immediately transferred from the fast storage. If a very large number of access requirements are concentrated on a few small pages or if the main memory is large enough, I think using main memory caching can definitely greatly improve web access performance.
3. How to implement/use cache. To implement cache, you need to consider the following issues:
1. Which pages will not change in a short period of time?
Analyze your own site, there are many such pages. For example, a website usually has news and information columns. These columns are usually posted by site maintainers at a certain time of the day, and the pages are rarely changed after that. Then these pages are suitable for static file caching. In fact, this is what the so-called news release system does, so you can also refer to the ideas of these systems to transform your original dynamic ASP pages.
2. Those pages are generated using the same logic for all visitors (that is, visitors are not distinguished).
In addition to columns such as news and information where all visitors see the same interface, resource-consuming applications such as forums can generally be designed to generate unified logic (the same post will be viewed the same by three people and three people). For such application pages, we also This can be achieved using static caching. You can also consider fragmenting the data and using scripting technology to process it beyond the processing capabilities of the server, that is, the client browser.
3. The costs and gains of using caching.
The main thing is "space for (response) time". Use caching technology to preprocess content that is frequently needed in the future to improve the responsiveness of the web server and, more importantly, win the favor of visitors.
The price is that the demand for web space increases, and at the same time, the access effect may be affected.
But I think proper caching has more advantages than disadvantages.
4. Those places are not suitable for caching dynamic query pages. Everyone's query content is different, so the display results are different, so it is impossible to cache the query results. Therefore, caching is more complicated and the cache utilization rate is low, causing management problems. The cost is high (assuming you cache 1,000 query keywords, then managing the correspondence between these keywords and the cache is also troublesome).
4. Example Analysis Assume that the original layout of a suggestion forum is as follows:
Under the root directory:
default.asp homepage, usually highlights, recommendations, etc.
listBorad.asp This file lists the names and introductions of all columns. If it carries the parameter MainBID, it means that the columns under the section are to be listed.
listThread.asp If this file does not carry any parameters, it means listing all posts, and if it carries MainBID, it means listing all posts in a certain block. If subBID is carried, it means listing posts in specific columns. If the page parameter is carried, the topics are listed in pages.
ViewThread.asp lists the content of a post. We assume that the post is displayed as a comment, and any comments are listed at the end. The ID parameter is the post to be displayed.
Reply.asp responds to a certain post and carries the parameter Id to reply to a certain post. The rest will not be discussed for the time being.
From the above, we can see that if all is done using original ASP/PHP, then the execution of almost every asp file requires database operations, frequent queries, and multi-table queries. You must know that querying the database will eventually lead to a decrease in performance and response speed, which will affect visitors' slow browsing and is not conducive to the quality of the web. What's more important is that for two people, A and B, if they access ViewThread.asp and the like, if the ID is the same, then many times they will see the same content (the HTML code received by their browsers is almost the same), but for this "Same content", the server needs to open database connections, queries, read records, display, close records and database connections. . . . If more people access the following operations that consume server resources, the final result will be that these people consume server resources more. In fact, these repeated efforts for the "same content" can be avoided by using caching technology for optimization. for example:
After submitting the content in reply.asp, we immediately call the static function and store the entire post content as a static html file such as viewThread_xxxx.htm. Under normal circumstances, when accessing viewThread.asp?ID=xxxx, the system automatically Redirect to the corresponding static file viewThreadxxxx.htm. In this way, when a post does not have the latest release, it will always be static content provided to viewers; once there is a new submission, it will be updated to the static file. In this way, many database operations will be saved and the response will be greatly improved. speed.
listBorad.asp can also be implemented statically. We can analyze the parameters it may carry, set the cache file name to listBoard_xx.htm, and update listBoard_xxx.htm when adding new columns. listThread.asp is similar, except that because it has more parameters, there will be a lot of cache files. If you want to cache listThread.asp? subBID=xxx&page=2, then the corresponding static file is listThread_xxx_p2.htm. The same goes for default.asp.
So how do you tell when to update? When will it be updated?
Discussing listThread.asp? subBID=xxx&page=2, we extract subID and page when executing listThread.asp, and then detect whether listThread_xxx_p2.htm exists. If it does not exist, call the static generation function to generate the file, and finally redirect here static files. Note that the absence here means that there is new content that needs to be updated.
So how to cause the file to not exist? delete. When we publish a new post, delete a post, or move a post, we can delete all static files like listThread_xxx_p2.htm. This tells you when to cache.
Now there is one question left, how to generate static files?
We note that the "same content" we mentioned before. We can make a copy of default.asp, listThread.asp, etc. before the transformation, named default_d.asp, listThread_2.asp, and in the same directory (theoretically listThtrad.asp?subID=123 is the same as LISTtHREAD_D.ASP? The access result of SUBID=123 will be the same content), so in the logic that needs to generate static files, we call the copy before transformation through WEB access request, get the html code, and store it as a static file. This web request is actually equivalent to the server itself viewing the HTML that will be output before any real browser accesses the static content, and then returns these codes and stores them as static files using the file operation function. This way, the cache file is created before the real viewer.
Such a solution will hardly touch the original layout, and will almost never cause errors such as 404 due to modification. Secondly, static files will also help your site be easily indexed by search engines such as Google. Why not?
Finally, a reminder, through web access, in the ASP programming environment, many people use the xmlHTTP component to access, which will cause many problems. xmlhttp itself will cache the requested resources, causing the content we request through this component to be not the latest, causing logical confusion. Therefore, you should choose the xml Server http object or winhttp component to implement web request resources.
Using caching technology in ASP can greatly improve the performance of your website. In fact, these implementation methods are very simple. It will explain how caching on the server works and how you can use a method called disconnection. ADO connection technology.
Before introducing these technologies, let’s explain what exactly ASP caching technology is.
The so-called cache is actually to open up a space in the memory to save data. By using the cache, you don't need to frequently access the data you save on the hard disk. By using the cache flexibly, you can avoid the distress of watching the poor hard disk become full. I am tortured by reading data. Once you execute a query and put the query results into the cache, you can quickly access the data repeatedly. And if you don't put the data into the cache, when you execute the query again, the server will spend the process getting and sorting it from the database.
When the data is stored in the cache, the time spent when querying again is mainly the time to display the data.
In other words, we should not put data that needs to change frequently in the cache of the server. We should put data that changes less but needs to be accessed frequently in the cache.
Now we will discuss how ASP uses caching technology on the server side, and then we will discuss how ASP uses caching technology on the client side.
When you have a large amount of data (static, that is, less changing) that needs to be displayed to the client, you can consider using server-side caching technology. This technology is especially suitable for websites with strong display style consistency (haha, it is not easy to use for non-mainstream websites.)
In fact, the implementation method is very simple. You only need to look at the simple example below to understand.
This is an example program for displaying book categories
DisplayBooks.ASP file:
< %@ LANGUAGE=JavaS
script % >
<html>
<body>
<form method=post>
Book classification; < %= getBooksListBox() % >
<p>
< input type=submit >
<%
function getBooksListBox()
{
BooksListBox = Application("BooksListBox")
if (BooksListBox != null) return BooksListBox;
crlf = String.fromCharCode(13, 10)
BooksListBox = “< select name=Books>” + crlf;
SQL = “Select * FROM Books orDER BY Name”;
cnnBooks = Server.CreateObject("ADODB.Connection");
cnnBooks.Open("Books", "Admin","");
rstBooks = cnnBooks.Execute(SQL);
fldBookName = rstBooks("BookName");
while (!rstBooks.EOF){
BooksListBox = BooksListBox + ” < option>” +
fldBookName + "" + crlf;
rstBooks.MoveNext();
}
BooksListBox = BooksListBox + ""
Application("BooksListBox") = BooksListBox
return BooksListBox;
}
%>
It's very simple. In fact, it uses very simple Application technology, and the difference is only one sentence:
Application("BooksListBox") = BooksListBox
You can verify it and you will find that the number of requests on the server will be reduced a lot. This situation is especially suitable for those website content that is not updated very frequently, for example, you only update it once a day (or a long time).
Next, we will discuss a client-side caching technology. This technology is also called disconnected ADO connection technology (the translation level is too low, why does it sound so awkward). This technology is mainly used to save users' personal information, such as user passwords, code names, etc. It mainly uses some properties of ADO. At the same time, it also answers the question that some netizens have mentioned about whether ADO objects can be used in Applocation. The explanation is not clear, let the code do the talking:
File GLOBAL.ASA:
< !–METADATA TYPE=”TypeLib” FILE=”C:Program FilesCommon
Filessystemadomsado15.dll”–>
< SCRIPT LANGUAGE=VBScript RUNAT=”Server” >
Sub Application_OnStart
SQL = “Select UserName, Password FROM UserInfo”
cnnUsers = “DSN=User”
Set rsUsers = Server.CreateObject("ADODB.Recordset")
'Note that the following two sentences are used to implement the ADO technology called available disconnection.
rsCustomers.CursorLocation = adUseClient
rsCustomers.Open SQL, cnnAdvWorks, adOpenStatic, AdLockReadOnly
' Disconnect the RecordSet from the database
rsCustomers.ActiveConnection = Nothing
Set Application("rsCustomers") = rsCustomers
End Sub
FileUsers.ASP
<%
'Clone method allows each user to have his own RecordSet collection
Set yourUsers = Application("rsUsers").Clone
Set UserName = yourUsers("UserName")
Set Password = yourUsers("Password")
Do Until yourUsers.EOF
%>
User name: < %= UserName % > User password: < %= Password % >
<%
yourUsers.MoveNext
Loop
%>