What I wrote before: This article is still related to ASP. I believe everyone who plays ASP feels this way. When there are more than 50,000 pieces of data - just like the music network, the latest 10 pieces need to be called and displayed on the page. ,The bad thing is that when n multiple users open the page for access, each user has to read the database once each time, which undoubtedly reduces the efficiency. Obviously, if the data can be saved in the memory and then read, It undoubtedly speeds up the process. The so-called cache is actually to open up a space in the memory to save data. Using cache, you don’t have to frequently access the data you save on the hard disk, because we hope that every user can see this data. The effect is the same. Consider using the application object because it is a common object for all visitors. The stored information and defined events can be used by the owner visitor. Here we need to use the asp built-in object APPLICATION. Regarding application, there are 2 One method [lock and unlock], two collections [content and staticobjects], two events [application_onstart and application_end], the application variable will not disappear because the user leaves. Once established, it will wait until the website is closed and the program is uninstalled. , Because of this, you must be particularly careful when using it! , Otherwise it will occupy memory. I don’t need to say more here. If you are interested, check out the relevant information. It is roughly like this. We are writing the data into a custom application. The general idea is to read and refresh at the specified time.
Example demonstration. First create a simple database, write a function to read it, and write it into a dim variable temp:
The following is a reference fragment:
Function DisplayRecords()
'This function originally assigned the recorded value Dim sql, conn, rs to a variable temp
'Conditional sql statement sql = "SELECT id, [szd_f], [szd_t] FROM admin"
'Open database connection Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="&Server.MapPath("db.mdb")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 3
'When the data matching the sq statement l has not been displayed, If Not rs.EOF Then
'Assign Dim temp to the temp variable
temp = "<table width=""90%"" align=""center"""
temp = temp & " border=""1"" bordercolor=""silver"""
temp = temp & " cellspacing=""2"" cellpadding=""0"">"
temp = temp & "<tr bgcolor=""#CCDDEE""><td width=""5%"""
temp = temp & ">ID</td><td>Operation</td>"
temp = temp & "<td>Number</td></tr>"
While Not rs.EOF
temp = temp & "<tr><td bgcolor=""#CCDDEE"">"
temp = temp & rs("ID") & "</td><td>" & rs("szd_f")
temp = temp & "</td><td>" & rs("szd_t")
temp = temp & "</td></tr>"
rs.MoveNext
Wend
temp = temp & "</table>"
'After the temp assignment is completed, return it to the function DisplayRecords = temp
Else
DisplayRecords = "Data Not Available."
End If
'Release memory rs.Close
conn.Close
Setrs=Nothing
Set conn = Nothing
End Function
ok, the above function has been transformed, and it is DisplayRecords when called.
Here is where the application comes into play:
'This function is to write to the cache Function DisplayCachedRecords(Secs)
Dim retVal, datVal, temp1
'Secs is the time to refresh the data each time, retVal is the data, datVal is the remaining time retVal = Application("cache_demo") 'Get the value of application datVal = Application("cache_demo_date") 'Get the value of application' Judge the value of datVal , that is, to calculate whether time has passed. If datVal = "" Then
'If it is empty, the datVal value is the current time in seconds plus the time defined by secs datVal = DateAdd("s",Secs,Now)
End If
'temp1 is the second difference between the current time and datVal temp1 = DateDiff("s", Now, datVal)
'If retVal is already the return value of the above function and the time is greater than 0
If temp1 > 0 And retVal <> "" Then
'This function returns the number of records DisplayCachedRecords = retVal
Response.Write "<b><font color=""green"">Use cache to read data"
Response.Write " ... (" & temp1 & " seconds remaining)</font></b>"
Response.Write "<br><br>"
Else
If 'retVal is empty, assign the value of DisplayRecords to variable temp2
Dim temp2
temp2 = DisplayRecords()
'Save to Application.------------------>Focus Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' The past time of the recorded cache is casually written here, and the difference relative to the total number of seconds is:
Response.Write "<b><font color=""red"">Refresh cache display..."
Response.Write "</font></b><br><br>"
End If
End Function
%>
The explanation is complete.
The following is the complete uncommented code
calling method: <%=DisplayCachedRecords(20)%>
Written at the end: If you feel that your server memory is not large enough, do not use a large amount of cache.